vim-vsnip
vim-vsnip copied to clipboard
Add $PREFIX (or $TRIGGER) variable
Having these nearly identical snippets:
{
"Let": {
"prefix": ["let"],
"body": ["${1:private }let ${2:name}${3:: Type}${4: = ${5:decl}}"]
},
"Var": {
"prefix": ["var"],
"body": ["${1:private }var ${2:name}${3:: Type}${4: = ${5:decl}}"]
}
}
I would love to be able to write them as one:
{
"Let": {
"prefix": ["let", "var"],
"body": ["${1:private }$PREFIX ${2:name}${3:: Type}${4: = ${5:decl}}"]
}
}
Where $PREFIX is either let or var depending on what was used to trigger the snippet.
It's interesting. But VSCode has not such variables https://github.com/microsoft/vscode/blob/main/src/vs/editor/contrib/snippet/snippetVariables.ts#L21
I know but there's also $VIM in vsnip which doesn't exist in VS Code either.
At least for me the VSCode snippet compatibility is rather for being able to use VSCode snippets in vim than write snippets for VSCode (which I don't use).
Such a feature should obviously not be used by anyone who wants to use their snippets in vim and VSCode both. For vim only users it might improve writing snippets. I have multiple snippets which I could unclutter using the trigger/prefix inside the snippet.
Hm... Should we introduce context-specific variable resolution?
I think this variable shouldn't work if the snippet loaded from package.json.
Ah... We already have specific variables. https://github.com/hrsh7th/vim-vsnip/blob/master/autoload/vsnip/variable.vim#L179
Maybe $VSNIP_PREFIX would be adequate then? 🙂
I've had a look and it seems that this feature isn't possible without changes to vsnip. At the time the variable is evaluated the snippet is also removed from the current line.
So the prefix would have to be added to the context when the snippet is detected and then handed down to the registered variables in their context.
Is this something you would merge when I create a PR for it?
I think we should add the prefix to Snippet.new( ... ) arguments.
I can merge the PR maybe.
While I still think this would be a nice addition I'm not sure I'll find the necessary time to implement it.