add snippet sequence

This commit is contained in:
Gabriela Araujo Britto 2021-10-29 13:28:23 -07:00
parent fd620c93f6
commit a52b84feeb
4 changed files with 31 additions and 6 deletions

View file

@ -1942,6 +1942,11 @@ namespace ts {
case SnippetKind.TabStop: case SnippetKind.TabStop:
emitTabStop(snippet); emitTabStop(snippet);
break; break;
case SnippetKind.Sequence:
emitSequence(snippet);
break;
default:
return Debug.fail("Unsupported SnippetKind.");
} }
} }
@ -1956,6 +1961,12 @@ namespace ts {
nonEscapingWrite(`\$${snippet.order}`); nonEscapingWrite(`\$${snippet.order}`);
} }
function emitSequence(snippet: SnippetSequence) {
for (const s of snippet.snippets) {
emitTabStop(s);
}
}
// //
// Identifiers // Identifiers
// //

View file

@ -6828,7 +6828,7 @@ namespace ts {
} }
/* @internal */ /* @internal */
export type SnippetElement = TabStop | Placeholder; export type SnippetElement = TabStop | Placeholder | SnippetSequence;
/* @internal */ /* @internal */
export interface TabStop { export interface TabStop {
@ -6842,6 +6842,12 @@ namespace ts {
order: number; order: number;
} }
/* @internal */
export interface SnippetSequence {
kind: SnippetKind.Sequence;
snippets: TabStop[];
}
// Reference: https://code.visualstudio.com/docs/editor/userdefinedsnippets#_snippet-syntax // Reference: https://code.visualstudio.com/docs/editor/userdefinedsnippets#_snippet-syntax
/* @internal */ /* @internal */
export const enum SnippetKind { export const enum SnippetKind {
@ -6849,6 +6855,7 @@ namespace ts {
Placeholder, // `${1:foo}` Placeholder, // `${1:foo}`
Choice, // `${1|one,two,three|}` Choice, // `${1|one,two,three|}`
Variable, // `$name`, `${name:default}` Variable, // `$name`, `${name:default}`
Sequence, // A sequence of tabstops, e.g. `$1$0`
} }
export const enum EmitFlags { export const enum EmitFlags {

View file

@ -852,12 +852,19 @@ namespace ts.Completions {
let tabstopStart = 1; let tabstopStart = 1;
if (preferences.includeCompletionsWithSnippetText) { if (preferences.includeCompletionsWithSnippetText) {
isSnippet = true; 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 an initial and final tabstop (i.e. `$1$0`) in the body of the suggested member,
// if it has one.
// Note: this assumes we won't have more than one body in the completion nodes, which should be the case. // 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("")); const emptyStatement = factory.createExpressionStatement(factory.createIdentifier(""));
setSnippetElement(emptyStatement1, { kind: SnippetKind.TabStop, order: 1 }); setSnippetElement(emptyStatement, {
kind: SnippetKind.Sequence,
snippets: [
{ kind: SnippetKind.TabStop, order: 1 },
{ kind: SnippetKind.TabStop, order: 0 },
],
});
tabstopStart = 2; tabstopStart = 2;
body = factory.createBlock([emptyStatement1], /* multiline */ true); body = factory.createBlock([emptyStatement], /* multiline */ true);
} }
else { else {
body = factory.createBlock([], /* multiline */ true); body = factory.createBlock([], /* multiline */ true);

View file

@ -30,7 +30,7 @@ verify.completions({
}, },
isSnippet: true, isSnippet: true,
insertText: insertText:
"\"\\$usd\"(${2:a}: ${3:number}): ${4:number} {\n $1\n}\n", "\"\\$usd\"(${2:a}: ${3:number}): ${4:number} {\n $1$0\n}\n",
} }
], ],
}); });