diff --git a/extensions/typescript-language-features/src/features/completions.ts b/extensions/typescript-language-features/src/features/completions.ts index becb983ab7b..05102d4177c 100644 --- a/extensions/typescript-language-features/src/features/completions.ts +++ b/extensions/typescript-language-features/src/features/completions.ts @@ -125,11 +125,17 @@ class MyCompletionItem extends vscode.CompletionItem { // Try getting longer, prefix based range for completions that span words const wordRange = this.document.getWordRangeAtPosition(this.position); + if (wordRange) { + this.range = wordRange; + } + const text = line.slice(Math.max(0, this.position.character - this.label.length), this.position.character).toLowerCase(); const entryName = this.label.toLowerCase(); for (let i = entryName.length; i >= 0; --i) { if (text.endsWith(entryName.substr(0, i)) && (!wordRange || wordRange.start.character > this.position.character - i)) { - this.range = new vscode.Range(this.position.line, Math.max(0, this.position.character - i), this.position.line, this.position.character); + this.range = new vscode.Range( + new vscode.Position(this.position.line, Math.max(0, this.position.character - i)), + this.position); break; } } diff --git a/extensions/typescript-language-features/src/test/completions.test.ts b/extensions/typescript-language-features/src/test/completions.test.ts index c189c5e4494..05a251b0d69 100644 --- a/extensions/typescript-language-features/src/test/completions.test.ts +++ b/extensions/typescript-language-features/src/test/completions.test.ts @@ -162,6 +162,20 @@ suite('TypeScript Completions', () => { )); }); + test('Accepting a member completion should result in valid code. #58597', async () => { + await createTestEditor(testDocumentUri, + `const abc = 123;`, + `ab$0c` + ); + + const document = await acceptFirstSuggestion(testDocumentUri, _disposables); + assert.strictEqual( + document.getText(), + joinLines( + `const abc = 123;`, + `abc` + )); + }); }); const joinLines = (...args: string[]) => args.join('\n');