nvim: Improve snippet jumping

Snippets can be jumped through more easily now since jumping between
snippets (with <tab> and <s-tab>) takes precedence over completions and
jumping through completions. That means when a snippet has been expanded
we can now cycle through its insertion points without worrying about
activating completion items instead.

Additionally, only jump through insertion points as long as we are
within the snippet boundaries with the cursor, so it doesn't surprise
jump later when pressing <tab> from somewhere else in the file.
This commit is contained in:
Marty Oehme 2024-06-27 17:24:29 +02:00
parent 894b7ff175
commit 851bf58b21
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A

View file

@ -135,12 +135,12 @@ return {
}), -- disable selection in cmd mode
}),
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
-- You could replace the expand_or_jumpable() calls with expand_or_locally_jumpable()
-- they way you will only jump inside the snippet region
elseif luasnip.expand_or_jumpable() then
-- expand_or_jumpable() will always jump
-- expand_or_locally_jumpable() only jumps when still inside snippet region
if luasnip.expand_or_locally_jumpable() then
luasnip.expand_or_jump()
elseif cmp.visible() then
cmp.select_next_item()
elseif has_words_before() then
cmp.complete()
else
@ -148,10 +148,10 @@ return {
end
end, { "i", "s" }),
["<S-Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.jumpable(-1) then
if luasnip.jumpable(-1) then
luasnip.jump(-1)
elseif cmp.visible() then
cmp.select_prev_item()
else
fallback()
end