neovim: cmp tweaks

This commit is contained in:
Daniel Lundin 2022-12-28 12:23:25 +01:00
parent cae9db14d4
commit d4d72b615f
3 changed files with 350 additions and 366 deletions

View file

@ -4,10 +4,10 @@ local M = {
"andersevenrud/cmp-tmux", "andersevenrud/cmp-tmux",
"hrsh7th/cmp-nvim-lsp", "hrsh7th/cmp-nvim-lsp",
"hrsh7th/cmp-buffer", "hrsh7th/cmp-buffer",
"hrsh7th/cmp-emoji",
"hrsh7th/cmp-path", "hrsh7th/cmp-path",
"hrsh7th/cmp-cmdline", "hrsh7th/cmp-cmdline",
"hrsh7th/cmp-nvim-lsp-signature-help", "hrsh7th/cmp-nvim-lsp-signature-help",
"L3MON4D3/LuaSnip",
"onsails/lspkind-nvim", "onsails/lspkind-nvim",
"saadparwaiz1/cmp_luasnip", "saadparwaiz1/cmp_luasnip",
}, },
@ -16,26 +16,21 @@ local M = {
function M.config() function M.config()
local cmp = require("cmp") local cmp = require("cmp")
local luasnip = require("luasnip")
local has_words_before = function() local has_words_before = function()
local line, col = unpack(vim.api.nvim_win_get_cursor(0)) local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
end end
vim.o.completeopt = "menuone,noselect"
cmp.setup({ cmp.setup({
formatting = { completion = {
format = require("lspkind").cmp_format({ completeopt = "menu,menuone,noinsert",
with_text = true,
menu = {
buffer = "[Buffer]",
tmux = "[Tmux]",
luasnip = "[LuaSnip]",
nvim_lsp = "[LSP]",
nvim_lua = "[Lua]",
path = "[Path]",
}, },
}),
formatting = {
format = require("lspkind").cmp_format(),
}, },
window = { window = {
@ -49,50 +44,37 @@ function M.config()
snippet = { snippet = {
expand = function(args) expand = function(args)
luasnip.lsp_expand(args.body) require("luasnip").lsp_expand(args.body)
end, end,
}, },
mapping = { mapping = {
["<Up>"] = cmp.mapping(cmp.mapping.select_prev_item(), { "i", "c" }), ["<Up>"] = cmp.mapping(cmp.mapping.select_prev_item(), { "i", "c" }),
["<Down>"] = cmp.mapping(cmp.mapping.select_next_item(), { "i", "c" }), ["<Down>"] = cmp.mapping(cmp.mapping.select_next_item(), { "i", "c" }),
["<C-p>"] = cmp.mapping(cmp.mapping.select_prev_item(), { "i", "c" }), ["<C-d>"] = cmp.mapping.scroll_docs(-2),
["<C-n>"] = cmp.mapping(cmp.mapping.select_next_item(), { "i", "c" }), ["<C-u>"] = cmp.mapping.scroll_docs(2),
["<C-d>"] = cmp.mapping.scroll_docs(-4),
["<C-u>"] = cmp.mapping.scroll_docs(4),
["<C-e>"] = cmp.mapping({ ["<C-e>"] = cmp.mapping({
i = cmp.mapping.abort(), i = cmp.mapping.abort(),
c = cmp.mapping.close(), c = cmp.mapping.close(),
}), }),
["<CR>"] = cmp.mapping(cmp.mapping.confirm({ select = false }), { "i", "c" }), -- ["<CR>"] = cmp.mapping(cmp.mapping.confirm({ select = false }), { "i", "c" }),
["<C-y>"] = cmp.mapping(cmp.mapping.confirm({ select = false }), { "i", "c" }), -- ["<C-Space>"] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }),
["<C-Space>"] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }), ["<CR>"] = cmp.mapping(cmp.mapping.confirm({ select = true })),
["<Tab>"] = cmp.mapping(function(fallback) ["<C-Space>"] = cmp.mapping(cmp.mapping.complete(), {}),
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.expand_or_locally_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" }),
}, },
-- experimental = {
-- ghost_text = {
-- hl_group = "LspCodeLens",
-- },
-- },
sources = cmp.config.sources({ sources = cmp.config.sources({
{ name = "buffer", priority = 1 }, { name = "nvim_lsp" },
{ name = "luasnip", priority = 4 }, { name = "nvim_lsp_signature_help" },
{ name = "copilot", group_index = 5 }, { name = "buffer" },
{ name = "luasnip" },
{ name = "copilot" },
{ {
name = "tmux", name = "tmux",
priority = 2, priority = 2,
@ -101,8 +83,7 @@ function M.config()
trigger_characters = {}, trigger_characters = {},
}, },
}, },
{ name = "nvim_lsp", priority = 3 }, { name = "emoji" },
{ name = "nvim_lsp_signature_help", priority = 4 },
}), }),
}) })
end end

View file

@ -57,6 +57,9 @@ function M.config()
GitSignsChange({ fg = "#FFA726" }), GitSignsChange({ fg = "#FFA726" }),
GitSignsChangeNr({ fg = "#FFE0B2" }), GitSignsChangeNr({ fg = "#FFE0B2" }),
-- LspCodeLens({ fg = "#00ff00", gui = "undercurl" }),
-- LspSignatureActiveParameter({ fg = "#ff0000", bg = "#ffffcc" }),
PMenu({ bg = "#F7F5F0" }), PMenu({ bg = "#F7F5F0" }),
PMenuBorder({ bg = "#F7F5F0", fg = "#886622" }), PMenuBorder({ bg = "#F7F5F0", fg = "#886622" }),
PMenuSel({ fg = "#FFFFFF", bg = "#1976D2" }), PMenuSel({ fg = "#FFFFFF", bg = "#1976D2" }),