Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add $PREFIX (or $TRIGGER) variable #218

Open
jandamm opened this issue Oct 4, 2021 · 8 comments
Open

Add $PREFIX (or $TRIGGER) variable #218

jandamm opened this issue Oct 4, 2021 · 8 comments

Comments

@jandamm
Copy link
Contributor

jandamm commented Oct 4, 2021

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.

@hrsh7th
Copy link
Owner

hrsh7th commented Oct 4, 2021

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

@jandamm
Copy link
Contributor Author

jandamm commented Oct 4, 2021

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.

@hrsh7th
Copy link
Owner

hrsh7th commented Oct 4, 2021

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

@hrsh7th
Copy link
Owner

hrsh7th commented Oct 4, 2021

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

@jandamm
Copy link
Contributor Author

jandamm commented Oct 4, 2021

Maybe $VSNIP_PREFIX would be adequate then? 🙂

@jandamm
Copy link
Contributor Author

jandamm commented Oct 9, 2021

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?

@hrsh7th
Copy link
Owner

hrsh7th commented Oct 9, 2021

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

I can merge the PR maybe.

@jandamm
Copy link
Contributor Author

jandamm commented Nov 17, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants