2023-09-06 09:19:32 +02:00
|
|
|
local cmp = require("cmp")
|
|
|
|
local luasnip = require("luasnip")
|
|
|
|
require("luasnip.loaders.from_vscode").lazy_load()
|
|
|
|
|
2023-09-17 09:36:22 +02:00
|
|
|
local has_words_before = function()
|
|
|
|
if vim.api.nvim_buf_get_option(0, "buftype") == "prompt" then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
|
|
|
|
return col ~= 0 and vim.api.nvim_buf_get_text(0, line - 1, 0, line - 1, col, {})[1]:match("^%s*$") == nil
|
|
|
|
end
|
|
|
|
|
2023-09-06 09:19:32 +02:00
|
|
|
cmp.setup({
|
|
|
|
formatting = {
|
|
|
|
format = require("lspkind").cmp_format({
|
|
|
|
mode = "symbol", -- show only symbol annotations
|
|
|
|
maxwidth = 50, -- prevent the popup from showing more than provided characters
|
|
|
|
ellipsis_char = "...", -- when popup menu exceed maxwidth, the truncated part would show ellipsis_char instead
|
2023-09-17 09:36:22 +02:00
|
|
|
symbol_map = {
|
|
|
|
Copilot = "",
|
|
|
|
},
|
2023-09-06 09:19:32 +02:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
snippet = {
|
|
|
|
-- REQUIRED - you must specify a snippet engine
|
|
|
|
expand = function(args)
|
|
|
|
require("luasnip").lsp_expand(args.body)
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
mapping = cmp.mapping.preset.insert({
|
|
|
|
["<C-b>"] = cmp.mapping.scroll_docs(-4),
|
|
|
|
["<C-f>"] = cmp.mapping.scroll_docs(4),
|
|
|
|
["<C-Space>"] = cmp.mapping.complete(),
|
|
|
|
["<C-e>"] = cmp.mapping.abort(),
|
|
|
|
["<CR>"] = cmp.mapping.confirm({ select = true }),
|
|
|
|
["<Tab>"] = cmp.mapping(function(fallback)
|
2023-09-17 09:36:22 +02:00
|
|
|
if cmp.visible() and has_words_before() then
|
|
|
|
cmp.select_next_item({ behavior = cmp.SelectBehavior.Select })
|
2023-09-06 09:19:32 +02:00
|
|
|
elseif luasnip.expand_or_jumpable() then
|
|
|
|
luasnip.expand_or_jump()
|
|
|
|
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" }),
|
|
|
|
}),
|
2023-09-06 17:45:31 +02:00
|
|
|
sources = {
|
|
|
|
{ name = "async_path", priority = 1 },
|
|
|
|
{ name = "buffer", priority = 1 },
|
|
|
|
{ name = "luasnip", priority = 2 },
|
2023-09-17 09:36:22 +02:00
|
|
|
{ name = "copilot", group_index = 3 },
|
2023-09-06 17:45:31 +02:00
|
|
|
{ name = "nvim_lsp", priority = 4 },
|
|
|
|
},
|
2023-09-06 09:19:32 +02:00
|
|
|
})
|
|
|
|
|
2023-09-06 17:45:31 +02:00
|
|
|
-- Set configuration for specific filetype.
|
|
|
|
cmp.setup.filetype("gitcommit", {
|
|
|
|
sources = cmp.config.sources({
|
|
|
|
{ name = "buffer" },
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
|
|
|
|
-- Use buffer source for `/` and `?` (if you enabled `native_menu`, this won't work anymore).
|
|
|
|
cmp.setup.cmdline({ "/", "?" }, {
|
|
|
|
mapping = cmp.mapping.preset.cmdline(),
|
|
|
|
sources = {
|
|
|
|
{ name = "buffer" },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
-- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore).
|
|
|
|
cmp.setup.cmdline(":", {
|
|
|
|
mapping = cmp.mapping.preset.cmdline(),
|
|
|
|
sources = cmp.config.sources({
|
|
|
|
{ name = "async_path" },
|
|
|
|
}, {
|
|
|
|
{ name = "cmdline" },
|
2023-09-06 09:19:32 +02:00
|
|
|
}),
|
2023-09-07 08:45:50 +02:00
|
|
|
enabled = function()
|
|
|
|
-- Set of commands where cmp will be disabled
|
|
|
|
local disabled = {
|
|
|
|
IncRename = true,
|
|
|
|
}
|
|
|
|
-- Get first word of cmdline
|
|
|
|
local cmd = vim.fn.getcmdline():match("%S+")
|
|
|
|
-- Return true if cmd isn't disabled
|
|
|
|
-- else call/return cmp.close(), which returns false
|
|
|
|
return not disabled[cmd] or cmp.close()
|
|
|
|
end,
|
2023-09-06 09:19:32 +02:00
|
|
|
})
|
2023-09-07 08:39:43 +02:00
|
|
|
-- If you want insert `(` after select function or method item
|
|
|
|
local cmp_autopairs = require("nvim-autopairs.completion.cmp")
|
|
|
|
local handlers = require("nvim-autopairs.completion.handlers")
|
|
|
|
|
|
|
|
cmp.event:on(
|
|
|
|
"confirm_done",
|
|
|
|
cmp_autopairs.on_confirm_done({
|
|
|
|
filetypes = {
|
|
|
|
-- "*" is a alias to all filetypes
|
|
|
|
["*"] = {
|
|
|
|
["("] = {
|
|
|
|
kind = {
|
|
|
|
cmp.lsp.CompletionItemKind.Function,
|
|
|
|
cmp.lsp.CompletionItemKind.Method,
|
|
|
|
},
|
|
|
|
handler = handlers["*"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
-- Disable for functional languages
|
|
|
|
haskell = false,
|
|
|
|
nix = false,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|