I'm developing an extension for VS Code that let's you create CodeLenses by writing a comment like that:
// Code-Lens-Action insert-snippet <SNIPPET_NAME>
...that inserts code snippet with a given name above that line.
I got stuck at the point of validating the snippet name because I could not find a way to list all active snippets (so snippets defined by the user but also built-in snippets and those provided by other extensions. Simply put: all snippets that may show up in IntelliSense while coding in the same file as the CodeLens) for the language of current active file. I want to notify the user that they might have mistyped the snippet name.
I know about this SO answer and I'll probably be using the vscode.commands.executeCommand function but I need to know if the name is correct.
I tried this:
vscode.commands.executeCommand("editor.action.insertSnippet", { "name": "non-existent-name" })
.then(
val => {
vscode.window.showInformationMessage(val);
},
reason => {
vscode.window.showErrorMessage(reason);
}
);
But it does not call the onrejected callback. Besides that, this command does not take in any Position or Range objects (I think; I haven't found any documentation on it besides that SO answer), it just inserts the snippet at the current cursor position. So I need to move the cursor before insertion, so I need to know if the name is correct before insertion to not move the cursor if name is not correct.
Clarification update:
So I don't know where you plan on the cursor being on that line.
I don't want to use current cursor position at all. I assume you used it in your example because you had to pass something to executeCommand. But some positions in the document won't get all suggestions. After a bit of testing (done by pressing Ctrl + Space (shortcut for editor.action.triggerSuggest) a bunch of times while changing the cursor position) I came to a conclusion that for a position to return all snippets it needs to be at least 1 white space away from any text.
Does the Position at the beginning of the snippet name do what you want?
No, because the only snippet name present in the document will be inside a comment that is supposed to trigger the CodeLens and suggestions don't show up there. I need a position in empty space where all suggestions show up.
So the supplementary question is: how to reliably get a position that is in a "free" space of the current document in a language agnostic way?
Quick Suggestionssetting forcommentsis enabled. You will either have to tell your users this or I suppose you could toggle the comment off, get your suggestions, and then toggle comments back on.