local present, cmp = pcall(require, "cmp") if not present then return end 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 vim.opt.completeopt = "menuone,noselect" -- nvim-cmp setup cmp.setup { window = { completion = { border = { "╭", "─", "╮", "│", "╯", "─", "╰", "│" }, winhighlight = "Normal:CmpPmenu,FloatBorder:CmpBorder,CursorLine:PmenuSel,Search:None", }, documentation = { border = { "╭", "─", "╮", "│", "╯", "─", "╰", "│" }, winhighlight = "Normal:CmpPmenu,FloatBorder:CmpBorder,CursorLine:PmenuSel,Search:None", }, }, experimental = { ghost_text = true, }, snippet = { expand = function(args) require("luasnip").lsp_expand(args.body) end, }, mapping = { [""] = cmp.mapping(function(fallback) if require("luasnip").expand_or_jumpable() then require("luasnip").expand_or_jump() else fallback() end end, { "i", "s" }), [""] = cmp.mapping(function(fallback) if require("luasnip").jumpable(-1) then require("luasnip").jump(-1) else fallback() end end, { "i", "s" }), [""] = cmp.mapping.scroll_docs(-4), [""] = cmp.mapping.scroll_docs(4), [""] = cmp.mapping.complete(), [""] = cmp.mapping.close(), [""] = cmp.mapping.confirm { behavior = cmp.ConfirmBehavior.Replace, select = false, }, [""] = cmp.mapping(function(fallback) if cmp.visible() then cmp.select_next_item() 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() else fallback() end end, { "i", "s" }), }, sources = { { name = "nvim_lua" }, { name = "nvim_lsp" }, { name = "buffer" }, { name = "path" }, }, formatting = { format = function(entry, vim_item) local icons = { 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 = "", } vim_item.kind = string.format("%s %s", icons[vim_item.kind], vim_item.kind) vim_item.menu = ({ nvim_lsp = "[LSP]", nvim_lua = "[Lua]", buffer = "[BUF]", })[entry.source.name] return vim_item end, }, }