2022-12-06 15:52:28 +00:00
|
|
|
local cmp_status_ok, cmp = pcall(require, "cmp")
|
|
|
|
if not cmp_status_ok then
|
2023-01-01 16:30:31 +00:00
|
|
|
return
|
2022-12-06 15:52:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local snip_status_ok, luasnip = pcall(require, "luasnip")
|
|
|
|
if not snip_status_ok then
|
2023-01-01 16:30:31 +00:00
|
|
|
return
|
2022-12-06 15:52:28 +00:00
|
|
|
end
|
|
|
|
require("luasnip/loaders/from_vscode").lazy_load()
|
|
|
|
|
|
|
|
local check_backspace = function()
|
2024-03-17 12:32:47 +00:00
|
|
|
local col = vim.fn.col(".") - 1
|
|
|
|
return col == 0 or vim.fn.getline("."):sub(col, col):match("%s")
|
2022-12-06 15:52:28 +00:00
|
|
|
end
|
|
|
|
|
2024-03-17 12:32:47 +00:00
|
|
|
local lspkind_status_ok, lspkind = pcall(require, "lspkind")
|
|
|
|
if not lspkind_status_ok then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2024-01-03 15:12:33 +00:00
|
|
|
-- some other good icons
|
2022-12-06 15:52:28 +00:00
|
|
|
local kind_icons = {
|
2024-01-03 15:12:33 +00:00
|
|
|
Text = "",
|
2023-01-01 16:30:31 +00:00
|
|
|
Method = "m",
|
2024-01-03 15:12:33 +00:00
|
|
|
Function = "",
|
2023-01-01 16:30:31 +00:00
|
|
|
Constructor = "",
|
|
|
|
Field = "",
|
2024-01-03 15:12:33 +00:00
|
|
|
Variable = "",
|
|
|
|
Class = "",
|
2023-01-01 16:30:31 +00:00
|
|
|
Interface = "",
|
|
|
|
Module = "",
|
|
|
|
Property = "",
|
|
|
|
Unit = "",
|
2024-01-03 15:12:33 +00:00
|
|
|
Value = "",
|
2023-01-01 16:30:31 +00:00
|
|
|
Enum = "",
|
2024-01-03 15:12:33 +00:00
|
|
|
Keyword = "",
|
2023-01-01 16:30:31 +00:00
|
|
|
Snippet = "",
|
2024-01-03 15:12:33 +00:00
|
|
|
Color = "",
|
|
|
|
File = "",
|
2023-01-01 16:30:31 +00:00
|
|
|
Reference = "",
|
2024-01-03 15:12:33 +00:00
|
|
|
Folder = "",
|
2023-01-01 16:30:31 +00:00
|
|
|
EnumMember = "",
|
2024-01-03 15:12:33 +00:00
|
|
|
Constant = "",
|
2023-01-01 16:30:31 +00:00
|
|
|
Struct = "",
|
|
|
|
Event = "",
|
2024-01-03 15:12:33 +00:00
|
|
|
Operator = "",
|
|
|
|
TypeParameter = "",
|
2023-05-21 20:18:09 +01:00
|
|
|
Copilot = "",
|
2023-06-06 20:23:01 +01:00
|
|
|
DB = "",
|
2022-12-06 15:52:28 +00:00
|
|
|
}
|
|
|
|
-- find more here: https://www.nerdfonts.com/cheat-sheet
|
|
|
|
|
2024-03-17 12:32:47 +00:00
|
|
|
cmp.setup({
|
2023-01-01 16:30:31 +00:00
|
|
|
snippet = {
|
|
|
|
expand = function(args)
|
|
|
|
luasnip.lsp_expand(args.body) -- For `luasnip` users.
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
mapping = {
|
2024-03-17 12:32:47 +00:00
|
|
|
["<C-n>"] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Insert }),
|
|
|
|
["<C-p>"] = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Insert }),
|
|
|
|
["<C-u>"] = cmp.mapping.scroll_docs(-4),
|
|
|
|
["<C-d>"] = cmp.mapping.scroll_docs(4),
|
|
|
|
["<C-e>"] = cmp.mapping.abort(),
|
|
|
|
["<C-y>"] = cmp.mapping(
|
|
|
|
cmp.mapping.confirm({
|
|
|
|
behavior = cmp.ConfirmBehavior.Insert,
|
|
|
|
select = true,
|
|
|
|
}),
|
|
|
|
{ "i", "c" }
|
|
|
|
),
|
2023-01-01 16:30:31 +00:00
|
|
|
["<C-Space>"] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }),
|
2023-01-14 04:05:17 +00:00
|
|
|
-- Accept currently selected item. If none selected, do nothing.
|
2023-01-14 04:32:16 +00:00
|
|
|
["<C-k>"] = cmp.mapping(function(fallback)
|
|
|
|
if luasnip.expandable() then
|
|
|
|
luasnip.expand()
|
|
|
|
elseif luasnip.expand_or_jumpable() then
|
|
|
|
luasnip.expand_or_jump()
|
|
|
|
elseif check_backspace() then
|
|
|
|
fallback()
|
|
|
|
else
|
|
|
|
fallback()
|
|
|
|
end
|
|
|
|
end),
|
|
|
|
["<C-j>"] = cmp.mapping(function(fallback)
|
2023-06-06 20:23:01 +01:00
|
|
|
if luasnip.jumpable(-1) then
|
|
|
|
luasnip.jump(-1)
|
2023-01-14 04:32:16 +00:00
|
|
|
else
|
|
|
|
fallback()
|
|
|
|
end
|
2024-03-17 12:32:47 +00:00
|
|
|
end),
|
2023-01-01 16:30:31 +00:00
|
|
|
},
|
|
|
|
formatting = {
|
|
|
|
fields = { "kind", "abbr", "menu" },
|
2024-03-17 12:32:47 +00:00
|
|
|
format = lspkind.cmp_format({
|
|
|
|
mode = "symbol_text", -- show only symbol annotations
|
|
|
|
maxwidth = 50, -- prevent the popup from showing more than provided characters (e.g 50 will not show more than 50 characters)
|
|
|
|
-- can also be a function to dynamically calculate max width such as
|
|
|
|
-- maxwidth = function() return math.floor(0.45 * vim.o.columns) end,
|
|
|
|
ellipsis_char = "...", -- when popup menu exceed maxwidth, the truncated part would show ellipsis_char instead (must define maxwidth first)
|
|
|
|
show_labelDetails = true, -- show labelDetails in menu. Disabled by default
|
|
|
|
|
|
|
|
-- The function below will be called before any actual modifications from lspkind
|
|
|
|
-- so that you can provide more controls on popup customization. (See [#30](https://github.com/onsails/lspkind-nvim/pull/30))
|
|
|
|
before = function(entry, vim_item)
|
|
|
|
vim_item.menu = ({
|
|
|
|
nvim_lsp = "[LSP]",
|
|
|
|
nvim_lua = "[LSP]",
|
|
|
|
path = "[Path]",
|
|
|
|
})[entry.source.name]
|
|
|
|
return vim_item
|
|
|
|
end,
|
|
|
|
}),
|
2023-01-01 16:30:31 +00:00
|
|
|
},
|
|
|
|
sorting = {
|
|
|
|
comparators = {
|
|
|
|
cmp.config.compare.exact,
|
|
|
|
cmp.config.compare.offset,
|
|
|
|
cmp.config.compare.recently_used,
|
2023-06-28 00:34:24 +01:00
|
|
|
|
|
|
|
--Make entries that start with underline appear after
|
|
|
|
function(entry1, entry2)
|
2024-03-17 12:32:47 +00:00
|
|
|
local _, entry1_under = entry1.completion_item.label:find("^_+")
|
|
|
|
local _, entry2_under = entry2.completion_item.label:find("^_+")
|
2023-06-28 00:34:24 +01:00
|
|
|
entry1_under = entry1_under or 0
|
|
|
|
entry2_under = entry2_under or 0
|
|
|
|
if entry1_under > entry2_under then
|
|
|
|
return false
|
|
|
|
elseif entry1_under < entry2_under then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end,
|
2023-01-01 16:30:31 +00:00
|
|
|
},
|
|
|
|
},
|
2024-03-17 12:32:47 +00:00
|
|
|
sources = cmp.config.sources({
|
|
|
|
{ name = "nvim_lsp" },
|
|
|
|
{ name = "nvim_lua" },
|
|
|
|
{ name = "luasnip" },
|
|
|
|
{ name = "path" },
|
|
|
|
}, {
|
|
|
|
{ name = "buffer", keyword_length = 5 },
|
|
|
|
}),
|
|
|
|
})
|
2024-01-03 15:12:33 +00:00
|
|
|
|
2024-03-17 12:32:47 +00:00
|
|
|
local cmp_autopairs = require("nvim-autopairs.completion.cmp")
|
|
|
|
cmp.event:on("confirm_done", cmp_autopairs.on_confirm_done())
|