2025-05-21 16:25:20 -07:00

143 lines
4.0 KiB
Lua

return {
"hrsh7th/nvim-cmp",
event = "InsertEnter",
dependencies = {
"hrsh7th/cmp-buffer", -- source for text in buffer
"hrsh7th/cmp-path", -- source for file system paths
"hrsh7th/cmp-cmdline", -- source for command mode
"saadparwaiz1/cmp_luasnip", -- for autocompletion
"rafamadriz/friendly-snippets", -- useful snippets
"onsails/lspkind.nvim", -- vs-code like pictograms
{
"L3MON4D3/LuaSnip",
-- follow latest release of given major
version = "v2.*",
-- install jsregexp
build = "make install_jsregexp",
}, -- snippet engine
},
config = function()
local cmp = require("cmp")
local luasnip = require("luasnip")
local lspkind = require("lspkind")
-- Loads vscode style snippets from installed plugins (e.g. friendly-snippets)
require("luasnip.loaders.from_vscode").lazy_load()
-- `/` cmdline setup.
cmp.setup.cmdline("/", {
mapping = cmp.mapping.preset.cmdline(),
sources = {
{ name = "buffer" },
},
})
-- `:` cmdline setup.
cmp.setup.cmdline(":", {
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources({
{ name = "path" },
}, {
{
name = "cmdline",
option = {
ignore_cmds = { "Man", "!" },
},
},
}),
})
-- Setup completion options and snippets and mappings
cmp.setup({
-- Configure how the completion menu behaves
-- menu: show menu
-- menuone: show menu even when there is only one option
-- preview: preview window explaining the offered completion
-- select: select an option automatically (noselect is the reverse)
completion = {
completeopt = "menu,menuone,preview,select",
},
-- Configure how nvim-cmp interacts with snippet engine
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body)
end,
},
mapping = cmp.mapping.preset.insert({
["<C-k>"] = cmp.mapping.select_prev_item(), -- previous suggestion
["<C-j>"] = cmp.mapping.select_next_item(), -- next suggestion
["<C-b>"] = cmp.mapping.scroll_docs(-4), -- scroll down in docs
["<C-f>"] = cmp.mapping.scroll_docs(4), -- scroll up in docs
["<C-Up>"] = cmp.mapping.scroll_docs(-4), -- scroll down in docs
["<C-Down>"] = cmp.mapping.scroll_docs(4), -- scroll up in docs
["<C-Space>"] = cmp.mapping.complete(), -- show completion suggestions
["<C-e>"] = cmp.mapping.abort(), -- close completion window
["<C-l>"] = cmp.mapping.complete({
config = { sources = {
{ name = "nvim_lsp" },
} },
}), -- show only lsp completions
["<C-s"] = cmp.mapping.complete({
config = { sources = {
{ name = "luasnip" },
} },
}), -- show only snippet completions
-- On CR, insert selected snippet or completion
["<CR>"] = cmp.mapping(function(fallback)
if cmp.visible() then
if luasnip.expandable() then
luasnip.expand()
else
cmp.confirm({
select = true,
})
end
else
fallback()
end
end),
-- On tab with completion open, go to next option
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.locally_jumpable(1) then
luasnip.jump(1)
else
fallback()
end
end, { "i", "s", "c" }),
-- On shift tab with completion open, go to prev option
["<S-Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.locally_jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end, { "i", "s", "c" }),
}),
-- Sources for autocompletion
sources = cmp.config.sources({
{ name = "nvim_lsp" }, -- lsp suggestions
{ name = "luasnip" }, -- snippets
{ name = "buffer" }, -- text within current buffer
{ name = "path" }, -- file system paths
}),
-- Configure lspkind for vs-code like pictograms in completion menu
formatting = {
format = lspkind.cmp_format({
maxwidth = 50,
ellipsis_char = "...",
show_labelDetails = true,
}),
},
})
end,
}