local cmp_kinds = { 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 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 local cmp = require("cmp") cmp.setup({ enabled = function() -- disable completion in comments local context = require("cmp.config.context") -- keep command mode completion enabled when cursor is in a comment if vim.api.nvim_get_mode().mode == "c" then return true else return not context.in_treesitter_capture("comment") and not context.in_syntax_group("Comment") end end, snippet = { expand = function(args) require("luasnip").lsp_expand(args.body) end, }, formatting = { format = function(_, vim_item) vim_item.kind = (cmp_kinds[vim_item.kind] or "") .. vim_item.kind return vim_item end, }, mapping = { [""] = cmp.mapping.scroll_docs(-4), [""] = cmp.mapping.scroll_docs(4), [""] = cmp.mapping(function(_) return vim.fn.pumvisible() == 1 and cmp.close() or cmp.complete() end), [""] = cmp.mapping({ i = cmp.mapping.abort(), c = cmp.mapping.close(), }), [""] = cmp.mapping.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = true, }), [""] = cmp.mapping(function(fallback) if cmp.visible() then cmp.select_next_item() elseif require("luasnip").expand_or_locally_jumpable() then vim.fn.feedkeys(vim.api.nvim_replace_termcodes("luasnip-expand-or-jump", true, true, true), "") elseif has_words_before() then cmp.complete() else fallback() end end, { "i", "s" }), [""] = cmp.mapping(function(fallback) if cmp.visible() then cmp.select_prev_item() elseif require("luasnip").jumpable(-1) then vim.fn.feedkeys(vim.api.nvim_replace_termcodes("luasnip-jump-prev", true, true, true), "") else fallback() end end, { "i", "s" }), }, sources = cmp.config.sources({ { name = "nvim_lsp", max_item_count = 20, priority_weight = 100 }, { name = "luasnip", priority_weight = 120 }, { name = "buffer", max_item_count = 5, priority_weight = 80 }, { name = "path", max_item_count = 10, priority_weight = 100 }, { name = "nvim_lsp_signature_help" }, }), view = { entries = "native" }, })