114 lines
2.3 KiB
Lua
114 lines
2.3 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
|
|
print("lspkind is not installed.")
|
|
return
|
|
end
|
|
|
|
lspkind.init()
|
|
|
|
local luasnip = require("luasnip")
|
|
local status, cmp = pcall(require, "cmp")
|
|
if not status then
|
|
print("nvim-cmp is not installed.")
|
|
return
|
|
end
|
|
|
|
if cmp == nil then
|
|
return
|
|
end
|
|
|
|
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 {
|
|
enabled = function ()
|
|
-- disable complete in comments
|
|
local context = require'cmp.config.context'
|
|
-- keep command mode completion enabled when cursor is in a comment
|
|
if vim.api.nvim_get_mode().mode == 'c' then
|
|
return true
|
|
else
|
|
return not context.in_treesitter_capture("comment")
|
|
and not context.in_syntax_group("Comment")
|
|
end
|
|
end,
|
|
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-b>"] = cmp.mapping.scroll_docs(-4),
|
|
["<C-f>"] = cmp.mapping.scroll_docs(4),
|
|
['<C-Space>'] = cmp.mapping.complete {
|
|
behavior = cmp.ConfirmBehavior.Insert,
|
|
select = true,
|
|
},
|
|
['<C-e>'] = cmp.mapping.abort(),
|
|
["<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]"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|