1

I am trying to have some additional lines inserted in snippets based on a parameter. I am not sure how design such snippet.

snippet 'mysnip' 'snippets with optional lines'
  This snippet line1 is inserted by default
  <This line1a should be inserted if parameter1 is true>
  This snippet line2 is inserted by default
  <This line2a should be inserted if parameter1 is true>
endsnippet
cbaumhardt
  • 303
  • 2
  • 11
Srik
  • 2,341
  • 2
  • 21
  • 35

3 Answers3

1

It is not very clear to me how/where you want to enter your parameters.

One option is to define two snippets, one called mysnip and the other one mysnip1 - in this case you pass the parameter in the snippet name, and the definition of these two snippets should be straightforward.

Another option is to just define one snippet mysnip, and pass the parameter somewhere within this snippet. A working example could look like this:

snippet mysnip1
${1:Change this snippet line to have the text "True" (without quotes).}
This line is always present. `!p
if t[1]=="True":
    snip += "A line displayed when $1 has the text True.
`
endsnippet
cbaumhardt
  • 303
  • 2
  • 11
  • Thanks for your response. I am looking for something similar to the code you have given. In the given e.g., the text "True" will be displayed in the final output. Is there any way not to display it? – Srik Feb 22 '16 at 15:12
  • @Srik: Where do you want to enter the parameter? Do just want to enter it one time and then it shall vanish? – cbaumhardt Feb 22 '16 at 15:21
  • I am just getting started with Ultisnips. I am open for suggestion on how the parameter should be passed. With first approach I'll end up with lot of repetitive code. The second approach works. The only thing is I have to remove True after the snippet is pasted. Which is okay. If it is possible to make it go away that would be a perfect solution. – Srik Feb 22 '16 at 15:26
  • Well if I was you, I would write two snippets then. Yes it does repeat some code, but I think it is a clearer approach and it saves you time whenever you use both snippets (cause no additional parameter needs to be entered). Furthermore, this is the way that most snippets are implemented (if you use honza/vim-snippets take a look at `:UltiSnipsEdit!` and select 3, `all.snippets` - you see a lot of repeated code there too). – cbaumhardt Feb 22 '16 at 15:45
  • I put my parameters on commented lines. So that I do not have to remove it later on. – Srik Mar 03 '16 at 05:18
1

You can fake this using regular expression triggers. It only works if you do not want to have tabstops in your optional arguments though:

snippet /mysnip([a-z]*)/ "Optionals" r
this is always here!`!p
if "a" in match.group(1):
  snip += "only when a" 
if "b" in match.group(1):
  snip += "only when b"`
endsnippet

If you type mysnip it will just be the first line, mysnipb the first and thirdm and mysnipab will be all of it.

SirVer
  • 1,397
  • 1
  • 16
  • 15
0

Can't you put your optional lines in a variable that your snippet engine will expand?

In case it doesn't join automatically empty lines produced from a empty variable, you may have to have your variable contain a newline character and put it or the line before/after.

Luc Hermitte
  • 31,979
  • 7
  • 69
  • 83