Nvim: Improve luasnip keymappings
This commit is contained in:
parent
a6c9da019f
commit
4bbfa49d6f
|
@ -26,6 +26,12 @@ return {
|
||||||
local cmp = require("cmp")
|
local cmp = require("cmp")
|
||||||
local luasnip = require("luasnip")
|
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
|
-- Set completeopt to have a better completion experience
|
||||||
vim.o.completeopt = "menuone,noselect"
|
vim.o.completeopt = "menuone,noselect"
|
||||||
|
|
||||||
|
@ -46,20 +52,44 @@ return {
|
||||||
behavior = cmp.ConfirmBehavior.Replace,
|
behavior = cmp.ConfirmBehavior.Replace,
|
||||||
select = true,
|
select = true,
|
||||||
}),
|
}),
|
||||||
["<Tab>"] = cmp.mapping(function(fallback)
|
-- Snippet placeholder forward
|
||||||
if cmp.visible() then
|
["<C-f>"] = cmp.mapping(function(fallback)
|
||||||
cmp.select_next_item()
|
if luasnip.jumpable(1) then
|
||||||
elseif luasnip.expand_or_jumpable() then
|
luasnip.jump(1)
|
||||||
luasnip.expand_or_jump()
|
|
||||||
else
|
else
|
||||||
fallback()
|
fallback()
|
||||||
end
|
end
|
||||||
end, { "i", "s" }),
|
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)
|
["<S-Tab>"] = cmp.mapping(function(fallback)
|
||||||
if cmp.visible() then
|
if cmp.visible() then
|
||||||
cmp.select_prev_item()
|
cmp.select_prev_item()
|
||||||
elseif luasnip.jumpable(-1) then
|
|
||||||
luasnip.jump(-1)
|
|
||||||
else
|
else
|
||||||
fallback()
|
fallback()
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue