From 641373270408b22f95587050dd0f9c8949244197 Mon Sep 17 00:00:00 2001 From: zackartz Date: Sun, 21 Apr 2024 18:49:30 -0400 Subject: [PATCH] 449 current 2024-04-21 18:49:27 24.05.20240421.a8a8401 6.8.6-zen1 * --- .../config/lua/plugins/editor/nvim-cmp.lua | 149 +++++++++++++++--- 1 file changed, 127 insertions(+), 22 deletions(-) diff --git a/modules/home-manager/vim/config/lua/plugins/editor/nvim-cmp.lua b/modules/home-manager/vim/config/lua/plugins/editor/nvim-cmp.lua index ebab573..65f08de 100644 --- a/modules/home-manager/vim/config/lua/plugins/editor/nvim-cmp.lua +++ b/modules/home-manager/vim/config/lua/plugins/editor/nvim-cmp.lua @@ -29,6 +29,54 @@ return { cmp.event:on("confirm_done", cmp_autopairs.on_confirm_done()) + local icons = { + branch = "", + bullet = "•", + o_bullet = "○", + check = "✔", + d_chev = "∨", + ellipses = "…", + file = "╼", + hamburger = "≡", + lock = "", + r_chev = ">", + ballot_x = " ", + up_tri = " ", + info_i = " ", + -- ballot_x = '✘', + -- up_tri = '▲', + -- info_i = '¡', + } + + local function get_lsp_completion_context(completion, source) + local ok, source_name = pcall(function() + return source.source.client.config.name + end) + if not ok then + return nil + end + + if source_name == "tsserver" then + return completion.detail + elseif source_name == "pyright" and completion.labelDetails ~= nil then + return completion.labelDetails.description + elseif source_name == "clang_d" then + local doc = completion.documentation + if doc == nil then + return + end + + local import_str = doc.value + + local i, j = string.find(import_str, '["<].*[">]') + if i == nil then + return + end + + return string.sub(import_str, i, j) + end + end + cmp.setup.cmdline(":", { mapping = cmp.mapping.preset.cmdline(), sources = cmp.config.sources({ @@ -103,29 +151,86 @@ return { end, { "i", "s" }), }, formatting = { - format = lspkind_status_ok and lspkind.cmp_format({ - mode = "symbol", - maxwidth = 25, - ellipsis_char = "...", - before = function(entry, vim_item) - if vim_item.kind == "Color" and entry.completion_item.documentation then - local _, _, r, g, b = - string.find(entry.completion_item.documentation, "^rgb%((%d+), (%d+), (%d+)") - if r then - local color = string.format("%02x", r) - .. string.format("%02x", g) - .. string.format("%02x", b) - local group = "Tw_" .. color - if vim.fn.hlID(group) < 1 then - vim.api.nvim_set_hl(0, group, { fg = "#" .. color }) - end - vim_item.kind_hl_group = group - return vim_item - end + -- format = lspkind_status_ok and lspkind.cmp_format({ + -- mode = "symbol", + -- maxwidth = 25, + -- ellipsis_char = "...", + -- before = function(entry, vim_item) + -- if vim_item.kind == "Color" and entry.completion_item.documentation then + -- local _, _, r, g, b = + -- string.find(entry.completion_item.documentation, "^rgb%((%d+), (%d+), (%d+)") + -- if r then + -- local color = string.format("%02x", r) + -- .. string.format("%02x", g) + -- .. string.format("%02x", b) + -- local group = "Tw_" .. color + -- if vim.fn.hlID(group) < 1 then + -- vim.api.nvim_set_hl(0, group, { fg = "#" .. color }) + -- end + -- vim_item.kind_hl_group = group + -- return vim_item + -- end + -- end + -- return vim_item + -- end, + -- }), + format = function(entry, vim_item) + if not require("cmp.utils.api").is_cmdline_mode() then + local abbr_width_max = 25 + local menu_width_max = 20 + + local choice = require("lspkind").cmp_format({ + ellipsis_char = icons.ellipsis, + maxwidth = abbr_width_max, + mode = "symbol", + })(entry, vim_item) + + choice.abbr = vim.trim(choice.abbr) + + local abbr_width = string.len(choice.abbr) + if abbr_width < abbr_width_max then + local padding = string.rep(" ", abbr_width_max - abbr_width) + vim_item.abbr = choice.abbr .. padding end - return vim_item - end, - }), + + local cmp_ctx = get_lsp_completion_context(entry.completion_item, entry.source) + if cmp_ctx ~= nil and cmp_ctx ~= "" then + choice.menu = cmp_ctx + else + choice.menu = "" + end + + local menu_width = string.len(choice.menu) + if menu_width > menu_width_max then + choice.menu = vim.fn.strcharpart(choice.menu, 0, menu_width_max - 1) + choice.menu = choice.menu .. icons.ellipses + else + local padding = string.rep(" ", menu_width_max - menu_width) + choice.menu = padding .. choice.menu + end + + return choice + else + local abbr_width_min = 20 + local abbr_width_max = 50 + + local choice = require("lspkind").cmp_format({ + ellipsis_char = icons.ellipses, + maxwidth = abbr_width_max, + mode = "symbol", + })(entry, vim_item) + + choice.abbr = vim.trim(choice.abbr) + + local abbr_width = string.len(choice.abbr) + if abbr_width < abbr_width_min then + local padding = string.rep(" ", abbr_width_min - abbr_width) + vim_item.abbr = choice.abbr .. padding + end + + return choice + end + end, }, } end,