dotfiles/nvim/lua/config/plugins/nvim-cmp.lua

134 lines
3.3 KiB
Lua
Raw Normal View History

2024-07-08 20:40:10 +02:00
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
2023-07-10 22:33:59 +02:00
-- Reference: https://github.com/hrsh7th/nvim-cmp#recommended-configuration
2024-01-23 22:23:58 +01:00
return function()
2024-07-08 20:40:10 +02:00
local cmp = require 'cmp'
2023-07-10 22:33:59 +02:00
2024-06-13 21:48:16 +02:00
cmp.setup {
2024-07-08 20:40:10 +02:00
-- Expand snippet in buffer
2024-06-13 21:48:16 +02:00
snippet = {
expand = function(args)
vim.snippet.expand(args.body)
end,
},
2024-07-08 20:40:10 +02:00
-- 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,
},
2024-06-13 21:48:16 +02:00
window = {
completion = cmp.config.window.bordered(),
documentation = cmp.config.window.bordered(),
},
mapping = cmp.mapping.preset.insert {
['<C-b>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<C-CR>'] = cmp.mapping.complete(),
['<C-e>'] = cmp.mapping.abort(),
['<CR>'] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
},
2024-07-08 20:40:10 +02:00
sources = cmp.config.sources(
{
{ name = 'nvim_lsp' }
},
{
{ name = 'buffer' }
}
)
2024-06-13 21:48:16 +02:00
}
2023-07-10 22:33:59 +02:00
2024-06-13 21:48:16 +02:00
-- 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' }
}
})
2023-07-10 22:33:59 +02:00
2024-06-13 21:48:16 +02:00
-- 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' }
})
})
2023-07-10 22:33:59 +02:00
end