Skip to content

Remove placeholders in class member snippets #46598

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 1 commit into from
Oct 29, 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
43 changes: 5 additions & 38 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -849,15 +849,14 @@ namespace ts.Completions {
const importAdder = codefix.createImportAdder(sourceFile, program, preferences, host);

let body;
let tabstopStart = 1;
if (preferences.includeCompletionsWithSnippetText) {
isSnippet = true;
// We are adding a final tabstop (i.e. $0) in the body of the suggested member, if it has one.
// We are adding a tabstop (i.e. `$0`) in the body of the suggested member,
// if it has one, so that the cursor ends up in the body once the completion is inserted.
// Note: this assumes we won't have more than one body in the completion nodes, which should be the case.
const emptyStatement1 = factory.createExpressionStatement(factory.createIdentifier(""));
setSnippetElement(emptyStatement1, { kind: SnippetKind.TabStop, order: 1 });
tabstopStart = 2;
body = factory.createBlock([emptyStatement1], /* multiline */ true);
const emptyStatement = factory.createExpressionStatement(factory.createIdentifier(""));
setSnippetElement(emptyStatement, { kind: SnippetKind.TabStop, order: 0 });
body = factory.createBlock([emptyStatement], /* multiline */ true);
}
else {
body = factory.createBlock([], /* multiline */ true);
Expand Down Expand Up @@ -915,9 +914,6 @@ namespace ts.Completions {
isAbstract);

if (completionNodes.length) {
if (preferences.includeCompletionsWithSnippetText) {
addSnippets(completionNodes, tabstopStart);
}
insertText = printer.printSnippetList(ListFormat.MultiLine, factory.createNodeArray(completionNodes), sourceFile);
}

Expand Down Expand Up @@ -965,35 +961,6 @@ namespace ts.Completions {
return undefined;
}

function addSnippets(nodes: Node[], orderStart: number): void {
let order = orderStart;
for (const node of nodes) {
addSnippetsWorker(node, /*parent*/ undefined);
}

function addSnippetsWorker(node: Node, parent: Node | undefined) {
if (isVariableLike(node) && node.kind === SyntaxKind.Parameter) {
// Placeholder
setSnippetElement(node.name, { kind: SnippetKind.Placeholder, order });
order += 1;
if (node.type) {
setSnippetElement(node.type, { kind: SnippetKind.Placeholder, order });
order += 1;
}
}
else if (isTypeNode(node) && parent && isFunctionLikeDeclaration(parent)) {
setSnippetElement(node, { kind: SnippetKind.Placeholder, order });
order += 1;
}
else if (isTypeParameterDeclaration(node) && parent && isFunctionLikeDeclaration(parent)) {
setSnippetElement(node, { kind: SnippetKind.Placeholder, order });
order += 1;
}

forEachChild(node, child => addSnippetsWorker(child, node));
}
}

function createSnippetPrinter(
printerOptions: PrinterOptions,
) {
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fourslash/completionsOverridingMethod2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ verify.completions({
},
isSnippet: true,
insertText:
"\"\\$usd\"(${2:a}: ${3:number}): ${4:number} {\n $1\n}\n",
"\"\\$usd\"(a: number): number {\n $0\n}\n",
}
],
});