Make all plugin configs return a function

Simplifies writing the `config = ...` field for each plugin
This commit is contained in:
2023-09-25 16:08:06 +02:00
parent 7739ec5591
commit ac35b77c87
33 changed files with 747 additions and 618 deletions
+147 -141
View File
@@ -16,9 +16,7 @@
-- https://github.com/hrsh7th/nvim-cmp
local module_name = "plugins.config.cmp"
local has_words_before = function ()
local function has_words_before()
unpack = unpack or table.unpack
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0
@@ -27,149 +25,157 @@ local has_words_before = function ()
:match("%s") == nil
end
local utils = require("utils")
local cmp = require("cmp")
local luasnip = require("luasnip")
local function setup()
local module_name = "plugins.config.cmp"
local utils = require("utils")
local cmp = require("cmp")
local luasnip = require("luasnip")
local lspkind
utils.try_require("lspkind", module_name, function (module)
lspkind = module
end)
local lspkind
utils.try_require("lspkind", module_name, function (module)
lspkind = module
end)
cmp.setup({
enabled = function ()
-- disable completion in comments
local context = require "cmp.config.context"
-- keep command mode completion enabled when cursor is in a comment
if vim.api.nvim_get_mode().mode == "c" then
return true
else
return not context.in_treesitter_capture("comment") and
not context.in_syntax_group("Comment")
end
end,
completion = { keyword_length = 3, },
snippet = {
expand = function (args)
luasnip.lsp_expand(args.body)
end,
},
formatting = {
format = function (entry, vim_item)
if lspkind then
vim_item = lspkind.cmp_format({
mode = "symbol",
maxwidth = 50,
ellipsis_char = "...",
before = function (_, item)
item.dup = 0 -- remove duplicates, see nvim-cmp #511
return item
end,
})(entry, vim_item)
cmp.setup({
enabled = function ()
-- disable completion in comments
local context = require "cmp.config.context"
-- keep command mode completion enabled when cursor is in a comment
if vim.api.nvim_get_mode().mode == "c" then
return true
else
return not context.in_treesitter_capture("comment") and
not context.in_syntax_group("Comment")
end
return vim_item
end,
},
experimental = { ghost_text = true, },
mapping = {
["<C-p>"] = cmp.mapping.select_prev_item(),
["<C-n>"] = cmp.mapping.select_next_item(),
["<C-d>"] = cmp.mapping.scroll_docs(-4),
["<C-f>"] = cmp.mapping.scroll_docs(4),
["<CR>"] = cmp.mapping(
function (fallback)
if cmp.visible() then
cmp.confirm(
{
behavior = cmp.ConfirmBehavior.Replace,
select = true,
}
)
else
fallback()
completion = { keyword_length = 3, },
snippet = {
expand = function (args)
luasnip.lsp_expand(args.body)
end,
},
formatting = {
format = function (entry, vim_item)
if lspkind then
vim_item = lspkind.cmp_format({
mode = "symbol",
maxwidth = 50,
ellipsis_char = "...",
before = function (_, item)
item.dup = 0 -- remove duplicates, see nvim-cmp #511
return item
end,
})(entry, vim_item)
end
end, { "i", "s", }
),
["<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", }
),
["<Down>"] = cmp.mapping(
function (fallback)
if cmp.visible() then
cmp.select_next_item()
else
fallback()
end
end, { "i", "s", }
),
["<Up>"] = cmp.mapping(
function (fallback)
if cmp.visible() then
cmp.select_prev_item()
else
fallback()
end
end, { "i", "s", }
),
},
sources = {
{ name = "nvim_lsp", },
{ name = "luasnip", },
{ name = "nvim_lua", },
{ name = "orgmode", },
{ name = "path", },
-- { name = 'buffer' },
},
})
cmp.setup.cmdline(
"/",
{
mapping = cmp.mapping.preset.cmdline(),
sources = { { name = "buffer", }, },
}
)
return vim_item
end,
},
experimental = { ghost_text = true, },
mapping = {
["<C-p>"] = cmp.mapping.select_prev_item(),
["<C-n>"] = cmp.mapping.select_next_item(),
["<C-d>"] = cmp.mapping.scroll_docs(-4),
["<C-f>"] = cmp.mapping.scroll_docs(4),
["<CR>"] = cmp.mapping(
function (fallback)
if cmp.visible() then
cmp.confirm(
{
behavior = cmp.ConfirmBehavior.Replace,
select = true,
}
)
else
fallback()
end
end, { "i", "s", }
),
["<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", }
),
["<Down>"] = cmp.mapping(
function (fallback)
if cmp.visible() then
cmp.select_next_item()
else
fallback()
end
end, { "i", "s", }
),
["<Up>"] = cmp.mapping(
function (fallback)
if cmp.visible() then
cmp.select_prev_item()
else
fallback()
end
end, { "i", "s", }
),
},
sources = {
{ name = "nvim_lsp", },
{ name = "luasnip", },
{ name = "nvim_lua", },
{ name = "orgmode", },
{ name = "path", },
-- { name = 'buffer' },
},
})
cmp.setup.cmdline(
":",
{
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources(
{ { name = "path", }, },
{ { name = "cmdline", option = { ignore_cmds = { "Man", "!", }, }, }, }
),
}
)
cmp.setup.cmdline(
"/",
{
mapping = cmp.mapping.preset.cmdline(),
sources = { { name = "buffer", }, },
}
)
utils.try_require(
"nvim-autopairs.completion.cmp",
module_name,
function (cmp_autopairs)
cmp.event:on(
"confirm_done",
cmp_autopairs.on_confirm_done()
)
end
)
cmp.setup.cmdline(
":",
{
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources(
{ { name = "path", }, },
{ {
name = "cmdline",
option = { ignore_cmds = { "Man", "!", }, },
}, }
),
}
)
utils.try_require(
"nvim-autopairs.completion.cmp",
module_name,
function (cmp_autopairs)
cmp.event:on(
"confirm_done",
cmp_autopairs.on_confirm_done()
)
end
)
end
return setup