local icons = require("fschauen.util.icons") local make_keymap = function(cmp) local if_visible = function(yes, no) no = no or function(fallback) fallback() end return function(fallback) if cmp.visible() then yes(fallback) else no(fallback) end end end local select = { behavior = cmp.SelectBehavior.Select } local next_or_complete = if_visible(cmp.mapping.select_next_item(select), cmp.mapping.complete()) local prev_or_complete = if_visible(cmp.mapping.select_prev_item(select), cmp.mapping.complete()) local next = if_visible(cmp.mapping.select_next_item(select)) local prev = if_visible(cmp.mapping.select_prev_item(select)) local abort = if_visible(cmp.mapping.abort()) local confirm = if_visible(cmp.mapping.confirm { select = true }) local confirm_or_complete = if_visible(cmp.mapping.confirm { select = true }, cmp.mapping.complete()) local scroll_docs_down = cmp.mapping.scroll_docs(3) local scroll_docs_up = cmp.mapping.scroll_docs(-3) -- Mappings that should work in both command line and Insert mode. return { -- stylua: ignore start [""] = { i = next_or_complete, c = next_or_complete }, [""] = { i = prev_or_complete, c = prev_or_complete }, [""] = { i = next_or_complete, c = next }, [""] = { i = prev_or_complete, c = prev }, [""] = { i = next_or_complete, c = next }, [""] = { i = prev_or_complete, c = prev }, [""] = { i = scroll_docs_down, c = scroll_docs_down }, [""] = { i = scroll_docs_up, c = scroll_docs_up }, [""] = { i = abort, c = abort }, [""] = { i = abort, c = abort }, [""] = { i = confirm, c = confirm }, [""] = { i = confirm, c = confirm_or_complete }, -- stylua: ignore end } end return { "hrsh7th/nvim-cmp", dependencies = { "hrsh7th/cmp-nvim-lsp", "hrsh7th/cmp-nvim-lua", "hrsh7th/cmp-path", "hrsh7th/cmp-buffer", "hrsh7th/cmp-cmdline", "onsails/lspkind-nvim", "L3MON4D3/LuaSnip", "saadparwaiz1/cmp_luasnip", }, event = { "CmdlineEnter", "InsertEnter" }, config = function() local cmp = require("cmp") local keymap = make_keymap(cmp) cmp.setup { mapping = keymap, enabled = function() local ctx = require("cmp.config.context") return not ctx.in_treesitter_capture("comment") and not ctx.in_syntax_group("Comment") end, snippet = { expand = function(args) require("luasnip").lsp_expand(args.body) end, }, formatting = { format = require("lspkind").cmp_format { mode = "symbol_text", symbol_map = icons.kind, menu = { buffer = "buf", nvim_lsp = "LSP", nvim_lua = "lua", path = "", }, }, }, sources = cmp.config.sources({ { name = "nvim_lua" }, { name = "nvim_lsp" }, { name = "luasnip" }, }, { { name = "path" }, { name = "buffer", keyword_length = 5 }, }), 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" }, }), }) cmp.setup.filetype("TelescopePrompt", { enabled = false, }) end, }