Skip to content

Commit 4aeb295

Browse files
author
Andy
authored
completions: Properly handle preferences.includeCompletionsWithInsertText (microsoft#23092)
1 parent 1e227c6 commit 4aeb295

2 files changed

Lines changed: 26 additions & 24 deletions

File tree

src/services/completions.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -202,26 +202,24 @@ namespace ts.Completions {
202202

203203
let insertText: string | undefined;
204204
let replacementSpan: TextSpan | undefined;
205-
if (preferences.includeCompletionsWithInsertText) {
206-
if (origin && origin.type === "this-type") {
207-
insertText = needsConvertPropertyAccess ? `this[${quote(name, preferences)}]` : `this.${name}`;
208-
}
209-
// We should only have needsConvertPropertyAccess if there's a property access to convert. But see #21790.
210-
// Somehow there was a global with a non-identifier name. Hopefully someone will complain about getting a "foo bar" global completion and provide a repro.
211-
else if ((origin && origin.type === "symbol-member" || needsConvertPropertyAccess) && propertyAccessToConvert) {
212-
insertText = needsConvertPropertyAccess ? `[${quote(name, preferences)}]` : `[${name}]`;
213-
const dot = findChildOfKind(propertyAccessToConvert!, SyntaxKind.DotToken, sourceFile)!;
214-
// If the text after the '.' starts with this name, write over it. Else, add new text.
215-
const end = startsWith(name, propertyAccessToConvert.name.text) ? propertyAccessToConvert.name.end : dot.end;
216-
replacementSpan = createTextSpanFromBounds(dot.getStart(sourceFile), end);
217-
}
218-
219-
if (isJsxInitializer) {
220-
if (insertText === undefined) insertText = name;
221-
insertText = `{${insertText}}`;
222-
if (typeof isJsxInitializer !== "boolean") {
223-
replacementSpan = createTextSpanFromNode(isJsxInitializer, sourceFile);
224-
}
205+
if (origin && origin.type === "this-type") {
206+
insertText = needsConvertPropertyAccess ? `this[${quote(name, preferences)}]` : `this.${name}`;
207+
}
208+
// We should only have needsConvertPropertyAccess if there's a property access to convert. But see #21790.
209+
// Somehow there was a global with a non-identifier name. Hopefully someone will complain about getting a "foo bar" global completion and provide a repro.
210+
else if ((origin && origin.type === "symbol-member" || needsConvertPropertyAccess) && propertyAccessToConvert) {
211+
insertText = needsConvertPropertyAccess ? `[${quote(name, preferences)}]` : `[${name}]`;
212+
const dot = findChildOfKind(propertyAccessToConvert!, SyntaxKind.DotToken, sourceFile)!;
213+
// If the text after the '.' starts with this name, write over it. Else, add new text.
214+
const end = startsWith(name, propertyAccessToConvert.name.text) ? propertyAccessToConvert.name.end : dot.end;
215+
replacementSpan = createTextSpanFromBounds(dot.getStart(sourceFile), end);
216+
}
217+
218+
if (isJsxInitializer) {
219+
if (insertText === undefined) insertText = name;
220+
insertText = `{${insertText}}`;
221+
if (typeof isJsxInitializer !== "boolean") {
222+
replacementSpan = createTextSpanFromNode(isJsxInitializer, sourceFile);
225223
}
226224
}
227225

tests/cases/fourslash/completionsRecommended_contextualTypes.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@
1616
////<MainButton e=/*jsx2*/ />
1717

1818
recommended("arg0");
19-
recommended("arg1", "F");
19+
recommended("arg1", { enumName: "F" });
2020
recommended("tag");
2121
recommended("jsx");
22-
recommended("jsx2");
22+
recommended("jsx2", { insertText: "{E}" });
2323

24-
function recommended(markerName: string, enumName = "E") {
24+
function recommended(markerName: string, { insertText, enumName = "E" }: { insertText?: string, enumName?: string } = {}) {
2525
goTo.marker(markerName);
26-
verify.completionListContains(enumName, `enum ${enumName}`, "", "enum", undefined, undefined , { isRecommended: true });
26+
verify.completionListContains(enumName, `enum ${enumName}`, "", "enum", undefined, undefined , {
27+
isRecommended: true,
28+
includeInsertTextCompletions: true,
29+
insertText,
30+
});
2731
}

0 commit comments

Comments
 (0)