starter/lua/plugins/cmp.lua
2025-08-12 12:38:34 -03:00

143 lines
4.8 KiB
Lua

-- I've set up a comprehensive completion system using nvim-cmp with the following features:
--
-- 1. Main Completion Sources:
-- - LSP completions (cmp-nvim-lsp)
-- - Snippets (LuaSnip + friendly-snippets)
-- - Buffer words (cmp-buffer)
-- - File paths (cmp-path)
-- - Command line completions (cmp-cmdline)
--
-- 2. Key Mappings:
-- - `<C-n>/<C-p>` - Navigate through completion items
-- - `<C-b>/<C-f>` - Scroll documentation
-- - `<C-Space>` - Trigger completion
-- - `<C-e>` - Close completion window
-- - `<CR>` - Confirm selection
-- - `<S-CR>` - Confirm with replacement
-- - `<Tab>/<S-Tab>` - Navigate completion items or snippets
--
-- 3. Features:
-- - Ghost text preview
-- - Icons for different completion types
-- - Source indicators ([LSP], [Buffer], etc.)
-- - Command-line completion for / ? :
-- - Snippet support with friendly-snippets included
--
-- The configuration is optimized for performance by:
-- - Loading on InsertEnter
-- - Using the latest versions of plugins
-- - Proper lazy loading of snippets
return {
{
"hrsh7th/nvim-cmp",
version = false, -- last release is way too old
event = "InsertEnter",
dependencies = {
"hrsh7th/cmp-nvim-lsp", -- LSP source for nvim-cmp
"hrsh7th/cmp-buffer", -- Buffer completions
"hrsh7th/cmp-path", -- Path completions
"hrsh7th/cmp-cmdline", -- Cmdline completions
"saadparwaiz1/cmp_luasnip", -- Snippet completions
{
"L3MON4D3/LuaSnip",
version = "v2.*",
build = "make install_jsregexp",
dependencies = {
"rafamadriz/friendly-snippets", -- Preconfigured snippets
},
},
},
config = function()
local cmp = require("cmp")
local luasnip = require("luasnip")
-- Load friendly-snippets
require("luasnip.loaders.from_vscode").lazy_load()
cmp.setup({
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body)
end,
},
completion = {
completeopt = "menu,menuone,noinsert",
},
mapping = cmp.mapping.preset.insert({
["<C-n>"] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Insert }),
["<C-p>"] = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.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 }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
["<S-CR>"] = cmp.mapping.confirm({
behavior = cmp.ConfirmBehavior.Replace,
select = true,
}), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
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" }),
}),
sources = cmp.config.sources({
{ name = "nvim_lsp" },
{ name = "luasnip" },
{ name = "buffer" },
{ name = "path" },
}),
formatting = {
format = function(entry, vim_item)
-- Kind icons
vim_item.kind = string.format('%s %s', require("user.icons").kinds[vim_item.kind], vim_item.kind)
-- Source
vim_item.menu = ({
buffer = "[Buffer]",
nvim_lsp = "[LSP]",
luasnip = "[Snippet]",
path = "[Path]",
})[entry.source.name]
return vim_item
end
},
experimental = {
ghost_text = true,
},
})
-- 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 = 'path' }
}, {
{ name = 'cmdline' }
})
})
end,
},
}