From 6752a5194b14676d2efe382aaf66c4fe8f9762ce Mon Sep 17 00:00:00 2001 From: Oli Morris Date: Tue, 24 Dec 2024 10:30:31 +0000 Subject: [PATCH] fix(neovim): and back to `nvim-cmp` again --- .config/nvim/lua/plugins/lsp.lua | 192 +++++++++++++++++++++---------- 1 file changed, 130 insertions(+), 62 deletions(-) diff --git a/.config/nvim/lua/plugins/lsp.lua b/.config/nvim/lua/plugins/lsp.lua index d08ee7b..ceeef1b 100644 --- a/.config/nvim/lua/plugins/lsp.lua +++ b/.config/nvim/lua/plugins/lsp.lua @@ -6,65 +6,131 @@ local icons = { } return { - -- LSP + -- Completion { - "neovim/nvim-lspconfig", + "hrsh7th/nvim-cmp", dependencies = { - { - "saghen/blink.cmp", -- Better completion - version = "*", - dependencies = { - "giuxtaposition/blink-cmp-copilot", - "rafamadriz/friendly-snippets", - "L3MON4D3/LuaSnip", + "hrsh7th/cmp-buffer", + "hrsh7th/cmp-path", + "hrsh7th/cmp-cmdline", + "saadparwaiz1/cmp_luasnip", + "hrsh7th/cmp-nvim-lua", + "lukas-reineke/cmp-under-comparator", + }, + config = function() + local luasnip = require("luasnip") + require("luasnip.loaders.from_vscode").lazy_load() + vim.opt.completeopt = { "menu", "menuone", "noselect" } + + local cmp = require("cmp") + cmp.setup({ + formatting = { + fields = { "abbr", "kind", "menu" }, + format = require("lspkind").cmp_format({ + mode = "symbol", -- show only symbol annotations + maxwidth = 50, -- prevent the popup from showing more than provided characters + ellipsis_char = "...", -- when popup menu exceed maxwidth, the truncated part would show ellipsis_char instead + }), }, - opts = { - -- 'enter' for mappings similar to 'super-tab' but with 'enter' to accept - keymap = { - preset = "enter", - [""] = { "select_prev", "fallback" }, - [""] = { "select_next", "fallback" }, - -- [""] = { "accept", "fallback" }, - -- [""] = { "hide", "fallback" }, - -- [""] = { "show", "show_documentation", "hide_documentation" }, - -- [""] = { "snippet_forward", "fallback" }, - -- [""] = { "snippet_backward", "fallback" }, - -- [""] = { "select_prev", "fallback" }, - -- [""] = { "select_next", "fallback" }, - -- [""] = { "scroll_documentation_up", "fallback" }, - -- [""] = { "scroll_documentation_down", "fallback" }, - }, + window = { + completion = cmp.config.window.bordered(), + documentation = cmp.config.window.bordered(), + }, + snippet = { + expand = function(args) + luasnip.lsp_expand(args.body) + end, + }, + mapping = cmp.mapping.preset.insert({ + [""] = cmp.mapping.confirm({ select = false }), + [""] = cmp.mapping(function(fallback) + local col = vim.fn.col(".") - 1 - snippets = { - expand = function(snippet) - require("luasnip").lsp_expand(snippet) - end, - active = function(filter) - if filter and filter.direction then - return require("luasnip").jumpable(filter.direction) - end - return require("luasnip").in_snippet() - end, - jump = function(direction) - require("luasnip").jump(direction) - end, - }, + if cmp.visible() then + cmp.select_next_item({ behavior = "select" }) + elseif luasnip.expand_or_locally_jumpable() then + luasnip.expand_or_jump() + elseif col == 0 or vim.fn.getline("."):sub(col, col):match("%s") then + fallback() + else + cmp.complete() + end + end, { "i", "s" }), + + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_prev_item({ behavior = "select" }) + elseif luasnip.locally_jumpable(-1) then + luasnip.jump(-1) + else + fallback() + end + end, { "i", "s" }), + + -- Go to next placeholder in the snippet + [""] = cmp.mapping(function(fallback) + if luasnip.jumpable(1) then + luasnip.jump(1) + else + fallback() + end + end, { "i", "s" }), + -- Go to previous placeholder in the snippet + [""] = cmp.mapping(function(fallback) + if luasnip.jumpable(-1) then + luasnip.jump(-1) + else + fallback() + end + end, { "i", "s" }), + }), + sources = { + { name = "luasnip", priority = 100, max_item_count = 5 }, + { name = "nvim_lsp", priority = 90 }, + { name = "path", priority = 20 }, + { name = "buffer", priority = 10, keyword_length = 3, max_item_count = 8 }, + { name = "nvim_lua" }, + { name = "nvim_lsp_signature_help" }, + }, + }) - sources = { - default = { "lsp", "path", "luasnip", "buffer", "copilot", "codecompanion" }, - providers = { - codecompanion = { - name = "CodeCompanion", - module = "codecompanion.providers.completion.blink", - }, - copilot = { - name = "copilot", - module = "blink-cmp-copilot", - }, + cmp.setup.cmdline(":", { + mapping = cmp.mapping.preset.cmdline(), + sources = cmp.config.sources({ + { name = "path" }, + { + name = "cmdline", + option = { + ignore_cmds = { "Man", "!" }, }, }, + }), + sorting = { + comparators = { + cmp.config.compare.offset, + cmp.config.compare.exact, + cmp.config.compare.score, + cmp.config.compare.recently_used, + require("cmp-under-comparator").under, + cmp.config.compare.kind, + }, }, - }, + }) + + cmp.setup.cmdline("/", { + mapping = cmp.mapping.preset.cmdline(), + sources = { + { name = "buffer" }, + }, + }) + end, + }, + + -- LSP + { + "neovim/nvim-lspconfig", + dependencies = { + { "hrsh7th/cmp-nvim-lsp" }, { "williamboman/mason.nvim", build = function() @@ -79,21 +145,19 @@ return { }, { "williamboman/mason-lspconfig.nvim" }, }, - config = function(_, opts) + config = function() require("lspconfig.ui.windows").default_options.border = "single" - require("luasnip.loaders.from_vscode").lazy_load({ paths = { "~/.config/snippets" } }) + vim.o.runtimepath = vim.o.runtimepath .. ",~/.dotfiles/.config/snippets" require("ufo").setup() - local has_blink, blink = pcall(require, "blink.cmp") - local capabilities = - vim.tbl_deep_extend("force", has_blink and blink.get_lsp_capabilities() or {}, opts.capabilities or {}, { - textDocument = { - foldingRange = { - dynamicRegistration = false, - lineFoldingOnly = true, - }, + local capabilities = vim.tbl_deep_extend("force", require("cmp_nvim_lsp").default_capabilities(), { + textDocument = { + foldingRange = { + dynamicRegistration = false, + lineFoldingOnly = true, }, - }) + }, + }) local lspconfig_defaults = require("lspconfig").util.default_config lspconfig_defaults.capabilities = vim.tbl_deep_extend("force", lspconfig_defaults.capabilities, capabilities) @@ -339,6 +403,10 @@ return { end, }, + -- Code snippets + "L3MON4D3/LuaSnip", + "rafamadriz/friendly-snippets", + -- Diagnostic signs { "ivanjermakov/troublesum.nvim",