Skip to content

Better formatting for generated fourslash tests #1377

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
merged 2 commits into from
Jul 9, 2025
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
35 changes: 17 additions & 18 deletions internal/fourslash/_scripts/convertFourslash.mts
Original file line number Diff line number Diff line change
Expand Up @@ -399,15 +399,15 @@ function parseVerifyCompletionArg(arg: ts.Expression): VerifyCompletionsCmd | un
console.error(`Expected array literal expression for completion.globalsPlus items, got ${items.getText()}`);
return undefined;
}
expected = `${funcName}([]fourslash.CompletionsExpectedItem{`;
expected = `${funcName}(\n[]fourslash.CompletionsExpectedItem{`;
for (const elem of items.elements) {
const result = parseExpectedCompletionItem(elem);
if (!result) {
return undefined;
}
expected += result + ", ";
expected += "\n" + result + ",";
}
expected += "}";
expected += "\n}";
if (opts) {
if (!ts.isObjectLiteralExpression(opts)) {
console.error(`Expected object literal expression for completion.globalsPlus options, got ${opts.getText()}`);
Expand Down Expand Up @@ -444,17 +444,17 @@ function parseVerifyCompletionArg(arg: ts.Expression): VerifyCompletionsCmd | un
if (!result) {
return undefined;
}
expected += result + ", ";
expected += "\n" + result + ",";
}
}
else {
const result = parseExpectedCompletionItem(init);
if (!result) {
return undefined;
}
expected += result;
expected += "\n" + result + ",";
}
expected += "}";
expected += "\n}";
}
if (propName === "includes") {
(goArgs ??= {}).includes = expected;
Expand All @@ -469,17 +469,17 @@ function parseVerifyCompletionArg(arg: ts.Expression): VerifyCompletionsCmd | un
case "excludes":
let excludes = "[]string{";
if (ts.isStringLiteral(init)) {
excludes += `${getGoStringLiteral(init.text)}, `;
excludes += `\n${getGoStringLiteral(init.text)},`;
}
else if (ts.isArrayLiteralExpression(init)) {
for (const elem of init.elements) {
if (!ts.isStringLiteral(elem)) {
return undefined; // Shouldn't happen
}
excludes += `${getGoStringLiteral(elem.text)}, `;
excludes += `\n${getGoStringLiteral(elem.text)},`;
}
}
excludes += "}";
excludes += "\n}";
(goArgs ??= {}).excludes = excludes;
break;
case "isNewIdentifierLocation":
Expand Down Expand Up @@ -518,7 +518,7 @@ function parseExpectedCompletionItem(expr: ts.Expression): string | undefined {
let isDeprecated = false; // !!!
let isOptional = false;
let extensions: string[] = []; // !!!
let item = "&lsproto.CompletionItem{";
let itemProps: string[] = [];
let name: string | undefined;
let insertText: string | undefined;
let filterText: string | undefined;
Expand All @@ -544,7 +544,7 @@ function parseExpectedCompletionItem(expr: ts.Expression): string | undefined {
if (!result) {
return undefined;
}
item += `SortText: ptrTo(string(${result})), `;
itemProps.push(`SortText: ptrTo(string(${result})),`);
if (result === "ls.SortTextOptionalMember") {
isOptional = true;
}
Expand All @@ -569,15 +569,15 @@ function parseExpectedCompletionItem(expr: ts.Expression): string | undefined {
break;
case "isRecommended":
if (init.kind === ts.SyntaxKind.TrueKeyword) {
item += `Preselect: ptrTo(true), `;
itemProps.push(`Preselect: ptrTo(true),`);
}
break;
case "kind":
const kind = parseKind(init);
if (!kind) {
return undefined;
}
item += `Kind: ptrTo(${kind}), `;
itemProps.push(`Kind: ptrTo(${kind}),`);
break;
case "kindModifiers":
const modifiers = parseKindModifiers(init);
Expand All @@ -603,11 +603,10 @@ function parseExpectedCompletionItem(expr: ts.Expression): string | undefined {
filterText ??= name;
name += "?";
}
item += `Label: ${getGoStringLiteral(name!)}, `;
if (insertText) item += `InsertText: ptrTo(${getGoStringLiteral(insertText)}), `;
if (filterText) item += `FilterText: ptrTo(${getGoStringLiteral(filterText)}), `;
item += "}";
return item;
if (filterText) itemProps.unshift(`FilterText: ptrTo(${getGoStringLiteral(filterText)}),`);
if (insertText) itemProps.unshift(`InsertText: ptrTo(${getGoStringLiteral(insertText)}),`);
itemProps.unshift(`Label: ${getGoStringLiteral(name!)},`);
return `&lsproto.CompletionItem{\n${itemProps.join("\n")}}`;
}
console.error(`Expected string literal or object literal for expected completion item, got ${expr.getText()}`);
return undefined; // Unsupported expression type
Expand Down
4 changes: 3 additions & 1 deletion internal/fourslash/tests/gen/asOperatorCompletion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ var y = x as /**/`
EditRange: ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Includes: []fourslash.CompletionsExpectedItem{"T"},
Includes: []fourslash.CompletionsExpectedItem{
"T",
},
},
})
}
9 changes: 7 additions & 2 deletions internal/fourslash/tests/gen/basicClassMembers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ var t = new n(0, 1, '');`
EditRange: ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Includes: []fourslash.CompletionsExpectedItem{"x", "y"},
Excludes: []string{"z"},
Includes: []fourslash.CompletionsExpectedItem{
"x",
"y",
},
Excludes: []string{
"z",
},
},
})
}
13 changes: 12 additions & 1 deletion internal/fourslash/tests/gen/codeCompletionEscaping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,18 @@ ___foo; __foo;/**/`
EditRange: ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Includes: []fourslash.CompletionsExpectedItem{&lsproto.CompletionItem{Kind: ptrTo(lsproto.CompletionItemKindText), SortText: ptrTo(string(ls.SortTextJavascriptIdentifiers)), Label: "__foo"}, &lsproto.CompletionItem{Kind: ptrTo(lsproto.CompletionItemKindText), SortText: ptrTo(string(ls.SortTextJavascriptIdentifiers)), Label: "___foo"}},
Includes: []fourslash.CompletionsExpectedItem{
&lsproto.CompletionItem{
Label: "__foo",
Kind: ptrTo(lsproto.CompletionItemKindText),
SortText: ptrTo(string(ls.SortTextJavascriptIdentifiers)),
},
&lsproto.CompletionItem{
Label: "___foo",
Kind: ptrTo(lsproto.CompletionItemKindText),
SortText: ptrTo(string(ls.SortTextJavascriptIdentifiers)),
},
},
},
})
}
7 changes: 6 additions & 1 deletion internal/fourslash/tests/gen/completionAfterNewline2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ func TestCompletionAfterNewline2(t *testing.T) {
EditRange: ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Exact: completionGlobalsPlus([]fourslash.CompletionsExpectedItem{&lsproto.CompletionItem{Label: "foo"}}, false),
Exact: completionGlobalsPlus(
[]fourslash.CompletionsExpectedItem{
&lsproto.CompletionItem{
Label: "foo",
},
}, false),
},
})
}
7 changes: 6 additions & 1 deletion internal/fourslash/tests/gen/completionAfterNewline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ func TestCompletionAfterNewline(t *testing.T) {
EditRange: ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Exact: completionGlobalsPlus([]fourslash.CompletionsExpectedItem{&lsproto.CompletionItem{Label: "foo"}}, false),
Exact: completionGlobalsPlus(
[]fourslash.CompletionsExpectedItem{
&lsproto.CompletionItem{
Label: "foo",
},
}, false),
},
})
}
7 changes: 6 additions & 1 deletion internal/fourslash/tests/gen/completionAsKeyword_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ function foo() {
EditRange: ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Includes: []fourslash.CompletionsExpectedItem{&lsproto.CompletionItem{SortText: ptrTo(string(ls.SortTextGlobalsOrKeywords)), Label: "as"}},
Includes: []fourslash.CompletionsExpectedItem{
&lsproto.CompletionItem{
Label: "as",
SortText: ptrTo(string(ls.SortTextGlobalsOrKeywords)),
},
},
},
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ class Foo extends Bar<TwoKeys> {
EditRange: ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Includes: []fourslash.CompletionsExpectedItem{&lsproto.CompletionItem{Label: "baz", InsertText: ptrTo("baz(a: { a?: (options: { thisFails?: any; }) => any; b?: (options: { thisFails?: any; }) => any; }): void {\n}"), FilterText: ptrTo("baz")}},
Includes: []fourslash.CompletionsExpectedItem{
&lsproto.CompletionItem{
Label: "baz",
InsertText: ptrTo("baz(a: { a?: (options: { thisFails?: any; }) => any; b?: (options: { thisFails?: any; }) => any; }): void {\n}"),
FilterText: ptrTo("baz"),
},
},
},
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ class ChildTwo extends Parent {
EditRange: ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Includes: []fourslash.CompletionsExpectedItem{"shouldWork"},
Includes: []fourslash.CompletionsExpectedItem{
"shouldWork",
},
},
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ test(/*ts*/)
EditRange: ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Includes: []fourslash.CompletionsExpectedItem{"\"a\"", "\"b\""},
Includes: []fourslash.CompletionsExpectedItem{
"\"a\"",
"\"b\"",
},
},
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ test({ foo: ['a', /*ts*/] })`
EditRange: ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Includes: []fourslash.CompletionsExpectedItem{"\"a\"", "\"b\""},
Includes: []fourslash.CompletionsExpectedItem{
"\"a\"",
"\"b\"",
},
},
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ test({ foo: [/*ts*/] })`
EditRange: ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Includes: []fourslash.CompletionsExpectedItem{"\"a\"", "\"b\""},
Includes: []fourslash.CompletionsExpectedItem{
"\"a\"",
"\"b\"",
},
},
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ out.a.a./*3*/a`
EditRange: ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Exact: []fourslash.CompletionsExpectedItem{"a"},
Exact: []fourslash.CompletionsExpectedItem{
"a",
},
},
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ test({ type: /*ts*/ })`
EditRange: ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Includes: []fourslash.CompletionsExpectedItem{"\"a\"", "\"b\""},
Includes: []fourslash.CompletionsExpectedItem{
"\"a\"",
"\"b\"",
},
},
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ func TestCompletionEntryForShorthandPropertyAssignment(t *testing.T) {
EditRange: ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Includes: []fourslash.CompletionsExpectedItem{&lsproto.CompletionItem{Kind: ptrTo(lsproto.CompletionItemKindField), Label: "name"}},
Includes: []fourslash.CompletionsExpectedItem{
&lsproto.CompletionItem{
Label: "name",
Kind: ptrTo(lsproto.CompletionItemKindField),
},
},
},
})
}
7 changes: 6 additions & 1 deletion internal/fourslash/tests/gen/completionExportFrom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ export {} /*2*/;`
EditRange: ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Includes: []fourslash.CompletionsExpectedItem{&lsproto.CompletionItem{SortText: ptrTo(string(ls.SortTextGlobalsOrKeywords)), Label: "from"}},
Includes: []fourslash.CompletionsExpectedItem{
&lsproto.CompletionItem{
Label: "from",
SortText: ptrTo(string(ls.SortTextGlobalsOrKeywords)),
},
},
},
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ a[|./**/|]`
EditRange: ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Exact: []fourslash.CompletionsExpectedItem{&lsproto.CompletionItem{Label: "p1"}, &lsproto.CompletionItem{Label: "p2", InsertText: ptrTo("[p2]")}},
Exact: []fourslash.CompletionsExpectedItem{
&lsproto.CompletionItem{
Label: "p1",
},
&lsproto.CompletionItem{
Label: "p2",
InsertText: ptrTo("[p2]"),
},
},
},
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ switch (a) {
EditRange: ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Exact: []fourslash.CompletionsExpectedItem{"abacus", "abaddon", "arf"},
Exact: []fourslash.CompletionsExpectedItem{
"abacus",
"abaddon",
"arf",
},
},
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ foo("[|/**/|]")`
EditRange: ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Exact: []fourslash.CompletionsExpectedItem{&lsproto.CompletionItem{Label: "bla"}},
Exact: []fourslash.CompletionsExpectedItem{
&lsproto.CompletionItem{
Label: "bla",
},
},
},
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ func TestCompletionForStringLiteral15(t *testing.T) {
EditRange: ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Exact: []fourslash.CompletionsExpectedItem{&lsproto.CompletionItem{Label: "foo"}},
Exact: []fourslash.CompletionsExpectedItem{
&lsproto.CompletionItem{
Label: "foo",
},
},
},
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ f2<Foo>("/*8*/", "/*9*/",,,);`
EditRange: ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Exact: []fourslash.CompletionsExpectedItem{"a", "b", "c"},
Exact: []fourslash.CompletionsExpectedItem{
"a",
"b",
"c",
},
},
})
}
Loading
Loading