Nvim: Improve luasnip keymappings

This commit is contained in:
Marko Korhonen 2024-02-28 09:04:56 +02:00
parent a6c9da019f
commit 4bbfa49d6f
Signed by: FunctionalHacker
GPG key ID: A7F78BCB859CD890

View file

@ -26,6 +26,12 @@ return {
local cmp = require("cmp")
local luasnip = require("luasnip")
local has_words_before = function()
unpack = unpack or table.unpack
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
end
-- Set completeopt to have a better completion experience
vim.o.completeopt = "menuone,noselect"
@ -46,20 +52,44 @@ return {
behavior = cmp.ConfirmBehavior.Replace,
select = true,
}),
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
-- Snippet placeholder forward
["<C-f>"] = cmp.mapping(function(fallback)
if luasnip.jumpable(1) then
luasnip.jump(1)
else
fallback()
end
end, { "i", "s" }),
-- Snippet placeholder backward
["<C-b>"] = cmp.mapping(function(fallback)
if luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end, { "i", "s" }),
-- Completion menu forward
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
-- If only one entry, select it
if #cmp.get_entries() == 1 then
cmp.confirm({ select = true })
else
cmp.select_next_item()
end
elseif has_words_before() then
cmp.complete()
if #cmp.get_entries() == 1 then
cmp.confirm({ select = true })
end
else
fallback()
end
end, { "i", "s" }),
-- Completion menu backward
["<S-Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end