nvim/lua/plugins/cmp.lua

179 lines
4.3 KiB
Lua
Raw Normal View History

2022-12-31 17:43:31 +01:00
local M = {
'hrsh7th/nvim-cmp',
2023-01-01 19:36:00 +01:00
-- lazy = false,
2022-12-31 17:43:31 +01:00
dependencies = {
2023-01-01 19:36:00 +01:00
{ 'honza/vim-snippets' },
{ 'dcampos/nvim-snippy' },
{ 'dcampos/cmp-snippy' },
2022-12-31 17:43:31 +01:00
'hrsh7th/cmp-nvim-lsp',
'hrsh7th/cmp-buffer',
'hrsh7th/cmp-cmdline',
'hrsh7th/cmp-path',
2023-01-01 17:04:23 +01:00
'hrsh7th/cmp-calc',
2022-12-31 17:43:31 +01:00
},
event = { 'InsertEnter', 'CmdlineEnter' },
2022-12-31 17:43:31 +01:00
}
function M.config()
local cmp = require 'cmp'
local ELLIPSIS_CHAR = ''
local MAX_LABEL_WIDTH = 20
--[[ local winhighlight = 'NormalFloat:Pmenu,NormalFloat:Pmenu,CursorLine:PmenuSel,Search:None' ]]
local has_words_before = function()
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
end
cmp.setup {
enabled = function()
return vim.api.nvim_buf_get_option(0, 'buftype') ~= 'prompt' or require('cmp_dap').is_dap_buffer()
end,
2022-12-31 17:43:31 +01:00
view = {
entries = {
name = 'custom', -- can be "custom", "wildmenu" or "native"
-- separator = ' | ',
},
},
completion = {
autocomplete = false,
},
snippet = {
expand = function(args)
require('snippy').expand_snippet(args.body)
end,
},
window = {
--[[ completion = {
border = 'rounded',
winhighlight = winhighlight,
},
documentation = {
border = 'rounded',
winhighlight = winhighlight,
}, ]]
completion = cmp.config.window.bordered(),
documentation = cmp.config.window.bordered(),
},
experimental = {
ghost_text = true,
},
formatting = {
fields = { 'menu', 'abbr', 'kind' },
format = function(entry, item)
local menu_icon = {
nvim_lsp = 'λ',
snippy = '',
buffer = 'Ω',
path = '🖫',
}
item.menu = menu_icon[entry.source.name]
local label = item.abbr
local truncated_label = vim.fn.strcharpart(label, 0, MAX_LABEL_WIDTH)
if truncated_label ~= label then
item.abbr = truncated_label .. ELLIPSIS_CHAR
end
return item
end,
},
mapping = {
['<C-k>'] = cmp.mapping.select_prev_item(),
['<C-j>'] = cmp.mapping.select_next_item(),
['<C-b>'] = cmp.mapping(cmp.mapping.scroll_docs(-4), { 'i', 'c' }),
['<C-f>'] = cmp.mapping(cmp.mapping.scroll_docs(4), { 'i', 'c' }),
['<C-Space>'] = cmp.mapping(cmp.mapping.complete(), { 'i', 'c' }),
['<C-e>'] = cmp.mapping {
i = cmp.mapping.abort(),
c = cmp.mapping.close(),
},
['<CR>'] = cmp.mapping.confirm { select = true },
['<Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item { behavior = cmp.SelectBehavior.Insert }
2023-01-01 19:36:00 +01:00
elseif require('snippy').can_expand_or_advance() then
require('snippy').expand_or_advance()
2022-12-31 17:43:31 +01:00
elseif has_words_before() then
cmp.complete()
else
fallback()
end
end, { 'i', 'c', 's' }),
['<S-Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item { behavior = cmp.SelectBehavior.Insert }
2023-01-01 19:36:00 +01:00
elseif require('snippy').can_jump(-1) then
require('snippy').previous()
2022-12-31 17:43:31 +01:00
else
fallback()
end
end, { 'i', 's', 'c' }),
},
2023-01-01 17:04:23 +01:00
sources = {
2022-12-31 17:43:31 +01:00
{ name = 'nvim_lsp' },
{ name = 'snippy' },
{ name = 'path' },
2023-01-19 19:29:40 +01:00
{ name = 'buffer' },
2023-01-01 17:04:23 +01:00
{ name = 'calc' },
2022-12-31 17:43:31 +01:00
},
}
cmp.setup.cmdline(':', {
mapping = {
['<TAB>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
-- cmp.complete()
2022-12-31 17:43:31 +01:00
else
cmp.complete()
end
end, { 'i', 's', 'c' }),
['<S-TAB>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item { behavior = cmp.SelectBehavior.Insert }
else
cmp.complete()
end
end, { 'i', 's', 'c' }),
['<CR>'] = cmp.mapping {
i = cmp.mapping.confirm { behavior = cmp.ConfirmBehavior.Replace, select = false },
c = function(fallback)
if cmp.visible() then
cmp.confirm { behavior = cmp.ConfirmBehavior.Replace, select = false }
else
fallback()
end
end,
},
2022-12-31 17:43:31 +01:00
},
sources = {
{ name = 'cmdline' },
{ name = 'path' },
2023-01-01 17:04:23 +01:00
{ name = 'calc' },
2022-12-31 17:43:31 +01:00
},
view = {
entries = { name = 'wildmenu', separator = ' · ' },
},
})
cmp.setup.cmdline('/', {
completion = { autocomplete = true },
sources = {
2023-01-01 17:04:23 +01:00
{ name = 'buffer' },
2022-12-31 17:43:31 +01:00
},
view = {
entries = { name = 'wildmenu', separator = ' · ' },
},
})
cmp.setup.filetype({ 'dap-repl', 'dapui_watches', 'dapui_hover' }, {
sources = {
{ name = 'dap' },
},
})
2022-12-31 17:43:31 +01:00
end
return M