I am attempting to add Completion snippets to an extension I’m writing based on the completions sample ( https://github.com/microsoft/vscode-extension-samples/tree/main/completions-sample ). I would like to have it branch complete. In my extension, SET is a keyword that then has possible params. Each param then has further params.
Ex: SET FOO|BAR|BAS where SET FOO can be ON|OFF, SET BAR is 1|2|3, and SET BAS is RED|BLUE.
I’ve tried to do it with
const SetCompletion = new vscode.CompletionItem('SET');
SetCompletion.insertText = new vscode.SnippetString('SET ${1|FOO,BAR,BAS|};');
And then Follow on with
const SetFooCompletion = new vscode.CompletionItem('FOO');
SetFooCompletion.insertText = new vscode.SnippetString('FOO ${1|ON,OFF|};');
const SetBarCompletion = new vscode.CompletionItem('BAR');
SetBarCompletion.insertText = new vscode.SnippetString('BAR ${1|1,2,3|};');
const SetBasCompletion = new vscode.CompletionItem('BAS');
SetBasCompletion.insertText = new vscode.SnippetString('BAS ${1|RED,BLUE|};');
And this sort of works if I retrigger completion after the first item is inserted. I think there might be a better way. In my real world code, there are about 15 SET things that I can call on, and each one has different types of params. Some are ON|OFF, some are numbers, some strings. In the above snippets, SET BAR ON is invalid, since BAR is an integer and only FOO is ON|OFF so I don’t want to provide all possible parameters to all SET statements. They should be specific to the SET command that resolves in the first completion.
Is this possible?
Edit: Fixed typos in sample code
2
Answers
The following code works.
If you use
new vscode.CompletionItem('FOO');
it does not work. Looks like the label must match the last word of the line.Also the line
does not give completion items. The
SET
must be a separate word. The completion items are created but are not shown.Or you can try:
You have to trigger the CompletionItems with
Ctrl+Space
after selecting one of theSET
options. Look carefully when you leave the snippet (or double snippet) when usingTAB
(multiple times)I took a long look at this trying to figure out what is going on and concluded that because your first snippet is a choice snippet that you will not be able to do it as simply as you would like.
Normally, if you used this
that
command
property would help you avoid having to manually re-trigger the suggestions forFOO/BAR/BAS
. But it doesn’t work. I am convinced that thecommand
runs after the snippet is inserted but before it is completed.You can see this if you use other commands like
In this case, you won’t even get the choice presented because the
leaveSnippet
command has already run!So it seems the simplest is this:
You would have to make a bunch of
SET xxx CompletionItems
but there would be no manually triggering of suggestions to complete.As an interesting aside (and more proof that the
command
is running before the snippet choice and thus of no use is code like this:With the command defined as
If you run that in a
FOO.<some extension>
orBAR.<ext>
orBAS.<ext>
it will work as expected. The snippet fileNameBase is resolved before thecommand
is run – the command just selects the top choice.