Skip to content

Commit

Permalink
Merge pull request #130 from zardoy/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy authored May 31, 2023
2 parents 247b38b + 3c40998 commit 83cd6d2
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
test:
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
os: [ubuntu-latest]
fail-fast: true
runs-on: ${{ matrix.os }}
timeout-minutes: 30
Expand Down
6 changes: 5 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/configurationType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,19 @@ export type Configuration = {
* @default true
*/
miscDefinitionImprovement: boolean
// todo change setting format to: vue.*
/**
* Removes definiion suggestion from vue `components` options.
* 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)
* @default false
*/
removeVueComponentsOptionDefinition: boolean
/**
* Use `filter-all` to also exclude auto-import completions (useful for Nuxt projects)
* @recommended filter-non-vue
* @default disable
*/
cleanupVueComponentCompletions: 'disable' | 'filter-non-vue' | 'filter-all'
/**
* Experimental, feedback welcome
* If default, namespace import or import path click resolves to .d.ts file, try to resolve .js file instead with the same name
Expand Down
12 changes: 8 additions & 4 deletions typescript/src/codeActions/functionExtractors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { equals } from 'rambda'
import { GetConfig } from '../types'
import {
createDummySourceFile,
Expand Down Expand Up @@ -113,10 +114,13 @@ export const handleFunctionRefactorEdits = (
...textChanges.slice(0, -2),
{
...insertChange,
newText: `<${componentName} ${args
.split(', ')
.map(identifierText => `${identifierText}={${identifierText}}`)
.join(' ')} />`,
newText: `<${componentName}${
args &&
` ${args
.split(', ')
.map(identifierText => `${identifierText}={${identifierText}}`)
.join(' ')}`
} />`,
},
{
span: functionChange.span,
Expand Down
16 changes: 14 additions & 2 deletions typescript/src/completionsAtPosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,9 @@ export const getCompletionsAtPosition = (
prior.entries = arrayMethods(prior.entries, position, sourceFile, c) ?? prior.entries
prior.entries = jsdocDefault(prior.entries, position, sourceFile, languageService) ?? prior.entries

const isVueFileName = (fileName: string | undefined) => fileName && (fileName.endsWith('.vue.ts') || fileName.endsWith('.vue.js'))
// #region Vue (Volar) specific
const isVueFile = fileName.endsWith('.vue.ts') || fileName.endsWith('.vue.js')
const isVueFile = isVueFileName(fileName)
if (isVueFile && exactNode) {
let node = ts.isIdentifier(exactNode) ? exactNode.parent : exactNode
if (ts.isPropertyAssignment(node)) node = node.parent
Expand All @@ -267,6 +268,17 @@ export const getCompletionsAtPosition = (
) {
prior.entries = prior.entries.filter(({ name, kind }) => kind === ts.ScriptElementKind.warning || !name.startsWith('__'))
}
// const afterComponentsMarker = sourceFile.getFullText().lastIndexOf('/* Components */') < position
const { line: curLine } = ts.getLineAndCharacterOfPosition(sourceFile, position)
const lines = sourceFile.getFullText().split('\n')
if (ts.isArrayLiteralExpression(node) && lines[curLine - 1] === '// @ts-ignore' && lines[curLine - 2]?.startsWith('__VLS_components')) {
if (c('cleanupVueComponentCompletions') === 'filter-all') {
prior.entries = []
}
if (c('cleanupVueComponentCompletions') === 'filter-non-vue') {
prior.entries = prior.entries.filter(entry => isVueFileName(entry.symbol?.declarations?.[0]?.getSourceFile().fileName))
}
}
}
// #endregion

Expand Down Expand Up @@ -351,7 +363,7 @@ export const getCompletionsAtPosition = (
})
}

if (prior.isGlobalCompletion) {
if (!prior.isMemberCompletion) {
prior.entries = markOrRemoveGlobalCompletions(prior.entries, position, languageService, c) ?? prior.entries
}
if (exactNode) {
Expand Down
46 changes: 31 additions & 15 deletions typescript/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,23 @@ import { findChildContainingExactPosition } from './utils'
export default (proxy: ts.LanguageService, languageService: ts.LanguageService, languageServiceHost: ts.LanguageServiceHost, c: GetConfig) => {
proxy.getDefinitionAndBoundSpan = (fileName, position) => {
const prior = languageService.getDefinitionAndBoundSpan(fileName, position)
if (!prior) {

if (c('removeModuleFileDefinitions') && prior) {
prior.definitions = prior.definitions?.filter(def => {
if (
def.kind === ts.ScriptElementKind.moduleElement &&
def.name.slice(1, -1).startsWith('*.') &&
def.containerKind === undefined &&
(def as import('typescript-full').DefinitionInfo).isAmbient
) {
return false
}
return true
})
}

// Definition fallbacks
if (!prior || prior.definitions?.length === 0) {
const program = languageService.getProgram()!
const sourceFile = program.getSourceFile(fileName)!
const node = findChildContainingExactPosition(sourceFile, position)
Expand Down Expand Up @@ -140,22 +156,22 @@ export default (proxy: ts.LanguageService, languageService: ts.LanguageService,
return true
})
}

if (c('removeVueComponentsOptionDefinition') && prior.definitions) {
prior.definitions = prior.definitions.filter(definition => definition.containerName !== '__VLS_componentsOption')
}
const program = languageService.getProgram()!
const sourceFile = program.getSourceFile(fileName)!

if (c('removeModuleFileDefinitions')) {
prior.definitions = prior.definitions?.filter(def => {
if (
def.kind === ts.ScriptElementKind.moduleElement &&
def.name.slice(1, -1).startsWith('*.') &&
def.containerKind === undefined &&
def['isAmbient']
) {
return false
}
return true
})
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 componentName = lines[curLine]?.match(/\.(\w+);?/)?.[1]
if (!componentName) return

prior.definitions = prior.definitions.filter(({ name }) => !(componentName === name && lines[curLine - 2] === '// @ts-ignore'))
}

return prior
Expand Down

0 comments on commit 83cd6d2

Please sign in to comment.