vimvim-pluginultisnips

Optional lines in Ultisnips using parameters


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

Solution

  • 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