Skip to content

Commit

Permalink
Merge pull request #134 from zardoy/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy authored Jun 10, 2023
2 parents a98aaae + 55f4aac commit 9941886
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 11 deletions.
8 changes: 8 additions & 0 deletions typescript/src/completions/isGoodPositionMethodCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ export const isGoodPositionMethodCompletion = (sourceFile: ts.SourceFile, positi
// type A = typeof obj["|"]
if (ts.isStringLiteralLike(currentNode)) return false
if (ts.isNamedExports(currentNode)) return false
if (
ts.isIdentifier(currentNode) &&
ts.isBinaryExpression(currentNode.parent) &&
currentNode.parent.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
currentNode === currentNode.parent.left
) {
return false
}
if (ts.isIdentifier(currentNode)) currentNode = currentNode.parent
if (ts.isExportSpecifier(currentNode)) return false
if (ts.isJsxSelfClosingElement(currentNode) || ts.isJsxOpeningElement(currentNode)) return false
Expand Down
7 changes: 4 additions & 3 deletions typescript/src/completionsAtPosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,10 @@ export const getCompletionsAtPosition = (
// )
const indexToPatch = prior.entries.findIndex(({ name, kind }) => name === 'toString' && kind !== ts.ScriptElementKind.warning)
if (indexToPatch !== -1) {
prior.entries[indexToPatch]!.insertText = `${prior.entries[indexToPatch]!.insertText ?? prior.entries[indexToPatch]!.name}()`
prior.entries[indexToPatch]!.kind = ts.ScriptElementKind.constElement
// prior.entries[indexToPatch]!.isSnippet = true
const entryToPatch = prior.entries[indexToPatch]!
entryToPatch.insertText = `${entryToPatch.insertText ?? entryToPatch.name}()`
entryToPatch.isSnippet = true
entryToPatch.kind = ts.ScriptElementKind.constElement
}
}

Expand Down
16 changes: 11 additions & 5 deletions typescript/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,20 @@ export default (proxy: ts.LanguageService, languageService: ts.LanguageService,
const lines = sourceFile.getFullText().split('\n')
const { line: curLine } = ts.getLineAndCharacterOfPosition(sourceFile, position)

const VLS_COMPONENT_STRING = `__VLS_templateComponents`
const isTemplateComponent = lines[curLine]?.startsWith(VLS_COMPONENT_STRING)
if (!isTemplateComponent) return
const VLS_COMPONENT_STRINGS = ['__VLS_templateComponents', '__VLS_components']

const isVLSComponent = VLS_COMPONENT_STRINGS.some(VLS_COMPONENT_STRING => lines[curLine]?.startsWith(VLS_COMPONENT_STRING))
const componentName = lines[curLine]?.match(/\.(\w+);?/)?.[1]
if (!componentName) return

prior.definitions = prior.definitions.filter(({ name }) => !(componentName === name && lines[curLine - 2] === '// @ts-ignore'))
prior.definitions =
!isVLSComponent || !componentName
? prior.definitions
: prior.definitions.filter(({ name, containerName }) => {
const isDefinitionInComponentsProperty = componentName === name && lines[curLine - 2] === '// @ts-ignore'
const isGlobalComponent = containerName === 'GlobalComponents'

return !isDefinitionInComponentsProperty || isGlobalComponent
})
}

return prior
Expand Down
5 changes: 3 additions & 2 deletions typescript/src/getPatchedNavTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const getPatchedNavModule = (additionalFeatures: AdditionalFeatures): { getNavig
// transform?: (found: string, content: string, position: number) => [string?, string?]
}
const addChildrenRecursivelySwitchFirstCase = ['function addChildrenRecursively(node)', 'switch (node.kind)']
const typeAliasCaseNeedle = [...addChildrenRecursivelySwitchFirstCase, 'TypeAliasDeclaration */']

const patchLocations: PatchLocation[] = [
{
Expand All @@ -44,7 +45,7 @@ const getPatchedNavModule = (additionalFeatures: AdditionalFeatures): { getNavig
break;`,
},
{
searchString: 'case 262 /* SyntaxKind.TypeAliasDeclaration */',
searchString: typeAliasCaseNeedle,
linesOffset: 3,
// https://github.com/microsoft/TypeScript/pull/52558/
addString: /* js */ `
Expand All @@ -54,7 +55,7 @@ const getPatchedNavModule = (additionalFeatures: AdditionalFeatures): { getNavig
`,
},
{
searchString: 'case 262 /* SyntaxKind.TypeAliasDeclaration */',
searchString: typeAliasCaseNeedle,
linesOffset: 0,
removeLines: 1,
},
Expand Down
3 changes: 2 additions & 1 deletion typescript/src/volarConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ const plugin = ((context, { typescript: tsModule } = {}) => {

const getResolvedUserConfig = async () => {
const regularConfig = await configurationHost.getConfiguration!<any>('tsEssentialPlugins')
const _vueSpecificConfig = await configurationHost.getConfiguration!<any>('[vue]')
const _vueSpecificConfig = (await configurationHost.getConfiguration!<any>('[vue]')) || {}

const vueSpecificConfig = Object.fromEntries(
compact(
Object.entries(_vueSpecificConfig).map(([key, value]) =>
Expand Down
3 changes: 3 additions & 0 deletions typescript/test/completions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ test('Banned positions for all method snippets', () => {
test({
/*|*/
})
test/*|*/ = test
;<Test/*|*/ />
;<Test/*|*/></Test>
;<Test test={/*|*/}></Test>
Expand Down Expand Up @@ -83,6 +84,8 @@ test('Not banned positions for method snippets', () => {
method: setTimeout/*|*/
})
test2/*|*/
test = test/*|*/
test/*|*/ >= test/*|*/
`)
for (const [i, pos] of cursorPositions.entries()) {
const result = isGoodPositionMethodCompletion(getSourceFile(), pos - 1, defaultConfigFunc)
Expand Down

0 comments on commit 9941886

Please sign in to comment.