diff --git a/config/nvim/lua/user/plugins/completion.lua b/config/nvim/lua/user/plugins/completion.lua index a7bda24..a985fb7 100644 --- a/config/nvim/lua/user/plugins/completion.lua +++ b/config/nvim/lua/user/plugins/completion.lua @@ -1,77 +1,107 @@ local config = function() local cmp = require('cmp') - local mapping = { - [''] = { - i = function() - if cmp.visible() then - cmp.select_next_item { behavior = cmp.SelectBehavior.Select } - else - cmp.complete() - end - end, - }, + local map = setmetatable({ + complete_or_select_next = function() + if cmp.visible() then + cmp.select_next_item { behavior = cmp.SelectBehavior.Select } + else + cmp.complete() + end + end, - [''] = { - i = function() - if cmp.visible() then - cmp.select_prev_item { behavior = cmp.SelectBehavior.Select } - else - cmp.complete() - end - end, - }, + complete_or_select_previous = function() + if cmp.visible() then + cmp.select_prev_item { behavior = cmp.SelectBehavior.Select } + else + cmp.complete() + end + end, - [''] = { i = cmp.mapping.select_next_item { behavior = cmp.SelectBehavior.Select } }, - [''] = { i = cmp.mapping.select_prev_item { behavior = cmp.SelectBehavior.Select } }, + tab_completion = function(fallback) + if cmp.visible() then + cmp.complete_common_string() + elseif vim.fn.mode() == 'c' then + cmp.complete() + else + fallback() + end + end, + }, { + -- Make `func` available in [i]nsert and [c]mdline modes. + __call = function(_, func) return { i = func, c = func } end + }) - [''] = { i = cmp.mapping.scroll_docs(4) }, - [''] = { i = cmp.mapping.scroll_docs(-4) }, + local keymap = { + [''] = map(map.complete_or_select_next), + [''] = map(map.complete_or_select_previous), - [''] = { i = cmp.mapping.abort() }, - [''] = { i = cmp.mapping.confirm { select = true } }, - [''] = { - i = function(fallback) - if cmp.visible() then - cmp.confirm { select = true } - else - fallback() - end - end, - }, + [''] = map(cmp.mapping.select_next_item { behavior = cmp.SelectBehavior.Select }), + [''] = map(cmp.mapping.select_prev_item { behavior = cmp.SelectBehavior.Select }), + + [''] = map(cmp.mapping.scroll_docs(4)), + [''] = map(cmp.mapping.scroll_docs(4)), + [''] = map(cmp.mapping.scroll_docs(-4)), + [''] = map(cmp.mapping.scroll_docs(-4)), + + [''] = map(cmp.mapping.abort()), + [''] = map(cmp.mapping.confirm { select = true }), + [''] = map(map.tab_completion), } cmp.setup { - mapping = mapping, + mapping = keymap, + + snippet = { + expand = function(_ --[[args]]) + -- require('luasnip').lsp_expand(args.body) TODO enable + end, + }, + + formatting = { + format = require('lspkind').cmp_format { + mode = 'symbol_text', + menu = { + buffer = "[buf]", + nvim_lsp = "[LSP]", + nvim_lua = "[lua]", + path = "[path]", + }, + }, + }, sources = cmp.config.sources({ - { name = 'nvim_lsp' }, { name = 'nvim_lua' }, + { name = 'nvim_lsp' }, + -- { name = "luasnip" }, TODO enable }, { { name = 'path' }, { name = 'buffer', keyword_length = 5 }, }), - formatting = (function() - local ok, lspkind = pcall(require, 'lspkind') - if not ok then return {} end - return { - format = lspkind.cmp_format { - mode = 'symbol_text', - menu = { - buffer = "[buf]", - nvim_lsp = "[LSP]", - nvim_lua = "[lua]", - path = "[path]", - }, - }, - } - end)(), + window = { + completion = cmp.config.window.bordered(), + documentation = cmp.config.window.bordered(), + }, experimental = { ghost_text = true, }, } + + cmp.setup.cmdline(':', { + mapping = keymap, + + completion = { + autocomplete = false, + }, + + sources = cmp.config.sources({ + { name = 'path' } + }, { + { name = 'cmdline' } + }), + }) end return { @@ -83,6 +113,7 @@ return { 'hrsh7th/cmp-nvim-lua', 'hrsh7th/cmp-path', 'hrsh7th/cmp-buffer', + 'hrsh7th/cmp-cmdline', 'onsails/lspkind-nvim', }, }