Skip to content

Adjust completion sortText by property assignment context #40079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/* @internal */
namespace ts.Completions {
export enum SortText {
LocationPriority = "0",
OptionalMember = "1",
MemberDeclaredBySpreadAssignment = "2",
SuggestedClassMembers = "3",
GlobalsOrKeywords = "4",
AutoImportSuggestions = "5",
JavascriptIdentifiers = "6"
SameName = "0",
LocationPriority = "1",
OptionalMember = "2",
MemberDeclaredBySpreadAssignment = "3",
SuggestedClassMembers = "4",
GlobalsOrKeywords = "5",
AutoImportSuggestions = "6",
JavascriptIdentifiers = "7"
}
export type Log = (message: string) => void;

Expand Down Expand Up @@ -945,6 +946,7 @@ namespace ts.Completions {
let isStartingCloseTag = false;
let isJsxInitializer: IsJsxInitializer = false;
let isJsxIdentifierExpected = false;
let contextPropertyName: string | undefined;

let location = getTouchingPropertyName(sourceFile, position);
if (contextToken) {
Expand All @@ -958,6 +960,8 @@ namespace ts.Completions {
if (contextToken.kind === SyntaxKind.DotToken || contextToken.kind === SyntaxKind.QuestionDotToken) {
isRightOfDot = contextToken.kind === SyntaxKind.DotToken;
isRightOfQuestionDot = contextToken.kind === SyntaxKind.QuestionDotToken;
const propertyAssignment = tryCast(findAncestor(contextToken, n => rangeContainsRange(location, n) && isPropertyAssignment(n)), isPropertyAssignment);
contextPropertyName = tryCast(propertyAssignment?.name, isIdentifier)?.escapedText as string | undefined;
switch (parent.kind) {
case SyntaxKind.PropertyAccessExpression:
propertyAccessToConvert = parent as PropertyAccessExpression;
Expand Down Expand Up @@ -1208,7 +1212,7 @@ namespace ts.Completions {
}
}
}
addTypeProperties(type, !!(node.flags & NodeFlags.AwaitContext), insertQuestionDot);
addTypeProperties(type, !!(node.flags & NodeFlags.AwaitContext), insertQuestionDot, contextPropertyName);
}

return;
Expand Down Expand Up @@ -1238,11 +1242,11 @@ namespace ts.Completions {
}
}
}
addTypeProperties(type, !!(node.flags & NodeFlags.AwaitContext), insertQuestionDot);
addTypeProperties(type, !!(node.flags & NodeFlags.AwaitContext), insertQuestionDot, contextPropertyName);
}
}

function addTypeProperties(type: Type, insertAwait: boolean, insertQuestionDot: boolean): void {
function addTypeProperties(type: Type, insertAwait: boolean, insertQuestionDot: boolean, contextPropertyName: string | undefined): void {
isNewIdentifierLocation = !!type.getStringIndexType();
if (isRightOfQuestionDot && some(type.getCallSignatures())) {
isNewIdentifierLocation = true;
Expand All @@ -1260,6 +1264,9 @@ namespace ts.Completions {
else {
for (const symbol of type.getApparentProperties()) {
if (typeChecker.isValidPropertyAccessForCompletions(propertyAccess, type, symbol)) {
if (contextPropertyName && symbol.name === contextPropertyName) {
symbolToSortTextMap[getSymbolId(symbol)] = SortText.SameName;
}
addPropertySymbol(symbol, /*insertAwait*/ false, insertQuestionDot);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/services/jsDoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ namespace ts.JsDoc {
name: tagName,
kind: ScriptElementKind.keyword,
kindModifiers: "",
sortText: "0",
sortText: Completions.SortText.LocationPriority,
};
}));
}
Expand All @@ -176,7 +176,7 @@ namespace ts.JsDoc {
name: `@${tagName}`,
kind: ScriptElementKind.keyword,
kindModifiers: "",
sortText: "0"
sortText: Completions.SortText.LocationPriority
};
}));
}
Expand Down Expand Up @@ -211,7 +211,7 @@ namespace ts.JsDoc {
return undefined;
}

return { name, kind: ScriptElementKind.parameterElement, kindModifiers: "", sortText: "0" };
return { name, kind: ScriptElementKind.parameterElement, kindModifiers: "", sortText: Completions.SortText.LocationPriority };
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/stringCompletions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace ts.Completions.StringCompletions {
name: type.value,
kindModifiers: ScriptElementKindModifier.none,
kind: ScriptElementKind.string,
sortText: "0",
sortText: SortText.LocationPriority,
replacementSpan: getReplacementSpanForContextToken(contextToken)
}));
return { isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: completion.isNewIdentifier, entries };
Expand Down
4 changes: 2 additions & 2 deletions src/testRunner/unittests/tsserver/metadataInResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ namespace ts.projectSystem {
offset: aTs.content.indexOf("this.") + 1 + "this.".length
};
const expectedCompletionEntries: readonly protocol.CompletionEntry[] = [
{ name: "foo", kind: ScriptElementKind.memberFunctionElement, kindModifiers: "", sortText: "0" },
{ name: "prop", kind: ScriptElementKind.memberVariableElement, kindModifiers: "", sortText: "0" }
{ name: "foo", kind: ScriptElementKind.memberFunctionElement, kindModifiers: "", sortText: Completions.SortText.LocationPriority },
{ name: "prop", kind: ScriptElementKind.memberVariableElement, kindModifiers: "", sortText: Completions.SortText.LocationPriority }
];

it("can pass through metadata when the command returns array", () => {
Expand Down
21 changes: 21 additions & 0 deletions tests/cases/fourslash/completionForAssignmentByAccess.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// <reference path='fourslash.ts'/>

//// interface T {
//// a: string
//// b: number
//// }
//// interface U {
//// b: number
//// }
//// declare const a: T
//// const b: U = {
//// b: a./*1*/
//// }

verify.completions({
marker: "1",
exact: [
{ name: "a", sortText: completion.SortText.LocationPriority },
{ name: "b", sortText: completion.SortText.SameName },
]
});
15 changes: 8 additions & 7 deletions tests/cases/fourslash/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -759,13 +759,14 @@ declare var classification: typeof FourSlashInterface.classification;
declare namespace completion {
type Entry = FourSlashInterface.ExpectedCompletionEntryObject;
export const enum SortText {
LocationPriority = "0",
OptionalMember = "1",
MemberDeclaredBySpreadAssignment = "2",
SuggestedClassMembers = "3",
GlobalsOrKeywords = "4",
AutoImportSuggestions = "5",
JavascriptIdentifiers = "6"
SameName = "0",
LocationPriority = "1",
OptionalMember = "2",
MemberDeclaredBySpreadAssignment = "3",
SuggestedClassMembers = "4",
GlobalsOrKeywords = "5",
AutoImportSuggestions = "6",
JavascriptIdentifiers = "7"
}
export const enum CompletionSource {
ThisProperty = "ThisProperty/"
Expand Down