vim-vsnip icon indicating copy to clipboard operation
vim-vsnip copied to clipboard

Add $PREFIX (or $TRIGGER) variable

Open jandamm opened this issue 4 years ago • 8 comments

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.

jandamm avatar Oct 04 '21 09:10 jandamm

It's interesting. But VSCode has not such variables https://github.com/microsoft/vscode/blob/main/src/vs/editor/contrib/snippet/snippetVariables.ts#L21

hrsh7th avatar Oct 04 '21 10:10 hrsh7th

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.

jandamm avatar Oct 04 '21 11:10 jandamm

Hm... Should we introduce context-specific variable resolution? I think this variable shouldn't work if the snippet loaded from package.json.

hrsh7th avatar Oct 04 '21 11:10 hrsh7th

Ah... We already have specific variables. https://github.com/hrsh7th/vim-vsnip/blob/master/autoload/vsnip/variable.vim#L179

hrsh7th avatar Oct 04 '21 11:10 hrsh7th

Maybe $VSNIP_PREFIX would be adequate then? 🙂

jandamm avatar Oct 04 '21 11:10 jandamm

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?

jandamm avatar Oct 09 '21 13:10 jandamm

I think we should add the prefix to Snippet.new( ... ) arguments.

I can merge the PR maybe.

hrsh7th avatar Oct 09 '21 18:10 hrsh7th

While I still think this would be a nice addition I'm not sure I'll find the necessary time to implement it.

jandamm avatar Nov 17 '21 18:11 jandamm