Skip to content

Commit 83cd6d2

Browse files
authored
Merge pull request #130 from zardoy/develop
2 parents 247b38b + 3c40998 commit 83cd6d2

File tree

6 files changed

+66
-23
lines changed

6 files changed

+66
-23
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
test:
77
strategy:
88
matrix:
9-
os: [ubuntu-latest, windows-latest]
9+
os: [ubuntu-latest]
1010
fail-fast: true
1111
runs-on: ${{ matrix.os }}
1212
timeout-minutes: 30

pnpm-lock.yaml

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/configurationType.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,19 @@ export type Configuration = {
334334
* @default true
335335
*/
336336
miscDefinitionImprovement: boolean
337+
// todo change setting format to: vue.*
337338
/**
338339
* Removes definiion suggestion from vue `components` options.
339340
* Might be useful with [Vetur-extended goToDefinition](https://github.com/zardoy/vetur-extended/blob/main/src/gotoDefinition.ts) for components as a replacement for (https://github.com/vuejs/language-tools/issues/1245)
340341
* @default false
341342
*/
342343
removeVueComponentsOptionDefinition: boolean
344+
/**
345+
* Use `filter-all` to also exclude auto-import completions (useful for Nuxt projects)
346+
* @recommended filter-non-vue
347+
* @default disable
348+
*/
349+
cleanupVueComponentCompletions: 'disable' | 'filter-non-vue' | 'filter-all'
343350
/**
344351
* Experimental, feedback welcome
345352
* If default, namespace import or import path click resolves to .d.ts file, try to resolve .js file instead with the same name

typescript/src/codeActions/functionExtractors.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { equals } from 'rambda'
12
import { GetConfig } from '../types'
23
import {
34
createDummySourceFile,
@@ -113,10 +114,13 @@ export const handleFunctionRefactorEdits = (
113114
...textChanges.slice(0, -2),
114115
{
115116
...insertChange,
116-
newText: `<${componentName} ${args
117-
.split(', ')
118-
.map(identifierText => `${identifierText}={${identifierText}}`)
119-
.join(' ')} />`,
117+
newText: `<${componentName}${
118+
args &&
119+
` ${args
120+
.split(', ')
121+
.map(identifierText => `${identifierText}={${identifierText}}`)
122+
.join(' ')}`
123+
} />`,
120124
},
121125
{
122126
span: functionChange.span,

typescript/src/completionsAtPosition.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,9 @@ export const getCompletionsAtPosition = (
254254
prior.entries = arrayMethods(prior.entries, position, sourceFile, c) ?? prior.entries
255255
prior.entries = jsdocDefault(prior.entries, position, sourceFile, languageService) ?? prior.entries
256256

257+
const isVueFileName = (fileName: string | undefined) => fileName && (fileName.endsWith('.vue.ts') || fileName.endsWith('.vue.js'))
257258
// #region Vue (Volar) specific
258-
const isVueFile = fileName.endsWith('.vue.ts') || fileName.endsWith('.vue.js')
259+
const isVueFile = isVueFileName(fileName)
259260
if (isVueFile && exactNode) {
260261
let node = ts.isIdentifier(exactNode) ? exactNode.parent : exactNode
261262
if (ts.isPropertyAssignment(node)) node = node.parent
@@ -267,6 +268,17 @@ export const getCompletionsAtPosition = (
267268
) {
268269
prior.entries = prior.entries.filter(({ name, kind }) => kind === ts.ScriptElementKind.warning || !name.startsWith('__'))
269270
}
271+
// const afterComponentsMarker = sourceFile.getFullText().lastIndexOf('/* Components */') < position
272+
const { line: curLine } = ts.getLineAndCharacterOfPosition(sourceFile, position)
273+
const lines = sourceFile.getFullText().split('\n')
274+
if (ts.isArrayLiteralExpression(node) && lines[curLine - 1] === '// @ts-ignore' && lines[curLine - 2]?.startsWith('__VLS_components')) {
275+
if (c('cleanupVueComponentCompletions') === 'filter-all') {
276+
prior.entries = []
277+
}
278+
if (c('cleanupVueComponentCompletions') === 'filter-non-vue') {
279+
prior.entries = prior.entries.filter(entry => isVueFileName(entry.symbol?.declarations?.[0]?.getSourceFile().fileName))
280+
}
281+
}
270282
}
271283
// #endregion
272284

@@ -351,7 +363,7 @@ export const getCompletionsAtPosition = (
351363
})
352364
}
353365

354-
if (prior.isGlobalCompletion) {
366+
if (!prior.isMemberCompletion) {
355367
prior.entries = markOrRemoveGlobalCompletions(prior.entries, position, languageService, c) ?? prior.entries
356368
}
357369
if (exactNode) {

typescript/src/definitions.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,23 @@ import { findChildContainingExactPosition } from './utils'
55
export default (proxy: ts.LanguageService, languageService: ts.LanguageService, languageServiceHost: ts.LanguageServiceHost, c: GetConfig) => {
66
proxy.getDefinitionAndBoundSpan = (fileName, position) => {
77
const prior = languageService.getDefinitionAndBoundSpan(fileName, position)
8-
if (!prior) {
8+
9+
if (c('removeModuleFileDefinitions') && prior) {
10+
prior.definitions = prior.definitions?.filter(def => {
11+
if (
12+
def.kind === ts.ScriptElementKind.moduleElement &&
13+
def.name.slice(1, -1).startsWith('*.') &&
14+
def.containerKind === undefined &&
15+
(def as import('typescript-full').DefinitionInfo).isAmbient
16+
) {
17+
return false
18+
}
19+
return true
20+
})
21+
}
22+
23+
// Definition fallbacks
24+
if (!prior || prior.definitions?.length === 0) {
925
const program = languageService.getProgram()!
1026
const sourceFile = program.getSourceFile(fileName)!
1127
const node = findChildContainingExactPosition(sourceFile, position)
@@ -140,22 +156,22 @@ export default (proxy: ts.LanguageService, languageService: ts.LanguageService,
140156
return true
141157
})
142158
}
159+
143160
if (c('removeVueComponentsOptionDefinition') && prior.definitions) {
144-
prior.definitions = prior.definitions.filter(definition => definition.containerName !== '__VLS_componentsOption')
145-
}
161+
const program = languageService.getProgram()!
162+
const sourceFile = program.getSourceFile(fileName)!
146163

147-
if (c('removeModuleFileDefinitions')) {
148-
prior.definitions = prior.definitions?.filter(def => {
149-
if (
150-
def.kind === ts.ScriptElementKind.moduleElement &&
151-
def.name.slice(1, -1).startsWith('*.') &&
152-
def.containerKind === undefined &&
153-
def['isAmbient']
154-
) {
155-
return false
156-
}
157-
return true
158-
})
164+
const lines = sourceFile.getFullText().split('\n')
165+
const { line: curLine } = ts.getLineAndCharacterOfPosition(sourceFile, position)
166+
167+
const VLS_COMPONENT_STRING = `__VLS_templateComponents`
168+
const isTemplateComponent = lines[curLine]?.startsWith(VLS_COMPONENT_STRING)
169+
if (!isTemplateComponent) return
170+
171+
const componentName = lines[curLine]?.match(/\.(\w+);?/)?.[1]
172+
if (!componentName) return
173+
174+
prior.definitions = prior.definitions.filter(({ name }) => !(componentName === name && lines[curLine - 2] === '// @ts-ignore'))
159175
}
160176

161177
return prior

0 commit comments

Comments
 (0)