Skip to content

Commit 7c6c447

Browse files
committed
fix(methodSnippetsInsertText): don't add method snippet completion is already snippet (in other words it can be explicitly disabled either by us or by you with replaceSuggestions)
1 parent 0de80d2 commit 7c6c447

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

typescript/src/completions/functionCompletions.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@ export default (entries: ts.CompletionEntry[]) => {
4141
if (!valueDeclaration) return
4242

4343
// const dateNow = Date.now()
44-
if (enableResolvingInsertText) {
44+
if (enableResolvingInsertText && !entry.isSnippet) {
4545
const resolveData = {} as { isAmbiguous: boolean }
4646
const methodSnippet = constructMethodSnippet(languageService, sourceFile, position, symbol, c, resolveData)
4747
if (!methodSnippet || resolveData.isAmbiguous) return
48+
const originalText = entry.insertText ?? entry.name
49+
const insertTextSnippetAdd = `(${methodSnippet.map((x, i) => `$\{${i + 1}:${x}}`).join(', ')})`
4850
return {
4951
...entry,
50-
insertText: insertTextAfterEntry(entry.insertText ?? entry.name, `(${methodSnippet.map((x, i) => `$\{${i + 1}:${x}}`).join(', ')})`),
52+
insertText: insertTextAfterEntry(originalText, insertTextSnippetAdd),
5153
labelDetails: {
5254
detail: `(${methodSnippet.join(', ')})`,
5355
description: ts.displayPartsToString(entry.sourceDisplay),

typescript/test/completions.spec.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ const compareMethodSnippetAgainstMarker = (inputMarkers: number[], marker: numbe
124124
expect(Array.isArray(expected) ? methodSnippet : snippetToInsert, `At marker ${marker}`).toEqual(expected)
125125
}
126126

127-
const assertCompletionInsertText = (marker: number, entryName: string | undefined, insertTextExpected: string) => {
127+
const assertCompletionInsertText = (marker: number, entryPredicate: string | undefined | number, insertTextExpected: string) => {
128128
const { entries } = getCompletionsAtPosition(currentTestingContext.markers[marker]!)!
129-
const entry = entryName === undefined ? entries[0] : entries.find(({ name }) => name === entryName)
129+
const entry = typeof entryPredicate === 'string' ? entries.find(({ name }) => name === entryPredicate) : entries[entryPredicate ?? 0]
130130
expect(entry?.insertText).toEqual(insertTextExpected)
131131
}
132132

@@ -293,7 +293,7 @@ describe('Method snippets', () => {
293293
compareMethodSnippetAgainstMarker(markers, 2, 'ambiguous')
294294
})
295295

296-
test('methodSnippetsInsertText all', () => {
296+
test.only('methodSnippetsInsertText all', () => {
297297
overrideSettings({
298298
methodSnippetsInsertText: 'all',
299299
})
@@ -306,9 +306,14 @@ describe('Method snippets', () => {
306306
test/*2*/
307307
}
308308
}
309+
310+
const b: { a() } = {
311+
/*3*/
312+
}
309313
`)
310314
assertCompletionInsertText(1, 'a', 'a(${1:a}, ${2:b})')
311315
assertCompletionInsertText(2, 'test', 'this.test()')
316+
assertCompletionInsertText(3, 1, 'a() {\n$0\n},')
312317
})
313318
})
314319

@@ -602,7 +607,7 @@ test('Object Literal Completions', () => {
602607
/*1*/
603608
})
604609
605-
const somethingWithUntions: { a: string } | { a: any[], b: string } = {/*2*/}
610+
const somethingWithUnions: { a: string } | { a: any[], b: string } = {/*2*/}
606611
607612
makeDay({
608613
additionalOptions: {
@@ -673,7 +678,7 @@ test('Object Literal Completions', () => {
673678
"name": "callback",
674679
},
675680
{
676-
"insertText": "callback() {\\\\n $0\\\\n},",
681+
"insertText": "callback() {\\\\n$0\\\\n},",
677682
"isSnippet": true,
678683
"kind": "method",
679684
"kindModifiers": "optional",
@@ -738,7 +743,7 @@ test('Object Literal Completions with keepOriginal: remove & builtin method snip
738743
`)
739744
completion(1, {
740745
exact: {
741-
insertTexts: ['a: {\n\t$1\n},$0', 'onA() {\n $0\n},'],
746+
insertTexts: ['a: {\n\t$1\n},$0', 'onA() {\n$0\n},'],
742747
all: {
743748
isSnippet: true,
744749
},

typescript/test/testing.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ export const getCompletionsAtPosition = (pos: number, { fileName = entrypoint, s
4040
defaultConfigFunc,
4141
languageService,
4242
languageServiceHost.getScriptSnapshot(entrypoint)!,
43-
undefined,
43+
{
44+
convertTabsToSpaces: false,
45+
},
4446
{ scriptKind: ts.ScriptKind.TSX, compilerOptions: {} },
4547
)
4648
if (shouldHave) expect(result).not.toBeUndefined()

0 commit comments

Comments
 (0)