Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 10 additions & 1 deletion src/harness/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,16 @@ namespace FourSlash {
assert.equal(actual.hasAction, hasAction);
assert.equal(actual.isRecommended, isRecommended);
assert.equal(actual.source, source);
assert.equal(actual.sortText, sortText || ts.Completions.SortText.LocationPriority, this.messageAtLastKnownMarker(`Actual entry: ${JSON.stringify(actual)}`));

let isSortTextEqual: boolean;
if (!sortText) {
Comment thread
fuafa marked this conversation as resolved.
Outdated
isSortTextEqual = [ts.Completions.SortText.LocationPriorityFulfilled, ts.Completions.SortText.LocationPriority].indexOf(actual.sortText as ts.Completions.SortText) !== -1
|| (actual.kindModifiers === "optional" && actual.sortText === ts.Completions.SortText.LocationPriorityOptional);
}
else {
isSortTextEqual = actual.sortText === sortText;
}
assert.equal(isSortTextEqual, true, this.messageAtLastKnownMarker(`Actual entry: ${JSON.stringify(actual)}`));

if (text !== undefined) {
const actualDetails = this.getCompletionEntryDetails(actual.name, actual.source)!;
Expand Down
48 changes: 40 additions & 8 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
namespace ts.Completions {
export enum SortText {
LocationPriority = "0",
SuggestedClassMembers = "1",
GlobalsOrKeywords = "2",
AutoImportSuggestions = "3",
JavascriptIdentifiers = "4"
LocationPriorityOptional = "1",
Comment thread
fuafa marked this conversation as resolved.
Outdated
LocationPriorityFulfilled = "2",
SuggestedClassMembers = "3",
GlobalsOrKeywords = "4",
AutoImportSuggestions = "5",
JavascriptIdentifiers = "6"
}
export type Log = (message: string) => void;

Expand Down Expand Up @@ -103,6 +105,7 @@ namespace ts.Completions {
isJsxInitializer,
insideJsDocTagTypeExpression,
symbolToSortTextMap,
fulfilledSymbols,
} = completionData;

if (location && location.parent && isJsxClosingElement(location.parent)) {
Expand Down Expand Up @@ -163,7 +166,8 @@ namespace ts.Completions {
isJsxInitializer,
recommendedCompletion,
symbolToOriginInfoMap,
symbolToSortTextMap
symbolToSortTextMap,
fulfilledSymbols
);
}

Expand Down Expand Up @@ -317,6 +321,7 @@ namespace ts.Completions {
recommendedCompletion?: Symbol,
symbolToOriginInfoMap?: SymbolOriginInfoMap,
symbolToSortTextMap?: SymbolSortTextMap,
fulfilledSymbols?: ReadonlyArray<Symbol>,
): Map<true> {
const start = timestamp();
// Tracks unique names.
Expand All @@ -335,9 +340,23 @@ namespace ts.Completions {
continue;
}

let sortText = symbolToSortTextMap && symbolToSortTextMap[getSymbolId(symbol)];
Comment thread
fuafa marked this conversation as resolved.
Outdated
if (!sortText) {
if (fulfilledSymbols && fulfilledSymbols.length > 0) {
fulfilledSymbols.forEach(fulfilledSymbol => {
if (fulfilledSymbol.name === symbol.name) {
sortText = SortText.LocationPriorityFulfilled;
}
});
}
}
if (!sortText) {
sortText = SymbolDisplay.getSymbolModifiers(symbol) === "optional" ? SortText.LocationPriorityOptional : SortText.LocationPriority;
}

const entry = createCompletionEntry(
symbol,
symbolToSortTextMap && symbolToSortTextMap[getSymbolId(symbol)] || SortText.LocationPriority,
sortText,
location,
sourceFile,
typeChecker,
Expand All @@ -347,7 +366,7 @@ namespace ts.Completions {
recommendedCompletion,
propertyAccessToConvert,
isJsxInitializer,
preferences
preferences,
);
if (!entry) {
continue;
Expand Down Expand Up @@ -581,6 +600,7 @@ namespace ts.Completions {
readonly isJsxInitializer: IsJsxInitializer;
readonly insideJsDocTagTypeExpression: boolean;
readonly symbolToSortTextMap: SymbolSortTextMap;
readonly fulfilledSymbols?: ReadonlyArray<Symbol>;
}
type Request = { readonly kind: CompletionDataKind.JsDocTagName | CompletionDataKind.JsDocTag } | { readonly kind: CompletionDataKind.JsDocParameterName, tag: JSDocParameterTag };

Expand Down Expand Up @@ -872,6 +892,7 @@ namespace ts.Completions {
let isNewIdentifierLocation = false;
let keywordFilters = KeywordCompletionFilters.None;
let symbols: Symbol[] = [];
let fulfilledSymbols: Symbol[] | undefined = [];
const symbolToOriginInfoMap: SymbolOriginInfoMap = [];
const symbolToSortTextMap: SymbolSortTextMap = [];

Expand Down Expand Up @@ -924,7 +945,8 @@ namespace ts.Completions {
previousToken,
isJsxInitializer,
insideJsDocTagTypeExpression,
symbolToSortTextMap
symbolToSortTextMap,
fulfilledSymbols
};

type JSDocTagWithTypeExpression = JSDocParameterTag | JSDocPropertyTag | JSDocReturnTag | JSDocTypeTag | JSDocTypedefTag;
Expand Down Expand Up @@ -1494,6 +1516,16 @@ namespace ts.Completions {
if (typeMembers && typeMembers.length > 0) {
// Add filtered items to the completion list
symbols = filterObjectMembersList(typeMembers, Debug.assertDefined(existingMembers));
if (existingMembers && existingMembers.length > 0) {
Comment thread
fuafa marked this conversation as resolved.
Outdated
existingMembers.forEach(member => {
if (member.kind === SyntaxKind.SpreadAssignment) {
const expression = (<SpreadAssignment>member).expression;
const symbol = typeChecker.getSymbolAtLocation(expression);
const type = symbol && typeChecker.getTypeOfSymbolAtLocation(symbol, expression);
fulfilledSymbols = type && (<ObjectType>type).properties;
}
});
}
}
return GlobalsSearch.Success;
}
Expand Down
29 changes: 29 additions & 0 deletions tests/cases/fourslash/completionsPropertiesPriorities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// <reference path="fourslash.ts" />
// @strict: true

//// interface I {
//// B?: number;
//// a: number;
//// c?: string;
//// d: string
//// }

//// const foo = {
//// a: 1,
//// B: 2
//// }

//// const i: I = {
//// ...foo,
//// /*a*/
//// }

verify.completions({
marker: ['a'],
exact: [
{ name: 'B', kindModifiers: 'optional', sortText: completion.SortText.LocationPriorityFulfilled, kind: 'property' },
{ name: 'a', sortText: completion.SortText.LocationPriorityFulfilled, kind: 'property' },
{ name: 'c', kindModifiers: 'optional', sortText: completion.SortText.LocationPriorityOptional, kind: 'property' },
{ name: 'd', sortText: completion.SortText.LocationPriority, kind: 'property' }
]
});
10 changes: 6 additions & 4 deletions tests/cases/fourslash/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,10 +672,12 @@ declare namespace completion {
type Entry = FourSlashInterface.ExpectedCompletionEntryObject;
export const enum SortText {
LocationPriority = "0",
SuggestedClassMembers = "1",
GlobalsOrKeywords = "2",
AutoImportSuggestions = "3",
JavascriptIdentifiers = "4"
LocationPriorityOptional = "1",
LocationPriorityFulfilled = "2",
SuggestedClassMembers = "3",
GlobalsOrKeywords = "4",
AutoImportSuggestions = "5",
JavascriptIdentifiers = "6"
}
export const globalThisEntry: Entry;
export const undefinedVarEntry: Entry;
Expand Down