Skip to content

Commit f96835e

Browse files
committed
feat: include fix for builtin typescript.suggest.completeFunctionCalls in some positions out of the box
fix: don't insert method snippet in some positions, inclusing const assigning
1 parent 8f3280f commit f96835e

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

typescript/src/completionsAtPosition.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as emmet from '@vscode/emmet-helper'
44
import isInBannedPosition from './isInBannedPosition'
55
import { GetConfig } from './types'
66
import { findChildContainingPosition } from './utils'
7+
import { isGoodPositionBuiltinMethodCompletion } from './isGootPositionMethodCompletion'
78

89
export type PrevCompletionMap = Record<string, { originalName?: string; documentationOverride?: string | tslib.SymbolDisplayPart[] }>
910

@@ -225,6 +226,11 @@ export const getCompletionsAtPosition = (
225226
if (rule.patch?.insertText) suggestion.isSnippet = true
226227
}
227228

229+
// prevent vscode-builtin wrong insertText with methods snippets enabled
230+
if (!isGoodPositionBuiltinMethodCompletion(ts, sourceFile, position)) {
231+
prior.entries = prior.entries.map(item => ({ ...item, insertText: item.insertText ?? item.name, isSnippet: true }))
232+
}
233+
228234
if (c('correctSorting.enable')) prior.entries = prior.entries.map((entry, index) => ({ ...entry, sortText: `${entry.sortText ?? ''}${index}` }))
229235

230236
// console.log('signatureHelp', JSON.stringify(info.languageService.getSignatureHelpItems(fileName, position, {})))

typescript/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import type { Configuration } from '../../src/configurationType'
66
import _ from 'lodash'
77
import { GetConfig } from './types'
88
import { getCompletionsAtPosition, PrevCompletionMap } from './completionsAtPosition'
9-
import { CompletionEntry } from 'typescript/lib/tsserverlibrary'
109
import { getParameterListParts } from './completionGetMethodParameters'
1110
import { oneOf } from '@zardoy/utils'
11+
import { isGoodPositionMethodCompletion } from './isGootPositionMethodCompletion'
1212

1313
const thisPluginMarker = Symbol('__essentialPluginsMarker__')
1414

@@ -87,8 +87,9 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
8787
)
8888
if (!prior) return
8989
if (c('enableMethodSnippets') && oneOf(prior.kind as string, ts.ScriptElementKind.constElement, 'property')) {
90+
const goodPosition = isGoodPositionMethodCompletion(ts, fileName, sourceFile, position, info.languageService)
9091
const punctuationIndex = prior.displayParts.findIndex(({ kind }) => kind === 'punctuation')
91-
if (punctuationIndex !== 1) {
92+
if (goodPosition && punctuationIndex !== 1) {
9293
const isParsableMethod = prior.displayParts
9394
// next is space
9495
.slice(punctuationIndex + 2)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type tslib from 'typescript/lib/tsserverlibrary'
2+
import { findChildContainingPosition } from './utils'
3+
4+
export const isGoodPositionBuiltinMethodCompletion = (ts: typeof tslib, sourceFile: tslib.SourceFile, position: number) => {
5+
const importClauseCandidate = findChildContainingPosition(ts, sourceFile, position, 2)
6+
if (importClauseCandidate?.kind === 266) return false
7+
const currentNode = findChildContainingPosition(ts, sourceFile, position)
8+
// const obj = { method() {}, arrow: () => {} }
9+
// type A = typeof obj["|"]
10+
if (currentNode && ts.isStringLiteralLike(currentNode)) return false
11+
return true
12+
}
13+
14+
export const isGoodPositionMethodCompletion = (
15+
ts: typeof tslib,
16+
fileName: string,
17+
sourceFile: tslib.SourceFile,
18+
position: number,
19+
languageService: tslib.LanguageService,
20+
) => {
21+
if (!isGoodPositionBuiltinMethodCompletion(ts, sourceFile, position)) return false
22+
const { kind } = languageService.getQuickInfoAtPosition(fileName, position) ?? {}
23+
switch (kind) {
24+
case 'var':
25+
case 'let':
26+
case 'const':
27+
case 'alias':
28+
return false
29+
}
30+
// TODO check for brace here
31+
return true
32+
}

typescript/src/utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ export function findChildContainingPosition(
44
typescript: typeof import('typescript/lib/tsserverlibrary'),
55
sourceFile: tslib.SourceFile,
66
position: number,
7+
maxDepth?: number,
78
): tslib.Node | undefined {
9+
let currentDepth = 0
810
function find(node: ts.Node): ts.Node | undefined {
9-
if (position >= node.getStart() && position < node.getEnd()) return typescript.forEachChild(node, find) || node
11+
if (position >= node.getStart() && position < node.getEnd()) {
12+
if (++currentDepth === maxDepth) return node
13+
return typescript.forEachChild(node, find) || node
14+
}
1015

1116
return
1217
}

0 commit comments

Comments
 (0)