nixos-config/home-manager/neovim/nvim-cmp.lua

90 lines
2.6 KiB
Lua
Raw Normal View History

2022-06-20 13:49:53 +00:00
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
2022-06-29 06:41:32 +00:00
local cmp = require("cmp")
2022-06-20 13:49:53 +00:00
local luasnip = require("luasnip")
cmp.setup({
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body)
2022-06-20 13:49:53 +00:00
end
},
2022-06-29 06:41:32 +00:00
sources = cmp.config.sources({
2023-07-05 10:54:27 +00:00
{ name = "copilot" },
{ name = "nvim_lsp" },
{ name = "nvim_lsp_signature_help" },
{ name = "luasnip" },
{ name = "path" },
{ name = "buffer" },
{ name = "nvim-lua" },
{ name = "calc" },
2022-06-20 13:49:53 +00:00
}),
mapping = cmp.mapping.preset.insert({
2022-06-29 06:41:32 +00:00
["<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 }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
2022-06-20 13:49:53 +00:00
["<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" }),
}),
2022-08-23 06:03:33 +00:00
experimental = { ghost_text = true },
window = {
completion = cmp.config.window.bordered(),
documentation = cmp.config.window.bordered(),
},
formatting = {
fields = { "menu", "abbr", "kind" },
format = function(entry, item)
local menu_icon = {
2023-07-05 10:54:27 +00:00
copilot = "",
2022-08-23 06:03:33 +00:00
nvim_lsp = "λ",
luasnip = "",
buffer = "Ω",
path = "🖿",
}
item.menu = menu_icon[entry.source.name]
return item
end,
},
2022-06-20 13:49:53 +00:00
})
-- `/` cmdline setup.
2022-06-29 06:41:32 +00:00
cmp.setup.cmdline("/", {
sources = cmp.config.sources(
2022-07-04 06:52:35 +00:00
{ name = "buffer" }
2022-06-29 06:41:32 +00:00
)
})
2022-06-20 13:49:53 +00:00
-- `:` cmdline setup.
2022-06-29 06:41:32 +00:00
cmp.setup.cmdline(":", {
sources = cmp.config.sources(
2022-07-04 06:52:35 +00:00
{ name = "path" },
{ name = "cmdline" }
2022-06-29 06:41:32 +00:00
)
2022-06-20 13:49:53 +00:00
})