diff --git a/.config/nvim/lua/plugins/config/cmp.lua b/.config/nvim/lua/plugins/config/cmp.lua index 589f40fc..901b8e17 100644 --- a/.config/nvim/lua/plugins/config/cmp.lua +++ b/.config/nvim/lua/plugins/config/cmp.lua @@ -3,22 +3,15 @@ if not cmp_status_ok then return end -local function border(hl_name) - return { - { "╭", hl_name }, - { "─", hl_name }, - { "╮", hl_name }, - { "│", hl_name }, - { "╯", hl_name }, - { "─", hl_name }, - { "╰", hl_name }, - { "│", hl_name }, - } +local luasnip_status_ok, luasnip = pcall(require, 'luasnip') +if not luasnip_status_ok then + return end -local check_backspace = function() - local col = vim.fn.col(".") - 1 - return col == 0 or vim.fn.getline("."):sub(col, col):match("%s") +local has_words_before = function() + 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 cmp_window = require "cmp.utils.window" @@ -33,23 +26,25 @@ end cmp.setup({ window = { completion = { - border = border "CmpBorder", - winhighlight = "Normal:CmpPmenu,CursorLine:PmenuSel,Search:None", - }, - documentation = { - border = border "CmpDocBorder", + winhighlight = "Normal:Pmenu,FloatBorder:Pmenu,Search:None", + col_offset = -3, + side_padding = 0, }, }, + formatting = { + fields = { "kind", "abbr", "menu" }, + format = function(entry, vim_item) + local kind = require("lspkind").cmp_format({ mode = "symbol_text", maxwidth = 50 })(entry, vim_item) + local strings = vim.split(kind.kind, "%s", { trimempty = true }) + kind.kind = " " .. (strings[1] or "") .. " " + kind.menu = " (" .. (strings[2] or "") .. ")" + + return kind + end, + }, snippet = { expand = function(args) - require("luasnip").lsp_expand(args.body) - end, - }, - formatting = { - format = function(_, vim_item) - local icons = require('config.iconlist').kind - vim_item.kind = string.format("%s %s", icons[vim_item.kind], vim_item.kind) - return vim_item + luasnip.lsp_expand(args.body) end, }, mapping = { @@ -62,35 +57,29 @@ cmp.setup({ i = cmp.mapping.abort(), c = cmp.mapping.close(), }), - [""] = cmp.mapping.confirm({ select = true }), [""] = cmp.mapping(function(fallback) if cmp.visible() then cmp.select_next_item() - elseif require('luasnip').expandable() then - require('luasnip').expand() - elseif require('luasnip').expand_or_jumpable() then - require('luasnip').expand_or_jump() - elseif check_backspace() then - fallback() + -- You could replace the expand_or_jumpable() calls with expand_or_locally_jumpable() + -- they 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", - }), + end, { "i", "s" }), + [""] = cmp.mapping(function(fallback) if cmp.visible() then cmp.select_prev_item() - elseif require('luasnip').jumpable(-1) then - require('luasnip').jump(-1) + elseif luasnip.jumpable(-1) then + luasnip.jump(-1) else fallback() end - end, { - "i", - "s", - }), + end, { "i", "s" }), }, sources = { { name = "nvim_lsp" },