LuaSnip
LuaSnip copied to clipboard
nested snippets help (porting from ultisnips)
Hi! I'm trying to port over my snippets from ultisnips. In particular I have the following ultisnip for tex files:
snippet up "Use Package" b
\usepackage${1:[${2:options}]}{${3:name}}
$0
endsnippet
which basically first selects [options] and then I can either erase that alltogether with backspace or jump into it and have the cursor in \usepackage[|]{name}. I'm trying to replicate such behaviour with luasnips as follows:
s({ trig = 'up', dscr = 'Use package' }, {
t('\\usepackage'),
sn(1, {
t('['),
i(1, 'options'),
t(']'),
}),
t('{'),
i(2, 'name'),
t('}'),
i(0),
}),
but I cannot seem to achieve the desired behaviour. Do I need to add a double nested snippet? Why is that when I press <backspace> I'm back into normal mode rather than insert mode?
Thanks in advance.

This sounds like a good use case for a LuaSnip choice node. Here's an example where you choose between [options] (when you want to add package options) and an empty string (when you want only the package name).
s({trig="up"},
fmta(
[[
\usepackage<>{<>}
]],
{
c(1, {sn(nil, {t("["), i(1, "options"), t("]")}), t("")}),
i(2, "name"),
}
)
),
Here's the snippet in action:

Slightly different behavior from what you posted, but I think it's in the spirit of what you're after. You could also modify the empty string text node t("") to something else if you prefer.
I cycle through the choice nodes by pressing a key (in my case Control-F for "forward") mapped to <Plug>luasnip-next-choice; as far as I know using choice-nodes is an opt-in feature and you have to manually set a key binding in your config, e.g.
" Cycle forward through choice nodes with Control-F
imap <silent><expr> <C-f> luasnip#choice_active() ? '<Plug>luasnip-next-choice' : '<C-f>'
smap <silent><expr> <C-f> luasnip#choice_active() ? '<Plug>luasnip-next-choice' : '<C-f>'
(The LuaSnip README also has an example key binding for choice nodes.)
[Edit]
If you're porting from UltiSnips and use LaTeX, perhaps these resources might be useful:
- https://github.com/evesdropper/dotfiles/tree/main/nvim/luasnip#readme
- https://www.ejmastnak.com/tutorials/vim-latex/luasnip.html (self-plug)
Thanks for the reply and the excellent links. I'll thoroughly read them over the weekend.
So using choices is the best approximation I can get to the ultisnips behaviour of nested "deletable" (with backspace) snippets?
Thanks for the reply and the excellent links. I'll thoroughly read them over the weekend.
Great, glad they seem useful!
So using choices is the best approximation I can get to the ultisnips behaviour of nested "deletable" (with backspace) snippets?
Oof, I personally can't guarantee choice nodes are the best approximation---there may well be something closer to your Ultisnips version. I just suggested the choice node because it seemed like a good fit for your situation, i.e. switching between two flavors of the same command, even if it's a new technique and muscle memory to get used to.
Perhaps someone else will chime in later with a better suggestion.
I'd also +1 the choiceNodes.
It's possible to imitate ultisnips behaviour for nested placeholder with this (although it might not work anymore, been a while since I tried it last :sweat_smile:)
(That function is intended for the parser_nested_assembler-setup-option, but you can just pass the placeholder-content into it as a snippetNode, and get the modified, selectable snippetNode)
Thanks to both of you for the help! Using choice nodes to mimic the previous behaviour. Closing