Nvim: order completion sources for better priority

This commit is contained in:
Marko Korhonen 2023-03-15 13:26:22 +02:00
parent 74f5f1e3cb
commit 654e0917ca
No known key found for this signature in database
GPG Key ID: A7F78BCB859CD890

View File

@ -1,88 +1,88 @@
return function() return function()
local cmp = require("cmp") local cmp = require("cmp")
local luasnip = require("luasnip") local luasnip = require("luasnip")
if not cmp then if not cmp then
return return
end end
-- Setup git completion source -- Setup git completion source
require("cmp_git").setup() require("cmp_git").setup()
-- Set completeopt to have a better completion experience -- Set completeopt to have a better completion experience
vim.o.completeopt = "menuone,noselect" vim.o.completeopt = "menuone,noselect"
cmp.setup({ cmp.setup({
snippet = { snippet = {
expand = function(args) expand = function(args)
luasnip.lsp_expand(args.body) luasnip.lsp_expand(args.body)
end, end,
}, },
mapping = { mapping = {
["<C-p>"] = cmp.mapping.select_prev_item(), ["<C-p>"] = cmp.mapping.select_prev_item(),
["<C-n>"] = cmp.mapping.select_next_item(), ["<C-n>"] = cmp.mapping.select_next_item(),
["<C-d>"] = cmp.mapping.scroll_docs(-4), ["<C-d>"] = cmp.mapping.scroll_docs(-4),
["<C-f>"] = cmp.mapping.scroll_docs(4), ["<C-f>"] = cmp.mapping.scroll_docs(4),
["<C-Space>"] = cmp.mapping.complete(), ["<C-Space>"] = cmp.mapping.complete(),
["<C-e>"] = cmp.mapping.close(), ["<C-e>"] = cmp.mapping.close(),
["<CR>"] = cmp.mapping.confirm({ ["<CR>"] = cmp.mapping.confirm({
behavior = cmp.ConfirmBehavior.Replace, behavior = cmp.ConfirmBehavior.Replace,
select = true, select = true,
}), }),
["<Tab>"] = function(fallback) ["<Tab>"] = function(fallback)
if cmp.visible() then if cmp.visible() then
cmp.select_next_item() cmp.select_next_item()
elseif luasnip.expand_or_jumpable() then elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump() luasnip.expand_or_jump()
else else
fallback() fallback()
end end
end, end,
["<S-Tab>"] = function(fallback) ["<S-Tab>"] = function(fallback)
if cmp.visible() then if cmp.visible() then
cmp.select_prev_item() cmp.select_prev_item()
elseif luasnip.jumpable(-1) then elseif luasnip.jumpable(-1) then
luasnip.jump(-1) luasnip.jump(-1)
else else
fallback() fallback()
end end
end, end,
}, },
sources = { sources = {
{ name = "buffer" }, { name = "nvim_lsp" },
{ name = "git" }, { name = "nvim_lua" },
{ name = "luasnip" }, { name = "luasnip" },
{ name = "nvim_lsp" }, { name = "git" },
{ name = "nvim_lua" }, { name = "buffer" },
{ name = "path" }, { name = "spell" },
{ name = "spell" }, { name = "path" },
}, },
}) })
-- Enable autopairs when enter is processed -- Enable autopairs when enter is processed
-- on completion -- on completion
local cmp_autopairs = require("nvim-autopairs.completion.cmp") local cmp_autopairs = require("nvim-autopairs.completion.cmp")
cmp.event:on("confirm_done", cmp_autopairs.on_confirm_done()) cmp.event:on("confirm_done", cmp_autopairs.on_confirm_done())
-- `/` cmdline setup. -- `/` cmdline setup.
cmp.setup.cmdline("/", { cmp.setup.cmdline("/", {
mapping = cmp.mapping.preset.cmdline(), mapping = cmp.mapping.preset.cmdline(),
sources = { sources = {
{ name = "buffer" }, { name = "buffer" },
}, },
}) })
-- `:` cmdline setup. -- `:` cmdline setup.
cmp.setup.cmdline(":", { cmp.setup.cmdline(":", {
mapping = cmp.mapping.preset.cmdline(), mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources({ sources = cmp.config.sources({
{ name = "path" }, { name = "path" },
}, { }, {
{ {
name = "cmdline", name = "cmdline",
option = { option = {
ignore_cmds = { "Man", "!" }, ignore_cmds = { "Man", "!" },
}, },
}, },
}), }),
}) })
end end