nvimconfig/after/plugin/completion.lua
Robert Morrison 962d7f6971
General completion/snips setup
configure
- nvim-cmp
- luasnips
- nvim-treesitter
2022-07-15 06:49:59 +01:00

91 lines
1.9 KiB
Lua

-- Configure Completion
vim.opt.completeopt = {"menu","menuone","noselect"}
vim.opt.shortmess:append "c"
local ok, lspkind = pcall(require, "lspkind")
if not ok then
return
end
lspkind.init()
local luasnip = require("luasnip")
local cmp = require("cmp")
local has_words_before = function()
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
cmp.setup {
mapping = {
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
elseif has_words_before() then
cmp.complete()
else
fallback()
end
end, { "i", "s" }),
["<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
end, { "i", "s" }),
["<C-n>"] = cmp.mapping.select_next_item { behavior = cmp.SelectBehavior.Insert },
["<C-p>"] = cmp.mapping.select_prev_item { behavior = cmp.SelectBehavior.Insert },
["<C-d>"] = cmp.mapping.scroll_docs(-4),
["<C-f>"] = cmp.mapping.scroll_docs(4),
["<CR>"] = cmp.mapping(
cmp.mapping.confirm {
behavior = cmp.ConfirmBehavior.Insert,
select = true,
})
},
sources = {
{name = "nvim_lua"},
{name = "nvim_lsp"},
{name = "path"},
{name = "luasnip"},
{
name = "buffer",
option = {
keyword_length = 5,
},
},
},
snippet = {
expand = function(args)
require("luasnip").lsp_expand(args.body)
end,
},
experimental = {
native_menu = false,
ghost_text = true,
},
formatting = {
format = lspkind.cmp_format {
mode = 'symbol_text',
menu = {
buffer = "[BUF]",
nvim_lsp = "[LSP]",
nvim_lua = "[API]",
path = "[PTH]",
luasnip = "[SNP]"
}
}
}
}