Skip to content
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

Completions sorting overhaul #46703

Merged
merged 6 commits into from
Dec 3, 2021
Merged
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
9 changes: 8 additions & 1 deletion src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,11 @@ namespace ts {
return deduplicated as any as SortedReadonlyArray<T>;
}

export function insertSorted<T>(array: SortedArray<T>, insert: T, compare: Comparer<T>): void {
export function createSortedArray<T>(): SortedArray<T> {
return [] as any as SortedArray<T>; // TODO: GH#19873
}

export function insertSorted<T>(array: SortedArray<T>, insert: T, compare: Comparer<T>, allowDuplicates?: boolean): void {
if (array.length === 0) {
array.push(insert);
return;
Expand All @@ -808,6 +812,9 @@ namespace ts {
if (insertIndex < 0) {
array.splice(~insertIndex, 0, insert);
}
else if (allowDuplicates) {
array.splice(insertIndex, 0, insert);
}
}

export function sortAndDeduplicate<T>(array: readonly string[]): SortedReadonlyArray<string>;
Expand Down
37 changes: 34 additions & 3 deletions src/harness/fourslashImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -913,9 +913,26 @@ namespace FourSlash {
}

if (ts.hasProperty(options, "exact")) {
ts.Debug.assert(!ts.hasProperty(options, "includes") && !ts.hasProperty(options, "excludes"));
ts.Debug.assert(!ts.hasProperty(options, "includes") && !ts.hasProperty(options, "excludes") && !ts.hasProperty(options, "unsorted"));
if (options.exact === undefined) throw this.raiseError("Expected no completions");
this.verifyCompletionsAreExactly(actualCompletions.entries, toArray(options.exact), options.marker);
this.verifyCompletionsAreExactly(actualCompletions.entries, options.exact, options.marker);
}
else if (options.unsorted) {
ts.Debug.assert(!ts.hasProperty(options, "includes") && !ts.hasProperty(options, "excludes"));
for (const expectedEntry of options.unsorted) {
const name = typeof expectedEntry === "string" ? expectedEntry : expectedEntry.name;
const found = nameToEntries.get(name);
if (!found) throw this.raiseError(`Unsorted: completion '${name}' not found.`);
if (!found.length) throw this.raiseError(`Unsorted: no completions with name '${name}' remain unmatched.`);
this.verifyCompletionEntry(found.shift()!, expectedEntry);
}
if (actualCompletions.entries.length !== options.unsorted.length) {
const unmatched: string[] = [];
nameToEntries.forEach(entries => {
unmatched.push(...entries.map(e => e.name));
});
this.raiseError(`Additional completions found not included in 'unsorted': ${unmatched.join("\n")}`);
}
}
else {
if (options.includes) {
Expand Down Expand Up @@ -992,7 +1009,11 @@ namespace FourSlash {
}
}

private verifyCompletionsAreExactly(actual: readonly ts.CompletionEntry[], expected: readonly FourSlashInterface.ExpectedCompletionEntry[], marker?: ArrayOrSingle<string | Marker>) {
private verifyCompletionsAreExactly(actual: readonly ts.CompletionEntry[], expected: ArrayOrSingle<FourSlashInterface.ExpectedCompletionEntry> | FourSlashInterface.ExpectedExactCompletionsPlus, marker?: ArrayOrSingle<string | Marker>) {
if (!ts.isArray(expected)) {
expected = [expected];
}

// First pass: test that names are right. Then we'll test details.
assert.deepEqual(actual.map(a => a.name), expected.map(e => typeof e === "string" ? e : e.name), marker ? "At marker " + JSON.stringify(marker) : undefined);

Expand All @@ -1003,6 +1024,16 @@ namespace FourSlash {
}
this.verifyCompletionEntry(completion, expectedCompletion);
});

// All completions were correct in the sort order given. If that order was produced by a function
// like `completion.globalsPlus`, ensure the "plus" array was sorted in the same way.
const { plusArgument, plusFunctionName } = expected as FourSlashInterface.ExpectedExactCompletionsPlus;
if (plusArgument) {
assert.deepEqual(
plusArgument,
expected.filter(entry => plusArgument.includes(entry)),
`At marker ${JSON.stringify(marker)}: Argument to '${plusFunctionName}' was incorrectly sorted.`);
}
}

/** Use `getProgram` instead of accessing this directly. */
Expand Down
Loading