Skip to content

Commit

Permalink
remove placeholders in class member snippets (#46598)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabritto committed Oct 29, 2021
1 parent e3fab9f commit 6b1e8f7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 39 deletions.
43 changes: 5 additions & 38 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -857,15 +857,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 @@ -923,9 +922,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 @@ -973,35 +969,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",
}
],
});

0 comments on commit 6b1e8f7

Please sign in to comment.