97 lines
2.0 KiB
Lua
97 lines
2.0 KiB
Lua
-- Setup nvim-cmp.
|
|
local lspkind = require('lspkind')
|
|
lspkind.init()
|
|
|
|
local cmp = require('cmp')
|
|
local luasnip = require("luasnip")
|
|
|
|
local function has_words_before()
|
|
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{
|
|
completion = { completeopt = 'menu,menuone,noinsert' },
|
|
snippet = {
|
|
expand = function(args)
|
|
require("luasnip").lsp_expand(args.body)
|
|
end,
|
|
},
|
|
mapping = {
|
|
['<C-d>'] = cmp.mapping.scroll_docs(-4),
|
|
['<C-f>'] = cmp.mapping.scroll_docs(4),
|
|
['<C-Space>'] = cmp.mapping.complete(),
|
|
['<C-e>'] = cmp.mapping.close(),
|
|
['<CR>'] = cmp.mapping.confirm({
|
|
behavior = cmp.ConfirmBehavior.Insert,
|
|
select = true,
|
|
}),
|
|
|
|
["<C-n>"] = 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" }),
|
|
|
|
["<C-p>"] = 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" }),
|
|
},
|
|
sources = {
|
|
{ name = 'nvim_lua' },
|
|
|
|
{ name = 'nvim_lsp', max_item_count = 20 },
|
|
{ name = 'cmp_tabnine' },
|
|
{ name = 'path' },
|
|
{ name = 'luasnip' },
|
|
{ name = 'buffer', max_item_count = 10, keyword_length = 5 },
|
|
},
|
|
formatting = {
|
|
format = lspkind.cmp_format{
|
|
with_text = true,
|
|
menu = {
|
|
buffer = "[buf]",
|
|
cmp_tabnine = "[tab9]",
|
|
nvim_lsp = "[lsp]",
|
|
nvim_lua = "[api]",
|
|
path = "[path]",
|
|
luasnip = "[snip]",
|
|
}
|
|
}
|
|
},
|
|
experimental = {
|
|
native_menu = false,
|
|
ghost_text = true,
|
|
}
|
|
}
|
|
|
|
-- Use buffer source for `/`.
|
|
cmp.setup.cmdline('/', {
|
|
sources = {
|
|
{ name = 'buffer' }
|
|
}
|
|
})
|
|
|
|
-- Use cmdline & path source for ':'.
|
|
-- cmp.setup.cmdline(':', {
|
|
-- sources = cmp.config.sources({
|
|
-- { name = 'path' }
|
|
-- }, {
|
|
-- { name = 'cmdline' }
|
|
-- })
|
|
-- })
|
|
|
|
|