dotfiles/config/nvim/lua/fschauen/plugins/completion.lua

127 lines
3 KiB
Lua

local icons = require("fschauen.util.icons")
local make_keymap = function(cmp)
local if_visible = function(yes, no)
no = no or function(fallback)
fallback()
end
return function(fallback)
if cmp.visible() then
yes(fallback)
else
no(fallback)
end
end
end
local select = { behavior = cmp.SelectBehavior.Select }
local next_or_complete = if_visible(cmp.mapping.select_next_item(select), cmp.mapping.complete())
local prev_or_complete = if_visible(cmp.mapping.select_prev_item(select), cmp.mapping.complete())
local cmp_map = function(rhs, modes)
if modes == nil then
modes = { "i", "c" }
elseif type(modes) ~= "table" then
modes = { modes }
end
return cmp.mapping(rhs, modes)
end
-- Mappings that should work in both command line and Insert mode.
return {
-- stylua: ignore start
["<c-n>"] = cmp_map(next_or_complete),
["<c-p>"] = cmp_map(prev_or_complete),
["<c-j>"] = cmp_map(next_or_complete),
["<c-k>"] = cmp_map(prev_or_complete),
["<s-down>"] = cmp_map(cmp.mapping.scroll_docs( 3)),
["<s-up>"] = cmp_map(cmp.mapping.scroll_docs(-3)),
["<c-e>"] = cmp_map(cmp.mapping.abort()),
["<c-y>"] = cmp_map(cmp.mapping.confirm { select = true }),
-- stylua: ignore end
}
end
return {
"hrsh7th/nvim-cmp",
dependencies = {
"hrsh7th/cmp-nvim-lsp",
"hrsh7th/cmp-nvim-lua",
"hrsh7th/cmp-path",
"hrsh7th/cmp-buffer",
"hrsh7th/cmp-cmdline",
"onsails/lspkind-nvim",
"L3MON4D3/LuaSnip",
"saadparwaiz1/cmp_luasnip",
},
event = { "CmdlineEnter", "InsertEnter" },
config = function()
local cmp = require("cmp")
local keymap = make_keymap(cmp)
cmp.setup {
mapping = keymap,
enabled = function()
local ctx = require("cmp.config.context")
return not ctx.in_treesitter_capture("comment") and not ctx.in_syntax_group("Comment")
end,
snippet = {
expand = function(args)
require("luasnip").lsp_expand(args.body)
end,
},
formatting = {
format = require("lspkind").cmp_format {
mode = "symbol_text",
symbol_map = icons.kind,
menu = {
buffer = "buf",
nvim_lsp = "LSP",
nvim_lua = "lua",
path = "",
},
},
},
sources = cmp.config.sources({
{ name = "nvim_lua" },
{ name = "nvim_lsp" },
{ name = "luasnip" },
}, {
{ name = "path" },
{ name = "buffer", keyword_length = 5 },
}),
window = {
completion = cmp.config.window.bordered(),
documentation = cmp.config.window.bordered(),
},
experimental = { ghost_text = true },
}
cmp.setup.cmdline(":", {
mapping = keymap,
completion = { autocomplete = false },
sources = cmp.config.sources({
{ name = "path" },
}, {
{ name = "cmdline" },
}),
})
cmp.setup.filetype("TelescopePrompt", {
enabled = false,
})
end,
}