Skip to content

Improve quickinfo/signature/completions baselines #52308

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

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
100 changes: 94 additions & 6 deletions src/harness/fourslashImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1972,18 +1972,53 @@ export class TestState {
const baselineFile = this.getBaselineFileNameForContainingTestFile();
const result = ts.arrayFrom(this.testData.markerPositions.entries(), ([name, marker]) => ({
marker: { ...marker, name },
quickInfo: this.languageService.getQuickInfoAtPosition(marker.fileName, marker.position)
item: this.languageService.getQuickInfoAtPosition(marker.fileName, marker.position)
}));
Harness.Baseline.runBaseline(baselineFile, stringify(result));
const annotations = this.annotateContentWithTooltips(
result,
"quickinfo",
item => item.textSpan,
({ displayParts, documentation, tags }) => [
...(displayParts ? displayParts.map(p => p.text).join("").split("\n") : []),
...(documentation?.length ? documentation.map(p => p.text).join("").split("\n") : []),
...(tags?.length ? tags.map(p => `@${p.name} ${p.text?.map(dp => dp.text).join("") ?? ""}`).join("\n").split("\n") : [])
]);
Harness.Baseline.runBaseline(baselineFile, annotations + "\n\n" + stringify(result));
}

public baselineSignatureHelp() {
const baselineFile = this.getBaselineFileNameForContainingTestFile();
const result = ts.arrayFrom(this.testData.markerPositions.entries(), ([name, marker]) => ({
marker: { ...marker, name },
signatureHelp: this.languageService.getSignatureHelpItems(marker.fileName, marker.position, /*options*/ undefined)
item: this.languageService.getSignatureHelpItems(marker.fileName, marker.position, /*options*/ undefined)
}));
Harness.Baseline.runBaseline(baselineFile, stringify(result));
const annotations = this.annotateContentWithTooltips(
result,
"signature help",
() => undefined, // use default: marker.position
(item, previous) => {
const { documentation, tags, prefixDisplayParts, suffixDisplayParts, separatorDisplayParts, parameters } = item.items[item.selectedItemIndex];
const tooltip = [];
let signature = "";
if (prefixDisplayParts.length) signature += prefixDisplayParts.map(p => p.text).join("");
const separator = separatorDisplayParts.map(p => p.text).join("");
signature += parameters.map((p, i) => {
const text = p.displayParts.map(dp => dp.text).join("");
return i === item.argumentIndex ? "**" + text + "**" : text;
}).join(separator);
if (suffixDisplayParts.length) signature += suffixDisplayParts.map(p => p.text).join("");
tooltip.push(signature);
// only display signature documentation on the last argument when multiple arguments are marked
if (previous?.applicableSpan.start !== item.applicableSpan.start) {
if (documentation?.length) tooltip.push(...documentation.map(p => p.text).join("").split("\n"));
if (tags?.length) {
tooltip.push(...tags.map(p => `@${p.name} ${p.text?.map(dp => dp.text).join("") ?? ""}`).join("\n").split("\n"));
}
}
return tooltip;
}
);
Harness.Baseline.runBaseline(baselineFile, annotations + "\n\n" + stringify(result));
}

public baselineCompletions(preferences?: ts.UserPreferences) {
Expand All @@ -1993,7 +2028,7 @@ export class TestState {
const completions = this.getCompletionListAtCaret(preferences);
return {
marker: { ...marker, name },
completionList: {
item: {
...completions,
entries: completions?.entries.map(entry => ({
...entry,
Expand All @@ -2002,7 +2037,60 @@ export class TestState {
}
};
});
Harness.Baseline.runBaseline(baselineFile, stringify(result));
const annotations = this.annotateContentWithTooltips(
result,
"completions",
item => item.optionalReplacementSpan,
item => item.entries?.flatMap(
entry => entry.displayParts
? entry.displayParts.map(p => p.text).join("").split("\n")
: [`(${entry.kindModifiers}${entry.kind}) ${entry.name}`])
);
Harness.Baseline.runBaseline(baselineFile, annotations + "\n\n" + stringify(result));
}

private annotateContentWithTooltips<T extends ts.QuickInfo | ts.SignatureHelpItems | {
optionalReplacementSpan?: ts.TextSpan,
entries?: {
name: string,
kind: string,
kindModifiers?: string,
displayParts?: unknown,
}[]
}>(
items: ({
marker: Marker & { name: string },
item: T | undefined
})[],
opName: "completions" | "quickinfo" | "signature help",
getSpan: (t: T) => ts.TextSpan | undefined,
getToolTipContents: (t: T, prev: T | undefined) => string[] | undefined): string {
const bar = "-".repeat(70);
const sorted = items.slice();
// sort by file, then *backwards* by position in the file so I can insert multiple times on a line without counting
sorted.sort((q1, q2) =>
q1.marker.fileName === q1.marker.fileName
? (q1.marker.position > q2.marker.position ? -1 : 1)
: (q1.marker.fileName > q1.marker.fileName ? 1 : -1));
const files: Map<string, string[]> = new Map();
let previous: T | undefined;
for (const { marker, item } of sorted) {
const span = (item ? getSpan(item) : undefined) ?? { start: marker.position, length: 1 };
const startLc = this.languageServiceAdapterHost.positionToLineAndCharacter(marker.fileName, span.start);
const underline = " ".repeat(startLc.character) + "^".repeat(span.length);
let tooltip = [
bar,
...(item ? getToolTipContents(item, previous) : undefined) ?? [`No ${opName} at /*${marker.name}*/.`],
bar,
];
tooltip = tooltip.map(l => "| " + l);
const lines = files.get(marker.fileName) ?? this.getFileContent(marker.fileName).split(/\r?\n/);
lines.splice(startLc.line + 1, 0, underline, ...tooltip);
files.set(marker.fileName, lines);
previous = item;
}
return Array.from(files.entries(), ([fileName, lines]) => `=== ${fileName} ===\n` + lines.map(l => "// " + l).join("\n"))
.join("\n\n");
}

public baselineSmartSelection() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,103 @@
=== /tests/cases/fourslash/completionDetailsOfContextSensitiveParameterNoCrash.ts ===
// type __ = never;
//
// interface CurriedFunction1<T1, R> {
// (): CurriedFunction1<T1, R>;
// (t1: T1): R;
// }
// interface CurriedFunction2<T1, T2, R> {
// (): CurriedFunction2<T1, T2, R>;
// (t1: T1): CurriedFunction1<T2, R>;
// (t1: __, t2: T2): CurriedFunction1<T1, R>;
// (t1: T1, t2: T2): R;
// }
//
// interface CurriedFunction3<T1, T2, T3, R> {
// (): CurriedFunction3<T1, T2, T3, R>;
// (t1: T1): CurriedFunction2<T2, T3, R>;
// (t1: __, t2: T2): CurriedFunction2<T1, T3, R>;
// (t1: T1, t2: T2): CurriedFunction1<T3, R>;
// (t1: __, t2: __, t3: T3): CurriedFunction2<T1, T2, R>;
// (t1: T1, t2: __, t3: T3): CurriedFunction1<T2, R>;
// (t1: __, t2: T2, t3: T3): CurriedFunction1<T1, R>;
// (t1: T1, t2: T2, t3: T3): R;
// }
//
// interface CurriedFunction4<T1, T2, T3, T4, R> {
// (): CurriedFunction4<T1, T2, T3, T4, R>;
// (t1: T1): CurriedFunction3<T2, T3, T4, R>;
// (t1: __, t2: T2): CurriedFunction3<T1, T3, T4, R>;
// (t1: T1, t2: T2): CurriedFunction2<T3, T4, R>;
// (t1: __, t2: __, t3: T3): CurriedFunction3<T1, T2, T4, R>;
// (t1: __, t2: __, t3: T3): CurriedFunction2<T2, T4, R>;
// (t1: __, t2: T2, t3: T3): CurriedFunction2<T1, T4, R>;
// (t1: T1, t2: T2, t3: T3): CurriedFunction1<T4, R>;
// (t1: __, t2: __, t3: __, t4: T4): CurriedFunction3<T1, T2, T3, R>;
// (t1: T1, t2: __, t3: __, t4: T4): CurriedFunction2<T2, T3, R>;
// (t1: __, t2: T2, t3: __, t4: T4): CurriedFunction2<T1, T3, R>;
// (t1: __, t2: __, t3: T3, t4: T4): CurriedFunction2<T1, T2, R>;
// (t1: T1, t2: T2, t3: __, t4: T4): CurriedFunction1<T3, R>;
// (t1: T1, t2: __, t3: T3, t4: T4): CurriedFunction1<T2, R>;
// (t1: __, t2: T2, t3: T3, t4: T4): CurriedFunction1<T1, R>;
// (t1: T1, t2: T2, t3: T3, t4: T4): R;
// }
//
// declare var curry: {
// <T1, R>(func: (t1: T1) => R, arity?: number): CurriedFunction1<T1, R>;
// <T1, T2, R>(func: (t1: T1, t2: T2) => R, arity?: number): CurriedFunction2<T1, T2, R>;
// <T1, T2, T3, R>(func: (t1: T1, t2: T2, t3: T3) => R, arity?: number): CurriedFunction3<T1, T2, T3, R>;
// <T1, T2, T3, T4, R>(func: (t1: T1, t2: T2, t3: T3, t4: T4) => R, arity?: number): CurriedFunction4<T1, T2, T3, T4, R>;
// (func: (...args: any[]) => any, arity?: number): (...args: any[]) => any;
// placeholder: __;
// };
//
// export type StylingFunction = (
// keys: (string | false | undefined) | (string | false | undefined)[],
// ...rest: unknown[]
// ) => object;
//
// declare const getStylingByKeys: (
// mergedStyling: object,
// keys: (string | false | undefined) | (string | false | undefined)[],
// ...args: unknown[]
// ) => object;
//
// declare var mergedStyling: object;
//
// export const createStyling: CurriedFunction3<
// (base16Theme: object) => unknown,
// object | undefined,
// object | undefined,
// StylingFunction
// > = curry<
// (base16Theme: object) => unknown,
// object | undefined,
// object | undefined,
// StylingFunction
// >(
// (
// getStylingFromBase16: (base16Theme: object) => unknown,
// options: object = {},
// themeOrStyling: object = {},
// ...args
// ): StylingFunction => {
// return curry(getStylingByKeys, 2)(mergedStyling, ...args);
// ^^^^
// | ----------------------------------------------------------------------
// | (parameter) args: any
// | ----------------------------------------------------------------------
// },
// 3
// );

[
{
"marker": {
"fileName": "/tests/cases/fourslash/completionDetailsOfContextSensitiveParameterNoCrash.ts",
"position": 3101,
"name": ""
},
"quickInfo": {
"item": {
"kind": "parameter",
"kindModifiers": "",
"textSpan": {
Expand Down
59 changes: 58 additions & 1 deletion tests/baselines/reference/completionEntryForUnionMethod.baseline
Original file line number Diff line number Diff line change
@@ -1,11 +1,68 @@
=== /tests/cases/fourslash/completionEntryForUnionMethod.ts ===
// var y: Array<string>|Array<number>;
// y.map(
// ^^^
// | ----------------------------------------------------------------------
// | (property) Array<T>.concat: {
// | (...items: ConcatArray<string>[]): string[];
// | (...items: (string | ConcatArray<string>)[]): string[];
// | } | {
// | (...items: ConcatArray<number>[]): number[];
// | (...items: (number | ConcatArray<...>)[]): number[];
// | }
// | (property) Array<T>.every: {
// | <S extends string>(predicate: (value: string, index: number, array: string[]) => value is S, thisArg?: any): this is S[];
// | (predicate: (value: string, index: number, array: string[]) => unknown, thisArg?: any): boolean;
// | } | {
// | ...;
// | }
// | (property) Array<T>.filter: {
// | <S extends string>(predicate: (value: string, index: number, array: string[]) => value is S, thisArg?: any): S[];
// | (predicate: (value: string, index: number, array: string[]) => unknown, thisArg?: any): string[];
// | } | {
// | ...;
// | }
// | (method) Array<T>.forEach(callbackfn: ((value: string, index: number, array: string[]) => void) & ((value: number, index: number, array: number[]) => void), thisArg: any): void
// | (method) Array<T>.indexOf(searchElement: never, fromIndex: number): number
// | (method) Array<T>.join(separator?: string): string
// | (method) Array<T>.lastIndexOf(searchElement: never, fromIndex: number): number
// | (property) Array<T>.length: number
// | (method) Array<T>.map<unknown>(callbackfn: ((value: string, index: number, array: string[]) => unknown) & ((value: number, index: number, array: number[]) => unknown), thisArg: any): unknown[]
// | (method) Array<T>.pop(): string | number
// | (method) Array<T>.push(items: never[]): number
// | (property) Array<T>.reduce: {
// | (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string): string;
// | (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string, initialValue: string): string;
// | <U>(callbackfn: (previousValue: U, currentValue: string, currentIndex: number, array: string[]) => U, initialValue: U): U;
// | } | {
// | ...;
// | }
// | (property) Array<T>.reduceRight: {
// | (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string): string;
// | (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string, initialValue: string): string;
// | <U>(callbackfn: (previousValue: U, currentValue: string, currentIndex: number, array: string[]) => U, initialValue: U): U;
// | } | {
// | ...;
// | }
// | (method) Array<T>.reverse(): string[] | number[]
// | (method) Array<T>.shift(): string | number
// | (method) Array<T>.slice(start?: number, end?: number): string[] | number[]
// | (method) Array<T>.some(predicate: ((value: string, index: number, array: string[]) => unknown) & ((value: number, index: number, array: number[]) => unknown), thisArg: any): boolean
// | (method) Array<T>.sort(compareFn: ((a: string, b: string) => number) & ((a: number, b: number) => number)): string[] | number[]
// | (method) Array<T>.splice(start: number, deleteCount?: number): string[] | number[] (+2 overloads)
// | (method) Array<T>.toLocaleString(): string
// | (method) Array<T>.toString(): string
// | (method) Array<T>.unshift(items: never[]): number
// | ----------------------------------------------------------------------

[
{
"marker": {
"fileName": "/tests/cases/fourslash/completionEntryForUnionMethod.ts",
"position": 41,
"name": ""
},
"completionList": {
"item": {
"flags": 0,
"isGlobalCompletion": false,
"isMemberCompletion": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
=== /a.ts ===
// import * as foo from "";
// ^
// | ----------------------------------------------------------------------
// | ----------------------------------------------------------------------

[
{
"marker": {
"fileName": "/a.ts",
"position": 22,
"name": ""
},
"completionList": {
"item": {
"isGlobalCompletion": false,
"isMemberCompletion": false,
"isNewIdentifierLocation": true,
Expand Down
16 changes: 14 additions & 2 deletions tests/baselines/reference/completionImportCallAssertion.baseline
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
=== /tests/cases/fourslash/main.ts ===
// import("./other.json", {});
// ^
// | ----------------------------------------------------------------------
// | (property) ImportCallOptions.assert?: ImportAssertions
// | ----------------------------------------------------------------------
// import("./other.json", { asse});
// ^^^^
// | ----------------------------------------------------------------------
// | (property) ImportCallOptions.assert?: ImportAssertions
// | ----------------------------------------------------------------------

[
{
"marker": {
"fileName": "/tests/cases/fourslash/main.ts",
"position": 24,
"name": "0"
},
"completionList": {
"item": {
"flags": 0,
"isGlobalCompletion": false,
"isMemberCompletion": true,
Expand Down Expand Up @@ -73,7 +85,7 @@
"position": 57,
"name": "1"
},
"completionList": {
"item": {
"flags": 0,
"isGlobalCompletion": false,
"isMemberCompletion": true,
Expand Down
31 changes: 29 additions & 2 deletions tests/baselines/reference/completionsClassMembers1.baseline
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
=== /tests/cases/fourslash/completionsClassMembers1.ts ===
// interface I {
// method(): void;
// }
//
// export class C implements I {
// property = "foo"
//
// ^
// | ----------------------------------------------------------------------
// | (method) I.method(): void
// | abstract
// | accessor
// | async
// | constructor
// | declare
// | get
// | override
// | private
// | protected
// | public
// | readonly
// | set
// | static
// | ----------------------------------------------------------------------
// }

[
{
"marker": {
"fileName": "/tests/cases/fourslash/completionsClassMembers1.ts",
"position": 93,
"position": 92,
"name": ""
},
"completionList": {
"item": {
"flags": 0,
"isGlobalCompletion": false,
"isMemberCompletion": true,
Expand Down
Loading