Accepting a member completion should result in valid code

Fixes #58597

Default to replacing the word range in js/ts. This is a change in core behavior so  we'll need to see what the feedback is like for it.
This commit is contained in:
Matt Bierner
2019-01-04 18:09:48 -08:00
parent 7a4944fb64
commit 9d2787e42e
2 changed files with 21 additions and 1 deletions

View File

@@ -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;
}
}

View File

@@ -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');