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