From 9ece2e5369de46962422837be3dce3b8e889805d Mon Sep 17 00:00:00 2001 From: Christian Chiarulli Date: Tue, 7 Sep 2021 19:23:14 -0400 Subject: feat: compe -> cmp (#1496) --- lua/config/defaults.lua | 16 +++--- lua/core/autopairs.lua | 52 ++++++++++-------- lua/core/builtins/init.lua | 2 +- lua/core/cmp.lua | 123 +++++++++++++++++++++++++++++++++++++++++ lua/core/compe.lua | 133 --------------------------------------------- lua/lsp/init.lua | 6 ++ lua/lsp/kind.lua | 31 +++++++++++ lua/plugins.lua | 44 ++++++--------- 8 files changed, 215 insertions(+), 192 deletions(-) create mode 100644 lua/core/cmp.lua delete mode 100644 lua/core/compe.lua create mode 100644 lua/lsp/kind.lua (limited to 'lua') diff --git a/lua/config/defaults.lua b/lua/config/defaults.lua index 0f70a281..24455819 100644 --- a/lua/config/defaults.lua +++ b/lua/config/defaults.lua @@ -37,30 +37,30 @@ lvim = { lsp = { completion = { item_kind = { - "  (Text) ", + "  (Text) ", "  (Method)", "  (Function)", "  (Constructor)", - " ﴲ (Field)", - "[] (Variable)", + "  (Field)", + "  (Variable)", "  (Class)", " ﰮ (Interface)", "  (Module)", - " 襁 (Property)", - "  (Unit)", + "  (Property)", + " 塞 (Unit)", "  (Value)", " 練 (Enum)", "  (Keyword)", "  (Snippet)", "  (Color)", - "  (File)", + "  (File)", "  (Reference)", "  (Folder)", "  (EnumMember)", " ﲀ (Constant)", - " ﳤ (Struct)", + "  (Struct)", "  (Event)", - "  (Operator)", + "  (Operator)", "  (TypeParameter)", }, }, diff --git a/lua/core/autopairs.lua b/lua/core/autopairs.lua index 24aa1875..b728cbc1 100644 --- a/lua/core/autopairs.lua +++ b/lua/core/autopairs.lua @@ -20,36 +20,44 @@ function M.config() end M.setup = function() - -- skip it, if you use another global object - _G.MUtils = {} local autopairs = require "nvim-autopairs" local Rule = require "nvim-autopairs.rule" - - vim.g.completion_confirm_key = "" - MUtils.completion_confirm = function() - if vim.fn.pumvisible() ~= 0 then - if vim.fn.complete_info()["selected"] ~= -1 then - return vim.fn["compe#confirm"](autopairs.esc "") - else - return autopairs.esc "" - end - else - return autopairs.autopairs_cr() - end - end - - if package.loaded["compe"] then - require("nvim-autopairs.completion.compe").setup { - map_cr = lvim.builtin.autopairs.map_cr, - map_complete = lvim.builtin.autopairs.map_complete, - } - end + local cond = require "nvim-autopairs.conds" autopairs.setup { check_ts = lvim.builtin.autopairs.check_ts, ts_config = lvim.builtin.autopairs.ts_config, } + -- vim.g.completion_confirm_key = "" + + autopairs.add_rule(Rule("$$", "$$", "tex")) + autopairs.add_rules { + Rule("$", "$", { "tex", "latex" }) -- don't add a pair if the next character is % + :with_pair(cond.not_after_regex_check "%%") -- don't add a pair if the previous character is xxx + :with_pair(cond.not_before_regex_check("xxx", 3)) -- don't move right when repeat character + :with_move(cond.none()) -- don't delete if the next character is xx + :with_del(cond.not_after_regex_check "xx") -- disable add newline when press + :with_cr(cond.none()), + } + autopairs.add_rules { + Rule("$$", "$$", "tex"):with_pair(function(opts) + print(vim.inspect(opts)) + if opts.line == "aa $$" then + -- don't add pair on that line + return false + end + end), + } + + if package.loaded["cmp"] then + require("nvim-autopairs.completion.cmp").setup { + map_cr = true, -- map on insert mode + map_complete = true, -- it will auto insert `(` after select function or method item + auto_select = true, -- automatically select the first item + } + end + require("nvim-treesitter.configs").setup { autopairs = { enable = true } } local ts_conds = require "nvim-autopairs.ts-conds" diff --git a/lua/core/builtins/init.lua b/lua/core/builtins/init.lua index 32f96af5..dc9b5ff5 100644 --- a/lua/core/builtins/init.lua +++ b/lua/core/builtins/init.lua @@ -4,7 +4,7 @@ local builtins = { "keymappings", "core.which-key", "core.gitsigns", - "core.compe", + "core.cmp", "core.dashboard", "core.dap", "core.terminal", diff --git a/lua/core/cmp.lua b/lua/core/cmp.lua new file mode 100644 index 00000000..7f912544 --- /dev/null +++ b/lua/core/cmp.lua @@ -0,0 +1,123 @@ +local M = {} + +local check_backspace = function() + local col = vim.fn.col "." - 1 + return col == 0 or vim.fn.getline("."):sub(col, col):match "%s" +end + +local function T(str) + return vim.api.nvim_replace_termcodes(str, true, true, true) +end + +local is_emmet_active = function() + local clients = vim.lsp.buf_get_clients() + + for _, client in pairs(clients) do + if client.name == "emmet_ls" then + return true + end + end + return false +end + +M.config = function() + local status_cmp_ok, cmp = pcall(require, "cmp") + if not status_cmp_ok then + return + end + local status_luasnip_ok, luasnip = pcall(require, "luasnip") + if not status_luasnip_ok then + return + end + lvim.builtin.cmp = { + formatting = { + format = function(entry, vim_item) + local icons = require("lsp.kind").icons + vim_item.kind = icons[vim_item.kind] + vim_item.menu = ({ + nvim_lsp = "(LSP)", + emoji = "(Emoji)", + path = "(Path)", + calc = "(Calc)", + cmp_tabnine = "(Tabnine)", + vsnip = "(Snippet)", + luasnip = "(Snippet)", + buffer = "(Buffer)", + })[entry.source.name] + vim_item.dup = ({ + buffer = 1, + path = 1, + nvim_lsp = 0, + })[entry.source.name] or 0 + return vim_item + end, + }, + snippet = { + expand = function(args) + require("luasnip").lsp_expand(args.body) + end, + }, + documentation = { + border = { "╭", "─", "╮", "│", "╯", "─", "╰", "│" }, + }, + sources = { + { name = "nvim_lsp" }, + { name = "path" }, + { name = "luasnip" }, + { name = "cmp_tabnine" }, + { name = "nvim_lua" }, + { name = "buffer" }, + { name = "calc" }, + { name = "emoji" }, + { name = "treesitter" }, + { name = "crates" }, + }, + mapping = { + [""] = cmp.mapping.scroll_docs(-4), + [""] = cmp.mapping.scroll_docs(4), + -- TODO: potentially fix emmet nonsense + [""] = cmp.mapping(function() + if vim.fn.pumvisible() == 1 then + vim.fn.feedkeys(T "", "n") + elseif luasnip.expand_or_jumpable() then + vim.fn.feedkeys(T "luasnip-expand-or-jump", "") + elseif check_backspace() then + vim.fn.feedkeys(T "", "n") + elseif is_emmet_active() then + return vim.fn["cmp#complete"]() + else + vim.fn.feedkeys(T "", "n") + end + end, { + "i", + "s", + }), + [""] = cmp.mapping(function(fallback) + if vim.fn.pumvisible() == 1 then + vim.fn.feedkeys(T "", "n") + elseif luasnip.jumpable(-1) then + vim.fn.feedkeys(T "luasnip-jump-prev", "") + else + fallback() + end + end, { + "i", + "s", + }), + + [""] = cmp.mapping.complete(), + [""] = cmp.mapping.close(), + [""] = cmp.mapping.confirm { + behavior = cmp.ConfirmBehavior.Replace, + select = true, + }, + }, + } + + M.setup = function() + require("luasnip/loaders/from_vscode").lazy_load() + require("cmp").setup(lvim.builtin.cmp) + end +end + +return M diff --git a/lua/core/compe.lua b/lua/core/compe.lua deleted file mode 100644 index 9eb3dcfa..00000000 --- a/lua/core/compe.lua +++ /dev/null @@ -1,133 +0,0 @@ -local M = {} - -M.config = function() - lvim.builtin.compe = { - active = true, - on_config_done = nil, - autocomplete = true, - debug = false, - min_length = 1, - preselect = "enable", - throttle_time = 80, - source_timeout = 200, - incomplete_delay = 400, - max_abbr_width = 100, - max_kind_width = 100, - max_menu_width = 100, - documentation = { - border = "single", - winhighlight = "NormalFloat:CompeDocumentation,FloatBorder:CompeDocumentationBorder", - max_width = 120, - min_width = 60, - max_height = math.floor(vim.o.lines * 0.3), - min_height = 1, - }, - -- documentation = true, - - source = { - path = { kind = "  (Path)" }, - buffer = { kind = "  (Buffer)" }, - calc = { kind = "  (Calc)" }, - vsnip = { kind = "  (Snippet)" }, - nvim_lsp = { kind = "  (LSP)" }, - nvim_lua = false, - spell = { kind = "  (Spell)" }, - tags = false, - vim_dadbod_completion = false, - snippets_nvim = false, - ultisnips = false, - treesitter = false, - emoji = { kind = " ﲃ (Emoji)", filetypes = { "markdown", "text" } }, - -- for emoji press : (idk if that in compe tho) - }, - - keymap = { - values = { - insert_mode = { - -- [""] = { 'pumvisible() ? "" : ""', { silent = true, noremap = true, expr = true } }, - -- [""] = { 'pumvisible() ? "" : ""', { silent = true, noremap = true, expr = true } }, - [""] = { "compe#complete()", { silent = true, noremap = true, expr = true } }, - [""] = { "compe#close('')", { silent = true, noremap = true, expr = true } }, - [""] = { "compe#scroll({ 'delta': +4 })", { silent = true, noremap = true, expr = true } }, - [""] = { "compe#scroll({ 'delta': -4 })", { silent = true, noremap = true, expr = true } }, - }, - }, - opts = { - insert_mode = { noremap = true, silent = true, expr = true }, - }, - }, - } -end - -M.setup = function() - vim.g.vsnip_snippet_dir = lvim.vsnip_dir - - local compe = require "compe" - - compe.setup(lvim.builtin.compe) - - local t = function(str) - return vim.api.nvim_replace_termcodes(str, true, true, true) - end - - local check_back_space = function() - local col = vim.fn.col "." - 1 - if col == 0 or vim.fn.getline("."):sub(col, col):match "%s" then - return true - else - return false - end - end - - local is_emmet_active = function() - local clients = vim.lsp.buf_get_clients() - - for _, client in pairs(clients) do - if client.name == "emmet_ls" then - return true - end - end - return false - end - - -- Use (s-)tab to: - --- move to prev/next item in completion menuone - --- jump to prev/next snippet's placeholder - _G.tab_complete = function() - if vim.fn.pumvisible() == 1 then - return t "" - elseif vim.fn.call("vsnip#jumpable", { 1 }) == 1 then - return t "(vsnip-jump-next)" - elseif check_back_space() then - return t "" - elseif is_emmet_active() then - return vim.fn["compe#complete"]() - else - return t "" - end - end - - _G.s_tab_complete = function() - if vim.fn.pumvisible() == 1 then - return t "" - elseif vim.fn.call("vsnip#jumpable", { -1 }) == 1 then - return t "(vsnip-jump-prev)" - else - return t "" - end - end - - local keymap = require "keymappings" - keymap.load(lvim.builtin.compe.keymap.values, lvim.builtin.compe.keymap.opts) - - vim.api.nvim_set_keymap("i", "", "v:lua.tab_complete()", { expr = true }) - vim.api.nvim_set_keymap("s", "", "v:lua.tab_complete()", { expr = true }) - vim.api.nvim_set_keymap("i", "", "v:lua.s_tab_complete()", { expr = true }) - vim.api.nvim_set_keymap("s", "", "v:lua.s_tab_complete()", { expr = true }) - - if lvim.builtin.compe.on_config_done then - lvim.builtin.compe.on_config_done(compe) - end -end - -return M diff --git a/lua/lsp/init.lua b/lua/lsp/init.lua index a734293a..edac2245 100644 --- a/lua/lsp/init.lua +++ b/lua/lsp/init.lua @@ -65,6 +65,12 @@ function M.common_capabilities() "additionalTextEdits", }, } + + local status_ok, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp") + if not status_ok then + return + end + capabilities = cmp_nvim_lsp.update_capabilities(capabilities) return capabilities end diff --git a/lua/lsp/kind.lua b/lua/lsp/kind.lua new file mode 100644 index 00000000..b78fd318 --- /dev/null +++ b/lua/lsp/kind.lua @@ -0,0 +1,31 @@ +local M = {} + +M.icons = { + Class = " ", + Color = " ", + Constant = "ﲀ ", + Constructor = " ", + Enum = "練", + EnumMember = " ", + Event = " ", + Field = " ", + File = "", + Folder = " ", + Function = " ", + Interface = "ﰮ ", + Keyword = " ", + Method = " ", + Module = " ", + Operator = "", + Property = " ", + Reference = " ", + Snippet = " ", + Struct = " ", + Text = " ", + TypeParameter = " ", + Unit = "塞", + Value = " ", + Variable = " ", +} + +return M diff --git a/lua/plugins.lua b/lua/plugins.lua index ef9ae041..be614c5f 100644 --- a/lua/plugins.lua +++ b/lua/plugins.lua @@ -24,49 +24,36 @@ return { end, disable = not lvim.builtin.telescope.active, }, - - -- Completion & Snippets + -- Install nvim-cmp, and buffer source as a dependency { - "hrsh7th/nvim-compe", - event = "InsertEnter", + "hrsh7th/nvim-cmp", config = function() - require("core.compe").setup() + require("core.cmp").setup() end, - disable = not lvim.builtin.compe.active, - -- wants = "vim-vsnip", - -- requires = { - -- { - -- "hrsh7th/vim-vsnip", - -- wants = "friendly-snippets", - -- event = "InsertCharPre", - -- }, - -- { - -- "rafamadriz/friendly-snippets", - -- event = "InsertCharPre", - -- }, - -- }, - }, - { - "hrsh7th/vim-vsnip", - -- wants = "friendly-snippets", - event = "InsertEnter", - disable = not lvim.builtin.compe.active, + requires = { + "L3MON4D3/LuaSnip", + "saadparwaiz1/cmp_luasnip", + "hrsh7th/cmp-buffer", + "hrsh7th/cmp-nvim-lsp", + "hrsh7th/cmp-path", + "hrsh7th/cmp-nvim-lua", + }, }, { "rafamadriz/friendly-snippets", - event = "InsertCharPre", - disable = not lvim.builtin.compe.active, + -- event = "InsertCharPre", + -- disable = not lvim.builtin.compe.active, }, -- Autopairs { "windwp/nvim-autopairs", -- event = "InsertEnter", - after = "nvim-compe", + after = "nvim-cmp", config = function() require("core.autopairs").setup() end, - disable = not lvim.builtin.autopairs.active or not lvim.builtin.compe.active, + disable = not lvim.builtin.autopairs.active, }, -- Treesitter @@ -98,6 +85,7 @@ return { require("core.gitsigns").setup() end, event = "BufRead", + commit = "25d4b182ece5b03cd4b2d8c196f3c38e0df58801", disable = not lvim.builtin.gitsigns.active, }, -- cgit v1.2.3