Skip to content

Commit

Permalink
commit uncommited
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy committed Sep 12, 2022
1 parent 3a4837c commit 8642b10
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"javascript",
"plugin",
"webstorm",
"TypeScript Hero"
"typescript hero"
],
"activationEvents": [
"onLanguage:javascript",
Expand Down
2 changes: 1 addition & 1 deletion typescript/src/completionsAtPosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export const getCompletionsAtPosition = (

// prevent vscode-builtin wrong insertText with methods snippets enabled
if (!isGoodPositionBuiltinMethodCompletion(ts, sourceFile, position)) {
prior.entries = prior.entries.map(item => ({ ...item, insertText: item.insertText ?? item.name, isSnippet: true }))
prior.entries = prior.entries.map(item => ({ ...item, insertText: (item.insertText ?? item.name).replace(/\$/g, '\\$'), isSnippet: true }))
}

if (c('correctSorting.enable')) prior.entries = prior.entries.map((entry, index) => ({ ...entry, sortText: `${entry.sortText ?? ''}${index}` }))
Expand Down
6 changes: 6 additions & 0 deletions typescript/src/dummyLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ export const createLanguageService = (files: Record<string, string>) => {
},
getCurrentDirectory: () => '',
getDefaultLibFileName: () => require.resolve('typescript/lib/lib.esnext.full.d.ts'),
fileExists(path) {
return path in files
},
readFile(path) {
return files[path]!
},
})
return {
languageService,
Expand Down
18 changes: 11 additions & 7 deletions typescript/src/isGoodPositionMethodCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import type tslib from 'typescript/lib/tsserverlibrary'
import { findChildContainingPosition, findChildContainingPositionMaxDepth } from './utils'

export const isGoodPositionBuiltinMethodCompletion = (ts: typeof tslib, sourceFile: tslib.SourceFile, position: number) => {
const importClauseCandidate = findChildContainingPositionMaxDepth(ts, sourceFile, position, 2)
console.log(sourceFile.getFullText().slice(0, position))
if (importClauseCandidate?.kind === 266) return false
const currentNode = findChildContainingPosition(ts, sourceFile, position)
// const obj = { method() {}, arrow: () => {} }
// type A = typeof obj["|"]
if (currentNode && ts.isStringLiteralLike(currentNode)) return false
const importClauseCandidate = findChildContainingPositionMaxDepth(ts, sourceFile, position, 3)
if (importClauseCandidate && ts.isImportClause(importClauseCandidate)) return false
let currentNode = findChildContainingPosition(ts, sourceFile, position)
if (currentNode) {
// const obj = { method() {}, arrow: () => {} }
// type A = typeof obj["|"]
if (ts.isStringLiteralLike(currentNode)) return false
if (ts.isIdentifier(currentNode)) currentNode = currentNode.parent
if (ts.isShorthandPropertyAssignment(currentNode)) currentNode = currentNode.parent
if (ts.isObjectBindingPattern(currentNode) || ts.isObjectLiteralExpression(currentNode)) return false
}
return true
}

Expand Down

0 comments on commit 8642b10

Please sign in to comment.