local cmp = require("cmp") local lspkind = require("lspkind") local luasnip = require("luasnip") local has_words_before = function() local unpack = unpack or table.unpack 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 base_mapping = { [""] = cmp.mapping.scroll_docs(-4), [""] = cmp.mapping.scroll_docs(4), [""] = cmp.mapping.complete {}, [""] = cmp.mapping.abort(), [""] = cmp.mapping.confirm { select = true }, } local buffer_mapping = vim.tbl_deep_extend("force", base_mapping, { [""] = cmp.mapping(function(fallback) if cmp.visible() then cmp.select_next_item() -- You could replace the expand_or_jumpable() calls with expand_or_locally_jumpable() -- that way you will only jump inside the snippet region elseif luasnip.expand_or_jumpable() then luasnip.expand_or_jump() 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 luasnip.jumpable(-1) then luasnip.jump(-1) else fallback() end end, { "i", "s" }), }) local config = { formatting = { format = lspkind.cmp_format { mode = "symbol_text", maxwidth = 50, ellipsis_char = "…", menu = { nvim_lsp = "[LSP]", nvim_lsp_signature_help = "[LSP]", nvim_lsp_document_symbol = "[LSP]", luasnip = "[Snippet]", buffer = "[Buffer]", path = "[Path]", nvim_lua = "[Lua]", emoji = "[Emoji]", }, }, }, completion = { completeopt = "menu,menuone", }, window = { completion = { side_padding = 0, scrollbar = false, }, documentation = {}, }, snippet = { expand = function(args) require("luasnip").lsp_expand(args.body) end, }, mapping = cmp.mapping.preset.insert(buffer_mapping), sources = cmp.config.sources { { name = "luasnip" }, { name = "nvim_lsp" }, { name = "neorg" }, { name = "buffer" }, { name = "fuzzy_path" }, }, } local cmdline_config = { mapping = cmp.mapping.preset.cmdline(base_mapping), completion = { completeopt = "menu,menuone,noselect", }, sources = cmp.config.sources({ { name = "fuzzy_path" }, }, { { name = "cmdline" }, }), } local search_config = { mapping = cmp.mapping.preset.cmdline(base_mapping), completion = { completeopt = "menu,menuone,noselect", }, sources = { { name = "buffer" }, }, } cmp.setup(config) cmp.setup.cmdline("/", search_config) cmp.setup.cmdline("?", search_config) cmp.setup.cmdline(":", cmdline_config)