From 9421ef807dd91feee8fea85fa84b32550f3e1c34 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Wed, 5 Feb 2025 18:30:32 +0100 Subject: [PATCH] nvim: Remove lsp-setup.nvim plugin Removed the plugin since all its functionality can be replicated with an equal amount of lines without it nowadays. Should be even simpler in the future when we can just have individual lsp server config files in the config dir. --- nvim/.config/nvim/lazy-lock.json | 3 +- nvim/.config/nvim/lua/plugins/lsp.lua | 316 +++++++++++--------------- 2 files changed, 136 insertions(+), 183 deletions(-) diff --git a/nvim/.config/nvim/lazy-lock.json b/nvim/.config/nvim/lazy-lock.json index 049641a..9cdd146 100644 --- a/nvim/.config/nvim/lazy-lock.json +++ b/nvim/.config/nvim/lazy-lock.json @@ -27,7 +27,6 @@ "jupytext.nvim": { "branch": "main", "commit": "c8baf3ad344c59b3abd461ecc17fc16ec44d0f7b" }, "lazy.nvim": { "branch": "main", "commit": "7e6c863bc7563efbdd757a310d17ebc95166cef3" }, "lazydev.nvim": { "branch": "main", "commit": "f59bd14a852ca43db38e3662395354cb2a9b13e0" }, - "lsp-setup.nvim": { "branch": "main", "commit": "6e4e977512ce426d8b52c27f3b6e6aefc73e1452" }, "ltex_extra.nvim": { "branch": "dev", "commit": "09dc879b1873001f855bca5ad1f024ca15b9bbaf" }, "lualine.nvim": { "branch": "master", "commit": "2a5bae925481f999263d6f5ed8361baef8df4f83" }, "luarocks.nvim": { "branch": "main", "commit": "1db9093915eb16ba2473cfb8d343ace5ee04130a" }, @@ -49,7 +48,7 @@ "nvim-colorizer.lua": { "branch": "master", "commit": "39142aa1390d6ccdca57cb6dc5b2c7bfed460ffe" }, "nvim-coverage": { "branch": "main", "commit": "a939e425e363319d952a6c35fb3f38b34041ded2" }, "nvim-lint": { "branch": "master", "commit": "789b7ada1b4f00e08d026dffde410dcfa6a0ba87" }, - "nvim-lspconfig": { "branch": "master", "commit": "e5bf88e5ea1c4cea5ea96b1e970cb264f7a401a1" }, + "nvim-lspconfig": { "branch": "master", "commit": "bf81bef7d75a0f4a0cf61462b318ea00b3c97cc8" }, "nvim-nio": { "branch": "master", "commit": "21f5324bfac14e22ba26553caf69ec76ae8a7662" }, "nvim-surround": { "branch": "main", "commit": "ae298105122c87bbe0a36b1ad20b06d417c0433e" }, "nvim-toggleterm.lua": { "branch": "main", "commit": "50ea089fc548917cc3cc16b46a8211833b9e3c7c" }, diff --git a/nvim/.config/nvim/lua/plugins/lsp.lua b/nvim/.config/nvim/lua/plugins/lsp.lua index a73b1fd..0618215 100644 --- a/nvim/.config/nvim/lua/plugins/lsp.lua +++ b/nvim/.config/nvim/lua/plugins/lsp.lua @@ -44,178 +44,46 @@ local servers = { yamlls = {}, } -return { - -- lsp setup +local lsp = { + { -- pretty lsp 'peek' menus + "DNLHC/glance.nvim", + opts = { border = { enable = true }, theme = { enable = true, mode = "auto" } }, + cmd = { "Glance" }, + }, { - "junnplus/lsp-setup.nvim", + "neovim/nvim-lspconfig", dependencies = { - { - "neovim/nvim-lspconfig", - -- will sometimes not keep up with lsp changes if set to stable - version = false, - }, - { - "williamboman/mason.nvim", - cmd = { - "Mason", - "MasonInstall", - "MasonUninstall", - "MasonUninstallAll", - "MasonLog", - "MasonUpdate", - }, - build = ":MasonUpdate", - keys = { - { "vm", ":Mason", desc = "Mason" }, - }, - }, { "williamboman/mason-lspconfig.nvim", + opts = { automatic_installation = true }, + dependencies = { + "williamboman/mason.nvim", + cmd = { + "Mason", + "MasonInstall", + "MasonUninstall", + "MasonUninstallAll", + "MasonLog", + "MasonUpdate", + }, + opts = {}, + build = ":MasonUpdate", + keys = { + { "vm", ":Mason", desc = "Mason" }, + }, + }, cmd = { "LspInstall", "LspUninstall" }, }, + { "saghen/blink.cmp", optional = true }, }, event = { "BufReadPost", "BufNewFile", "BufWritePre" }, - config = function() - vim.diagnostic.config({ virtual_text = true }) - vim.fn.sign_define("DiagnosticSignError", { text = "✘", texthl = "DiagnosticSignError" }) - vim.fn.sign_define("DiagnosticSignWarn", { text = "", texthl = "DiagnosticSignWarn" }) - vim.fn.sign_define("DiagnosticSignInfo", { text = "", texthl = "DiagnosticSignInfo" }) - vim.fn.sign_define("DiagnosticSignHint", { text = "", texthl = "DiagnosticSignHint" }) - - local lsp = require("lsp-setup") - - local function on_attach(_, bufnr) - local map = vim.keymap.set - map( - "n", - "[d", - "lua vim.diagnostic.goto_prev()", - { buffer = bufnr, desc = "Previous diagnostic" } - ) - map("n", "]d", "lua vim.diagnostic.goto_next()", { buffer = bufnr, desc = "Next diagnostic" }) - map( - "n", - "[D", - "lua vim.diagnostic.goto_prev({severity = vim.diagnostic.severity.ERROR})", - { buffer = bufnr, desc = "Previous error" } - ) - map( - "n", - "]D", - "lua vim.diagnostic.goto_next({severity = vim.diagnostic.severity.ERROR})", - { buffer = bufnr, desc = "Next error" } - ) - - if require("core.util").is_available("which-key") then - require("which-key").add({ "l", group = "language" }) - end - map( - "n", - "ld", - "lua vim.diagnostic.open_float()", - { buffer = bufnr, desc = "Line diagnostics" } - ) - map("n", "li", function() - vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled()) - end, { buffer = bufnr, desc = "Inlay hints" }) - map( - "n", - "la", - "lua vim.lsp.buf.code_action()", - { buffer = bufnr, desc = "Codeactions" } - ) - map( - "n", - "ln", - "lua vim.lsp.buf.rename()", - { buffer = bufnr, desc = "Rename element" } - ) - if vim.fn.exists(":Glance") then - map("n", "lr", "Glance references", { buffer = bufnr, desc = "References" }) - map("n", "lf", "Glance definitions", { buffer = bufnr, desc = "Definition" }) - map( - "n", - "lt", - "Glance type_definitions", - { buffer = bufnr, desc = "Type definition" } - ) - map( - "n", - "lm", - "Glance implementations", - { buffer = bufnr, desc = "Implementation" } - ) - elseif vim.fn.exists(":Telescope") then - map( - "n", - "lr", - "Telescope lsp_references", - { buffer = bufnr, desc = "References" } - ) - map( - "n", - "lf", - "Telescope lsp_definitions", - { buffer = bufnr, desc = "Definition" } - ) - map( - "n", - "lt", - "Telescope lsp_type_definitions", - { buffer = bufnr, desc = "Type definition" } - ) - map( - "n", - "lm", - "Telescope lsp_implementations", - { buffer = bufnr, desc = "Implementation" } - ) - else - map( - "n", - "lr", - "lua vim.lsp.buf.references()", - { buffer = bufnr, desc = "References" } - ) - map( - "n", - "lf", - "lua vim.lsp.buf.definition()", - { buffer = bufnr, desc = "Definition" } - ) - map( - "n", - "lt", - "lua vim.lsp.buf.type_definition()", - { buffer = bufnr, desc = "Type definition" } - ) - map( - "n", - "lm", - "lua vim.lsp.buf.implementation()", - { buffer = bufnr, desc = "Implementation" } - ) - end - map("n", "K", "lua vim.lsp.buf.hover()", { buffer = bufnr, desc = "Hover definition" }) - map( - "n", - "lc", - "lua vim.lsp.buf.declaration()", - { buffer = bufnr, desc = "Declaration" } - ) - map( - "n", - "ls", - "lua vim.lsp.buf.signature_help()", - { buffer = bufnr, desc = "Signature help" } - ) - map("n", "lo", function() - vim.diagnostic.enable(not vim.diagnostic.is_enabled()) - end, { buffer = bufnr, desc = "Toggle Diagnostics" }) - end + opts = { servers = servers }, + config = function(_, lspconfig_opts) + local lspconfig = require("lspconfig") -- Display diagnostics as virtual text only if not in insert mode -- /r/neovim/comments/12inp4c/disable_diagnostics_virtual_text_when_in_insert/jqqifwk/ + vim.diagnostic.config({ virtual_text = true }) vim.api.nvim_create_autocmd("InsertEnter", { callback = function() vim.diagnostic.config({ virtual_text = false }) @@ -227,16 +95,19 @@ return { end, }) - lsp.setup({ - default_mappings = false, - servers = servers, - on_attach = on_attach, - inlay_hints = { - enabled = vim.fn.has("nvim-0.10") == true and true or false, - }, - }) + vim.fn.sign_define("DiagnosticSignError", { text = "✘", texthl = "DiagnosticSignError" }) + vim.fn.sign_define("DiagnosticSignWarn", { text = "", texthl = "DiagnosticSignWarn" }) + vim.fn.sign_define("DiagnosticSignInfo", { text = "", texthl = "DiagnosticSignInfo" }) + vim.fn.sign_define("DiagnosticSignHint", { text = "", texthl = "DiagnosticSignHint" }) + + for server, config in pairs(lspconfig_opts.servers) do + -- TODO: Check if it actually can be ignored in Nvim 0.11+, see https://cmp.saghen.dev/installation.html#lazy-nvim + if vim.fn.has("nvim-0.11") == false then + config.capabilities = require("blink.cmp").get_lsp_capabilities(config.capabilities) + end + lspconfig[server].setup(config) + end - local lspconfig = require("lspconfig") lspconfig.nushell.setup({}) lspconfig.marksman.setup({ @@ -279,7 +150,6 @@ return { end, } end - on_attach(client, bufnr) end, on_new_config = function(conf, new_root) if require("lspconfig.util").root_pattern(".zk")(new_root) then @@ -295,12 +165,10 @@ return { -- we primarily use pyright for cmp lsp completion & hover info lspconfig.basedpyright.setup({ on_attach = function(client, bufnr) - on_attach(client, bufnr) require("core.util").set_python_env() if python_path == nil then python_path, _ = vim.fn.expand(require("core.util").get_python_venv_bin(client.config.root_dir)) end - -- print(string.format("[PYTHON VENV]: %s", vim.inspect(python_path))) client.config.settings.python = {} or client.config.settings.python client.config.settings.python.pythonPath = python_path end, @@ -319,7 +187,6 @@ return { }) lspconfig.ruff.setup({ on_attach = function(client, bufnr) - on_attach(client, bufnr) require("core.util").set_python_env() client.server_capabilities.hoverProvider = false -- we use pyright for hover info if python_path == nil then @@ -343,7 +210,6 @@ return { callback = function() lspconfig.ltex.setup({ on_attach = function(client, bufnr) - on_attach(client, bufnr) if require("core.util").is_available("ltex_extra") then require("ltex_extra").setup() end @@ -360,10 +226,98 @@ return { end, keys = { { "vs", ":LspInfo", desc = "LspInfo" } }, }, - -- pretty lsp 'peek' menus - { - "DNLHC/glance.nvim", - opts = { border = { enable = true }, theme = { enable = true, mode = "auto" } }, - cmd = { "Glance" }, - }, } + +vim.api.nvim_create_autocmd("LspAttach", { + desc = "LSP actions", + callback = function(event) + local o = function(add_opts) + return vim.tbl_extend("force", { buffer = event.buf }, add_opts) + end + + local map = vim.keymap.set + map("n", "K", "lua vim.lsp.buf.hover()", o({ desc = "Hover definition" })) + map("n", "[d", "lua vim.diagnostic.goto_prev()", o({ desc = "Previous diagnostic" })) + map("n", "]d", "lua vim.diagnostic.goto_next()", o({ desc = "Next diagnostic" })) + map( + "n", + "[D", + "lua vim.diagnostic.goto_prev({severity = vim.diagnostic.severity.ERROR})", + o({ desc = "Previous error" }) + ) + map( + "n", + "]D", + "lua vim.diagnostic.goto_next({severity = vim.diagnostic.severity.ERROR})", + o({ desc = "Next error" }) + ) + + if require("core.util").is_available("which-key") then + require("which-key").add({ "l", group = "language" }) + end + map("n", "ld", "lua vim.diagnostic.open_float()", o({ desc = "Show line diagnostics" })) + map("n", "lI", function() + vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled()) + end, o({ desc = "Toggle inlay hints" })) + map("n", "la", "lua vim.lsp.buf.code_action()", o({ desc = "Codeactions" })) + map("n", "ln", "lua vim.lsp.buf.rename()", o({ desc = "Rename element" })) + map("n", "lc", "lua vim.lsp.buf.declaration()", o({ desc = "Declaration" })) + map("n", "ls", "lua vim.lsp.buf.signature_help()", o({ desc = "Signature help" })) + map("n", "lo", function() + vim.diagnostic.enable(not vim.diagnostic.is_enabled()) + end, o({ desc = "Toggle Diagnostics" })) + + local pref = function(glances, telescope, fallback) + if glances and vim.fn.exists(":Glance") > 0 then + return glances + elseif telescope and vim.fn.exists(":Telescope") > 0 then + return telescope + else + return fallback + end + end + + map( + "n", + "lr", + pref( + "Glance references", + "Telescope lsp_references", + "lua vim.lsp.buf.references()" + ), + o({ desc = "References" }) + ) + map( + "n", + "lf", + pref( + "Glance definitions", + "Telescope lsp_definitions", + "lua vim.lsp.buf.definition()" + ), + o({ desc = "Definition" }) + ) + map( + "n", + "lt", + pref( + "Glance type_definitions", + "Telescope lsp_type_definitions", + "lua vim.lsp.buf.type_definition()" + ), + o({ desc = "Type definition" }) + ) + map( + "n", + "lm", + pref( + "Glance implementations", + "Telescope lsp_implementations", + "lua vim.lsp.buf.implementation()" + ), + o({ desc = "Implementation" }) + ) + end, +}) + +return lsp