local cmp = require 'cmp' local context = require 'cmp.config.context' vim.o.completeopt = nil -- Plugin helper local feedkey = function(key, mode) key = vim.api.nvim_replace_termcodes(key, true, true, true) vim.api.nvim_feedkeys(key, mode, true) end -- General setup cmp.setup { snippet = { expand = function(args) vim.fn['vsnip#anonymous'](args.body) end, }, window = { documentation = { border = 'rounded' }, }, formatting = { fields = { 'menu', 'abbr', 'kind' }, format = function(entry, item) item.menu = ({ nvim_lsp = 'λ', vsnip = '⋗', buffer = 'Ω', path = '🖫', })[entry.source.name] return item end, }, preselect = cmp.PreselectMode.None, mapping = { -- Confirm, jump or select with tab [''] = cmp.mapping(function(fallback) if cmp.get_selected_entry() then cmp.confirm() elseif vim.fn['vsnip#jumpable'](1) == 1 then feedkey('(vsnip-jump-next)', '') elseif vim.fn['vsnip#expandable']() == 1 then feedkey('(vsnip-expand)', '') elseif cmp.visible() then cmp.select_next_item() else fallback() end end, { 'i', 's' }), -- Jump back with shift tab [''] = cmp.mapping(function(fallback) if vim.fn['vsnip#jumpable'](-1) == 1 then feedkey('(vsnip-jump-prev)', '') else fallback() end end, { 'i', 's' }), -- Navigate options with ctrl-n and ctrl-p [''] = cmp.mapping(function(fallback) if not cmp.visible() then cmp.complete() else cmp.select_next_item() end end), [''] = cmp.mapping(function(fallback) if cmp.visible() then cmp.select_prev_item() else fallback() end end), }, sources = cmp.config.sources({ { name = 'nvim_lsp' }, { name = 'vsnip' }, { name = 'nvim_lua' }, }, { { name = 'buffer', keyword_length = 5 }, { name = 'path' }, }), completion = { keyword_length = 3 }, -- Disable in comments enabled = function() return not context.in_syntax_group('Comment') end, }