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.