diff --git a/server/src/modes/script/javascript.ts b/server/src/modes/script/javascript.ts index 61403ab1b6..69462dc23d 100644 --- a/server/src/modes/script/javascript.ts +++ b/server/src/modes/script/javascript.ts @@ -165,9 +165,11 @@ export async function getJavascriptMode( position, label, detail, + filterText: getFilterText(entry.insertText), sortText: entry.sortText + index, kind: toCompletionItemKind(entry.kind), textEdit: range && TextEdit.replace(range, entry.name), + insertText: entry.insertText, data: { // data used for resolving item details (see 'doResolve') languageId: scriptDoc.languageId, @@ -723,3 +725,28 @@ function convertTSDiagnosticCategoryToDiagnosticSeverity(c: ts.DiagnosticCategor return DiagnosticSeverity.Hint; } } + +/* tslint:disable:max-line-length */ +/** + * Adapted from https://github.com/microsoft/vscode/blob/2b090abd0fdab7b21a3eb74be13993ad61897f84/extensions/typescript-language-features/src/languageFeatures/completions.ts#L147-L181 + */ +function getFilterText(insertText: string | undefined): string | undefined { + // For `this.` completions, generally don't set the filter text since we don't want them to be overly prioritized. #74164 + if (insertText?.startsWith('this.')) { + return undefined; + } + + // Handle the case: + // ``` + // const xyz = { 'ab c': 1 }; + // xyz.ab| + // ``` + // In which case we want to insert a bracket accessor but should use `.abc` as the filter text instead of + // the bracketed insert text. + else if (insertText?.startsWith('[')) { + return insertText.replace(/^\[['"](.+)[['"]\]$/, '.$1'); + } + + // In all other cases, fallback to using the insertText + return insertText; +} diff --git a/test/lsp/features/completion/script.test.ts b/test/lsp/features/completion/script.test.ts index 9b5d4dc633..cae8b4d817 100644 --- a/test/lsp/features/completion/script.test.ts +++ b/test/lsp/features/completion/script.test.ts @@ -1,28 +1,32 @@ -import { activateLS, showFile } from '../../helper'; +import { activateLS } from '../../helper'; import { position, getDocUri } from '../../util'; import { testCompletion } from './helper'; describe('Should autocomplete for \ No newline at end of file