local cmp = require("cmp") local skkeleton = require("rc.skkeleton") local ui = require("rc.ui") local opts = {} opts.enabled = function() if skkeleton.is_enabled() then return false end return true end opts.sources = { { name = "nvim_lsp" }, { name = "path" }, { name = "buffer" }, } opts.completion = { completeopt = vim.o.completeopt, } local function has_words_before() local cursor = vim.api.nvim_win_get_cursor(0) local line, col = cursor[1], cursor[2] return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil end local mapping = cmp.mapping opts.mapping = mapping.preset.insert({ [""] = mapping(function(fallback) if cmp.visible() then cmp.select_next_item() elseif vim.snippet.active({ direction = 1 }) then vim.snippet.jump(1) elseif vim.lsp.inline_completion.get() then return elseif has_words_before() then cmp.complete() else fallback() end end, { "i", "c", "s" }), [""] = mapping(function(fallback) if cmp.visible() then cmp.select_prev_item() elseif vim.snippet.active({ direction = -1 }) then vim.snippet.jump(-1) else fallback() end end, { "i", "c", "s" }), [""] = mapping.confirm({ select = true }), [""] = mapping.abort(), [""] = mapping.scroll_docs(-4), [""] = mapping.scroll_docs(4), }) opts.snippet = { expand = function(args) vim.snippet.expand(args.body) end, } opts.window = { completion = { winhighlight = "Normal:Pmenu,CursorLine:PmenuSel,FloatBorder:FloatBorder,Search:None", col_offset = -3, side_padding = 0, scrollbar = true, scrolloff = 8, winblend = 11, }, documentation = { winhighlight = "Normal:Pmenu,FloatBorder:FloatBorder,Search:None", winblend = 11, }, } opts.formatting = { fields = { "kind", "abbr", "menu" }, ---@type fun(entry, vim_item): any format = function(_, vim_item) vim_item.kind = " " .. ui.kind[vim_item.kind] .. "⠀" vim_item.abbr = ui.truncate(vim_item.abbr:gsub(vim.pesc("..."), ui.ellipsis), 50) do local menu = vim_item.menu or "" if vim.bo.filetype == "rust" then menu = menu:gsub("%(use ([^%)]+)%)%s*(.*)", "%2 (%1)") :gsub("%(alias ([^%)]+)%)%s*(.*)", "%2 (alias %1)") :gsub("%(as [^%)]+%)", "") :gsub("unsafe ", "") :gsub("const ", "") end vim_item.menu = ui.truncate(vim.fn.trim(menu), 70) end return vim_item end, } cmp.setup(opts) cmp.setup.cmdline(":", { mapping = cmp.mapping.preset.cmdline(), sources = cmp.config.sources({ { name = "path" }, }, { { name = "cmdline" }, }), }) cmp.setup.cmdline({ "/", "?" }, { mapping = cmp.mapping.preset.cmdline(), sources = { { name = "buffer" }, }, }) -- if border ~= 'none' then -- local thumb = {} -- for _ = 1, vim.o.lines do -- table.insert(thumb, '┃') -- end -- require('cmp.utils.buffer').cache = setmetatable({}, { -- ---@param name string|integer -- ---@param buf buffer -- __newindex = function(t, name, buf) -- if type(name) == 'string' and name:find('thumb_buf') then -- vim.api.nvim_buf_set_lines(buf, 0, 1, false, thumb) -- end -- rawset(t, name, buf) -- end, -- }) -- end