From 2c087db1a77227c4aa0400fea8053c803a1d8ebe Mon Sep 17 00:00:00 2001 From: Simon Lasbrugnas Date: Mon, 11 Nov 2024 04:11:20 +0100 Subject: [PATCH] fix(cmp): formatting using lspkind --- lua/custom/completion.lua | 53 +++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/lua/custom/completion.lua b/lua/custom/completion.lua index f8b476c..49e3aac 100644 --- a/lua/custom/completion.lua +++ b/lua/custom/completion.lua @@ -1,9 +1,9 @@ local cmp = require("cmp") -local defaults = require("cmp.config.default")() local lspkind = require "lspkind" require("custom.snippets") cmp.setup({ + preselect = cmp.PreselectMode.None, completion = { completeopt = "menu,menuone,noinsert", }, @@ -13,6 +13,13 @@ cmp.setup({ [""] = cmp.mapping.scroll_docs(-4), [""] = cmp.mapping.scroll_docs(4), [""] = cmp.mapping.abort(), + [''] = function() + if cmp.visible_docs() then + cmp.close_docs() + else + cmp.open_docs() + end + end, [""] = cmp.mapping.confirm({ behavior = cmp.ConfirmBehavior.Insert, select = true, @@ -25,28 +32,30 @@ cmp.setup({ end, }, sources = cmp.config.sources({ + { name = "luasnip", priority = 100 }, { name = "nvim_lsp" }, { name = "path" }, - { name = "luasnip" }, - { name = "buffer" }, + { name = "buffer", group_index = 10 }, + { name = "lazydev", group_index = 0, } }), - sorting = defaults.sorting, - format = lspkind.cmp_format({ - mode = 'text_symbol', -- show only symbol annotations - maxwidth = { - -- prevent the popup from showing more than provided characters (e.g 50 will not show more than 50 characters) - -- can also be a function to dynamically calculate max width such as - -- menu = function() return math.floor(0.45 * vim.o.columns) end, - menu = 50, -- leading text (labelDetails) - abbr = 50, -- actual suggestion item - }, - ellipsis_char = '...', -- when popup menu exceed maxwidth, the truncated part would show ellipsis_char instead (must define maxwidth first) - show_labelDetails = true, -- show labelDetails in menu. Disabled by default - - -- The function below will be called before any actual modifications from lspkind - -- so that you can provide more controls on popup customization. (See [#30](https://github.com/onsails/lspkind-nvim/pull/30)) - before = function(_, vim_item) - return vim_item - end - }) + formatting = { + expandable_indicator = true, + fields = { "kind", "abbr", "menu" }, + format = lspkind.cmp_format({ + mode = 'symbol_text', + maxwidth = 50, + ellipsis_char = '...', + show_labelDetails = true, + before = function(entry, vim_item) + vim_item.menu = ({ + buffer = "[Buffer]", + nvim_lsp = "[LSP]", + luasnip = "[Snippet]", + path = "[Path]", + lazydev = "[Lazy]", + })[entry.source.name] + return vim_item + end + }) + } })