From d65e02235572ca3f9c2265965d8cfbc6f8d6b8ff Mon Sep 17 00:00:00 2001 From: abzcoding Date: Thu, 16 Sep 2021 21:52:27 +0430 Subject: [PATCH] refactor(nvim-cmp): use nvim-cmp instead of compe and lazyload everything --- lua/doom/modules/config/doom-cmp.lua | 28 ++++++----- .../modules/config/doom-lsp-signature.lua | 28 +++++++++-- lua/doom/modules/config/doom-lspinstall.lua | 27 +++++++++- lua/doom/modules/config/doom-luasnip.lua | 26 +++++++++- lua/doom/utils/init.lua | 49 +++++++++++++++++++ 5 files changed, 140 insertions(+), 18 deletions(-) diff --git a/lua/doom/modules/config/doom-cmp.lua b/lua/doom/modules/config/doom-cmp.lua index 2fc91f562..c267775d8 100644 --- a/lua/doom/modules/config/doom-cmp.lua +++ b/lua/doom/modules/config/doom-cmp.lua @@ -38,17 +38,16 @@ return function() end local function check_backspace() 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 + return col == 0 or vim.fn.getline("."):sub(col, col):match("%s") end cmp.setup({ completion = { completeopt = "menu,menuone,preview,noinsert", }, + documentation = { + border = { "╭", "─", "╮", "│", "╯", "─", "╰", "│" }, + }, formatting = { format = function(entry, item) item.kind = string.format("%s %s", get_kind(item.kind), item.kind) @@ -59,21 +58,26 @@ return function() nvim_lua = "[Lua]", path = "[Path]", })[entry.source.name] - + item.dup = ({ + buffer = 1, + path = 1, + nvim_lsp = 0, + })[entry.source.name] or 0 return item end, }, - mappings = { + mapping = { + [""] = cmp.mapping.select_prev_item(), + [""] = cmp.mapping.select_next_item(), + [""] = cmp.mapping.scroll_docs(-4), + [""] = cmp.mapping.scroll_docs(4), [""] = cmp.mapping.complete(), + [""] = cmp.mapping.close(), + -- [""] = cmp.mapping.close(), [""] = cmp.mapping.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = true, }), - [""] = cmp.mapping.close(), - [""] = cmp.mapping.scroll_docs(4), - [""] = cmp.mapping.scroll_docs(-4), - [""] = cmp.mapping.select_prev_item(), - [""] = cmp.mapping.select_next_item(), [""] = cmp.mapping(function(fallback) if vim.fn.pumvisible() == 1 then vim.fn.feedkeys(t(""), "n") diff --git a/lua/doom/modules/config/doom-lsp-signature.lua b/lua/doom/modules/config/doom-lsp-signature.lua index eb0a5ab18..64c17e6ac 100644 --- a/lua/doom/modules/config/doom-lsp-signature.lua +++ b/lua/doom/modules/config/doom-lsp-signature.lua @@ -1,11 +1,31 @@ return function() -- Signature help - require("lsp_signature").setup({ + local cfg = { bind = true, - floating_window = true, + doc_lines = 10, + floating_window = false, -- show hint in a floating window, set to false for virtual text only mode floating_window_above_cur_line = true, + fix_pos = false, -- set to true, the floating window will not auto-close until finish all parameters + hint_enable = true, -- virtual hint enable + -- hint_prefix = "🐼 ", -- Panda for parameter + hint_prefix = " ", + hint_scheme = "String", + -- use_lspsaga = false, -- set to true if you want to use lspsaga popup + hi_parameter = "Search", -- how your parameter will be highlight + max_height = 12, -- max height of signature floating_window, if content is more than max_height, you can scroll down + -- to view the hiding contents + max_width = 120, -- max_width of signature floating_window, line will be wrapped if exceed max_width handler_opts = { - border = "single", + border = "single", -- double, single, shadow, none }, - }) + -- transpancy = 80, + extra_trigger_chars = {}, -- Array of extra characters that will trigger signature completion, e.g., {"(", ","} + zindex = 200, -- by default it will be on top of all floating windows, set to 50 send it to bottom + debug = false, -- set to true to enable debug logging + log_path = "debug_log_file_path", -- debug log path + padding = "", -- character to pad on left and right of signature can be ' ', or '|' etc + shadow_blend = 36, -- if you using shadow as border use this set the opacity + shadow_guibg = "Black", -- if you using shadow as border use this set the color e.g. 'Green' or '#121315' + } + require("lsp_signature").setup(cfg) end diff --git a/lua/doom/modules/config/doom-lspinstall.lua b/lua/doom/modules/config/doom-lspinstall.lua index 7b62ef1ac..cd21c97cb 100644 --- a/lua/doom/modules/config/doom-lspinstall.lua +++ b/lua/doom/modules/config/doom-lspinstall.lua @@ -3,8 +3,33 @@ return function() -- Snippets support local capabilities = vim.lsp.protocol.make_client_capabilities() - -- capabilities = require("cmp_nvim_lsp").update_capabilities(capabilities) + capabilities.textDocument.completion.completionItem.preselectSupport = true + capabilities.textDocument.completion.completionItem.insertReplaceSupport = true + capabilities.textDocument.completion.completionItem.labelDetailsSupport = true + capabilities.textDocument.completion.completionItem.deprecatedSupport = true + capabilities.textDocument.completion.completionItem.commitCharactersSupport = true + capabilities.textDocument.completion.completionItem.tagSupport = { valueSet = { 1 } } capabilities.textDocument.completion.completionItem.snippetSupport = true + capabilities.textDocument.completion.completionItem.resolveSupport = { + properties = { "documentation", "detail", "additionalTextEdits" }, + } + capabilities.textDocument.codeAction = { + dynamicRegistration = false, + codeActionLiteralSupport = { + codeActionKind = { + valueSet = { + "", + "quickfix", + "refactor", + "refactor.extract", + "refactor.inline", + "refactor.rewrite", + "source", + "source.organizeImports", + }, + }, + }, + } local lua_lsp = require("lua-dev").setup({ lspconfig = { diff --git a/lua/doom/modules/config/doom-luasnip.lua b/lua/doom/modules/config/doom-luasnip.lua index a572ed6c4..20e179169 100644 --- a/lua/doom/modules/config/doom-luasnip.lua +++ b/lua/doom/modules/config/doom-luasnip.lua @@ -1,3 +1,27 @@ return function() - require("luasnip.loaders.from_vscode").lazy_load() + local util = require("doom.utils") + local luasnip = require("luasnip") + + luasnip.config.set_config({ + history = true, + -- Update more often, :h events for more info. + updateevents = "TextChanged,TextChangedI", + }) + + require("luasnip/loaders/from_vscode").load() + + --- to jump to next snippet's placeholder + local function on_tab() + return luasnip.jump(1) and "" or util.t("") + end + + --- to jump to next snippet's placeholder + local function on_s_tab() + return luasnip.jump(-1) and "" or util.t("") + end + + util.imap("", on_tab, { expr = true }) + util.smap("", on_tab, { expr = true }) + util.imap("", on_s_tab, { expr = true }) + util.smap("", on_s_tab, { expr = true }) end diff --git a/lua/doom/utils/init.lua b/lua/doom/utils/init.lua index 74c584044..957f58bfc 100644 --- a/lua/doom/utils/init.lua +++ b/lua/doom/utils/init.lua @@ -148,6 +148,55 @@ M.write_file = function(path, content, mode) end) end +-- keep functions that we want to call +M.functions = {} + +-- execute the given lua functions +-- @tparam int index of the function in M.functions +function M.execute(id) + local func = M.functions[id] + if not func then + error("Function doest not exist: " .. id) + end + return func() +end + +-- map keybindings to functions and cmds +local key_map = function(mode, key, cmd, opts, defaults) + opts = vim.tbl_deep_extend("force", { silent = true }, defaults or {}, opts or {}) + + if type(cmd) == "function" then + table.insert(M.functions, cmd) + if opts.expr then + cmd = ([[luaeval('require("doom.utils").execute(%d)')]]):format(#M.functions) + else + cmd = ("lua require('doom.utils').execute(%d)"):format(#M.functions) + end + end + if opts.buffer ~= nil then + local buffer = opts.buffer + opts.buffer = nil + return vim.api.nvim_buf_set_keymap(buffer, mode, key, cmd, opts) + else + return vim.api.nvim_set_keymap(mode, key, cmd, opts) + end +end + +-- map termcode keybindings +function M.t(str) + return vim.api.nvim_replace_termcodes(str, true, true, true) +end + +-- insert mode keybinding +function M.imap(key, cmd, opts) + return key_map("i", key, cmd, opts) +end + +-- substitute mode keybinding +function M.smap(key, cmd, opts) + return key_map("s", key, cmd, opts) +end + M.load_modules = function(module_path, modules) for i = 1, #modules, 1 do local ok, err = xpcall(