From 4dd4067fab56d7af6cff6bc942969fb5b6483faf Mon Sep 17 00:00:00 2001 From: JafarAbdi Date: Sat, 7 Sep 2024 11:38:21 +0100 Subject: [PATCH] Builtin completion --- neovim/.config/nvim/init.lua | 194 +++++++---------------------- neovim/.config/nvim/lazy-lock.json | 14 +-- 2 files changed, 43 insertions(+), 165 deletions(-) diff --git a/neovim/.config/nvim/init.lua b/neovim/.config/nvim/init.lua index 457d572b..f98b6e33 100644 --- a/neovim/.config/nvim/init.lua +++ b/neovim/.config/nvim/init.lua @@ -424,14 +424,6 @@ vim.api.nvim_create_autocmd("FileType", { end, }) -vim.api.nvim_create_autocmd("FileType", { - pattern = { "cpp", "c" }, - group = general_group, - callback = function() - -- This fixes an issue with nvim-cmp -- see https://github.com/hrsh7th/nvim-cmp/issues/1035#issuecomment-1195456419 - vim.opt_local.cindent = false - end, -}) -- A terrible way to handle symlinks vim.api.nvim_create_autocmd("BufWinEnter", { callback = function(params) @@ -465,13 +457,52 @@ vim.api.nvim_create_autocmd("TermOpen", { group = general_group, }) +local completion = { + pattern = "\\k", +} + +vim.api.nvim_create_autocmd("InsertCharPre", { + callback = function() + if tonumber(vim.fn.pumvisible()) ~= 0 then + return + end + local function feedkeys(keys) + vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(keys, true, false, true), "n", true) + end + if vim.fn.match(vim.v.char, completion.pattern) >= 0 then + if + vim.iter(vim.lsp.get_clients({ bufnr = 0 })):any(function(client) + return client.supports_method(vim.lsp.protocol.Methods.textDocument_completion) + end) + then + vim.lsp.completion.trigger() + else + feedkeys("") + end + end + end, + group = general_group, +}) + vim.api.nvim_create_autocmd("FocusGained", { command = "checktime", group = general_group }) +vim.api.nvim_create_autocmd("LspDetach", { + callback = function(args) + vim.lsp.completion.enable(false, args.data.client_id, args.buf) + end, + group = lsp_group, +}) vim.api.nvim_create_autocmd("LspAttach", { callback = function(args) + vim.lsp.completion.enable(true, args.data.client_id, args.buf, { autotrigger = true }) + vim.keymap.set("i", "", function() + vim.lsp.completion.trigger() + end, { buffer = args.buf }) + vim.keymap.set("i", "", function() + return tonumber(vim.fn.pumvisible()) ~= 0 and "" or "" + end, { expr = true, buffer = args.buf }) vim.keymap.set({ "n", "i" }, "", function() - local cmp = require("cmp") - if cmp.visible() then - cmp.close() + if tonumber(vim.fn.pumvisible()) == 1 then + vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("", true, false, true), "n", true) end vim.lsp.buf.signature_help() end, { buffer = args.buf, silent = true }) @@ -1139,133 +1170,6 @@ require("lazy").setup({ } end, }, - { - "hrsh7th/nvim-cmp", - event = { "InsertEnter" }, - dependencies = { - "hrsh7th/cmp-nvim-lsp", - "hrsh7th/cmp-buffer", - }, - config = function() - local cmp = require("cmp") - local compare = require("cmp.config.compare") - local cache = {} - local cmp_source = { - complete = function(_, params, callback) - local bufnr = vim.api.nvim_get_current_buf() - if not cache[bufnr] then - local completion_items = vim.tbl_map(function(snippet) - ---@type lsp.CompletionItem - local item = { - documentation = { - kind = cmp.lsp.MarkupKind.PlainText, - value = snippet.description or "", - }, - word = snippet.trigger, - label = snippet.trigger, - kind = vim.lsp.protocol.CompletionItemKind.Snippet, - insertText = type(snippet.body) == "function" and snippet.body() or snippet.body, - insertTextFormat = vim.lsp.protocol.InsertTextFormat.Snippet, - } - return item - end, get_buffer_snippets(params.context.filetype)) - cache[bufnr] = completion_items - end - - callback(cache[bufnr]) - end, - } - - cmp.register_source("snippets", cmp_source) - cmp.setup({ - snippet = { - expand = function(args) - vim.snippet.expand(args.body) - end, - }, - mapping = cmp.mapping.preset.insert({ - ["Tab"] = cmp.config.disable, - ["S-Tab"] = cmp.config.disable, - [""] = cmp.config.disable, - [""] = cmp.mapping.scroll_docs(4), - [""] = cmp.mapping.scroll_docs(-4), - [""] = cmp.mapping.confirm({ - behavior = cmp.ConfirmBehavior.Insert, - select = false, - }), - }), - sources = { - { name = "nvim_lsp" }, - { name = "snippets" }, - { - name = "buffer", - option = { - get_bufnrs = function() - return vim.api.nvim_list_bufs() - end, - }, - }, - }, - formatting = { - format = function(entry, vim_item) - vim_item.menu = ({ - buffer = "[Buffer]", - nvim_lsp = "[LSP]", - snippets = "[Snippet]", - })[entry.source.name] - local label = vim_item.abbr - -- https://github.com/hrsh7th/nvim-cmp/discussions/609 - local ELLIPSIS_CHAR = "…" - local MAX_LABEL_WIDTH = math.floor(vim.o.columns * 0.4) - local truncated_label = vim.fn.strcharpart(label, 0, MAX_LABEL_WIDTH) - if truncated_label ~= label then - vim_item.abbr = truncated_label .. ELLIPSIS_CHAR - end - return vim_item - end, - }, - sorting = { - comparators = { - compare.offset, - compare.exact, - -- compare.score, - -- https://github.com/p00f/clangd_extensions.nvim/blob/main/lua/clangd_extensions/cmp_scores.lua - function(entry1, entry2) - local diff - if entry1.completion_item.score and entry2.completion_item.score then - diff = (entry2.completion_item.score * entry2.score) - - (entry1.completion_item.score * entry1.score) - else - diff = entry2.score - entry1.score - end - if diff < 0 then - return true - elseif diff > 0 then - return false - end - end, - -- https://github.com/lukas-reineke/cmp-under-comparator - function(entry1, entry2) - local _, entry1_under = entry1.completion_item.label:find("^_+") - local _, entry2_under = entry2.completion_item.label:find("^_+") - entry1_under = entry1_under or 0 - entry2_under = entry2_under or 0 - if entry1_under > entry2_under then - return false - elseif entry1_under < entry2_under then - return true - end - end, - compare.recently_used, - compare.kind, - compare.sort_text, - compare.length, - compare.order, - }, - }, - }) - end, - }, }, { defaults = { lazy = true, -- every plugin is lazy-loaded by default @@ -1383,20 +1287,6 @@ end local q = require("qwahl") -local function try_jump(direction, key) - if vim.snippet.active({ direction = direction }) then - return string.format("lua vim.snippet.jump(%d)", direction) - end - return key -end - -vim.keymap.set({ "i", "s" }, "", function() - return try_jump(1, "") -end, { expr = true }) -vim.keymap.set({ "i", "s" }, "", function() - return try_jump(-1, "") -end, { expr = true }) - vim.keymap.set("t", "", [[]], { silent = true }) vim.keymap.set({ "i", "s" }, "", function() if vim.snippet then diff --git a/neovim/.config/nvim/lazy-lock.json b/neovim/.config/nvim/lazy-lock.json index d268a83e..8a9d4129 100644 --- a/neovim/.config/nvim/lazy-lock.json +++ b/neovim/.config/nvim/lazy-lock.json @@ -1,24 +1,12 @@ { - "cmp-buffer": { - "branch": "main", - "commit": "3022dbc9166796b644a841a02de8dd1cc1d311fa" - }, - "cmp-nvim-lsp": { - "branch": "main", - "commit": "39e2eda76828d88b773cc27a3f61d2ad782c922d" - }, "copilot.vim": { "branch": "release", - "commit": "a9fa7946d7307ceb138bfe2706ba75febf6450da" + "commit": "782461159655b259cff10ecff05efa761e3d4764" }, "lazy.nvim": { "branch": "main", "commit": "077102c5bfc578693f12377846d427f49bc50076" }, - "nvim-cmp": { - "branch": "main", - "commit": "ae644feb7b67bf1ce4260c231d1d4300b19c6f30" - }, "nvim-dap": { "branch": "master", "commit": "281a2e4cd1e7a17cea7ecb1745d84a8ab1249925"