Feat: set nice completion mapping with tab and enter

This commit is contained in:
Bertrand Benjamin 2022-05-02 07:27:33 +02:00
parent 2633d677c4
commit 62ba0e08fb
1 changed files with 31 additions and 3 deletions

View File

@ -41,7 +41,7 @@ local lsp_symbols = {
local cmp = require'cmp'
cmp.setup{
completion = {
completeopt = 'menuone,noselect',
completeopt = 'menuone,noinsert,noselect',
},
formatting = {
format = function(entry, vim_item)
@ -64,8 +64,27 @@ cmp.setup{
end,
},
mapping = {
-- ["<cr>"] = cmp.mapping.confirm({select = true, behavior = cmp.ConfirmBehavior.Replace}),
["<Tab>"] = cmp.mapping.confirm({select = true, behavior = cmp.ConfirmBehavior.Insert}),
["<cr>"] = cmp.mapping.confirm({select = false, behavior = cmp.ConfirmBehavior.Replace}),
-- ["<Tab>"] = cmp.mapping.confirm({select = true, behavior = cmp.ConfirmBehavior.Insert}),
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif vim.fn["vsnip#available"](1) == 1 then
feedkey("<Plug>(vsnip-expand-or-jump)", "")
elseif has_words_before() then
cmp.complete()
else
fallback() -- The fallback function sends a already mapped key. In this case, it's probably `<Tab>`.
end
end, { "i", "s" }),
["<S-Tab>"] = cmp.mapping(function()
if cmp.visible() then
cmp.select_prev_item()
elseif vim.fn["vsnip#jumpable"](-1) == 1 then
feedkey("<Plug>(vsnip-jump-prev)", "")
end
end, { "i", "s" }),
},
sources = {
{ name = 'vsnip' },
@ -91,5 +110,14 @@ cmp.setup{
},
}
-- Use buffer source for `/` (if you enabled `native_menu`, this won't work anymore).
cmp.setup.cmdline('/', {
mapping = cmp.mapping.preset.cmdline(),
sources = {
{ name = 'buffer' }
}
})
vim.cmd [[autocmd FileType TelescopePrompt lua require('cmp').setup.buffer { enabled = false }]]
vim.g.vsnip_snippet_dir = '~/.config/nvim/vsnips'