local kind_icon = { Text = '', Method = '󰆧', Function = '󰊕', Constructor = '', Field = '󰇽', Variable = '󰂡', Class = '󰠱', Interface = '', Module = '', Property = '󰜢', Unit = '', Value = '󰎠', Enum = '', Keyword = '󰌋', Snippet = '', Color = '󰏘', File = '󰈙', Reference = '', Folder = '󰉋', EnumMember = '', Constant = '󰏿', Struct = '', Event = '', Operator = '󰆕', TypeParameter = '󰅲', } local menu_icon = { -- name buffer = '', -- lsp ['rust-analyzer'] = '', clangd = '', cssls = '', hls = '', html = '', jsonls = '', lua_ls = '', nixd = '󱄅', ocamllsp = '', svelte = '', tsserver = '', } ---@param source cmp.Source ---@return string local function get_menu_icon(source) local name = source.name local lsp = ( source.source.client and source.source.client.config and source.source.client.config.name ) return menu_icon[lsp] or menu_icon[name] or lsp or name end -- Reference: https://github.com/hrsh7th/nvim-cmp#recommended-configuration return function() local cmp = require 'cmp' cmp.setup { -- Expand snippet in buffer snippet = { expand = function(args) vim.snippet.expand(args.body) end, }, -- Completion menu appearance -- https://github.com/hrsh7th/nvim-cmp/wiki/Menu-Appearance formatting = { expandable_indicator = true, fields = { 'kind', 'abbr', 'menu' }, format = function(entry, item) item.kind = kind_icon[item.kind] or ' ' item.menu = get_menu_icon(entry.source) -- Get the completion entry text shown in the completion window. local content = item.abbr -- Get the width of the current window. local win_width = vim.api.nvim_win_get_width(0) local max_width = math.floor(win_width * 0.2) if #content > max_width then item.abbr = vim.fn.strcharpart(content, 0, max_width - 3) .. "..." else item.abbr = content .. (" "):rep(max_width - #content) end return item end, }, window = { completion = cmp.config.window.bordered(), documentation = cmp.config.window.bordered(), }, mapping = cmp.mapping.preset.insert { [''] = cmp.mapping.scroll_docs(-4), [''] = cmp.mapping.scroll_docs(4), [''] = cmp.mapping.complete(), [''] = cmp.mapping.abort(), [''] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items. }, sources = cmp.config.sources( { { name = 'nvim_lsp' } }, { { name = 'buffer' } } ) } -- Use buffer source for `/` and `?` (if you enabled `native_menu`, this won't work anymore). cmp.setup.cmdline({ '/', '?' }, { mapping = cmp.mapping.preset.cmdline(), sources = { { name = 'buffer' } } }) -- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore). cmp.setup.cmdline(':', { mapping = cmp.mapping.preset.cmdline(), sources = cmp.config.sources({ { name = 'path' } }, { { name = 'cmdline' } }) }) end