From d7c66522cf365f516babcfeb1d4a2d36c3ea41af Mon Sep 17 00:00:00 2001 From: delta Date: Wed, 29 Oct 2025 16:35:38 +0100 Subject: a small refactor --- .config/nvim/lua/plugins/lsp.lua | 406 +++++++++++++++++---------------------- 1 file changed, 181 insertions(+), 225 deletions(-) (limited to '.config/nvim/lua/plugins/lsp.lua') diff --git a/.config/nvim/lua/plugins/lsp.lua b/.config/nvim/lua/plugins/lsp.lua index 1063c5d..a438f3a 100644 --- a/.config/nvim/lua/plugins/lsp.lua +++ b/.config/nvim/lua/plugins/lsp.lua @@ -1,254 +1,210 @@ -local icons = require("icons") +local icons = require "icons" -- this is horrible and it should be redone someday -- but that day is not today +local _supports_method = {} + return { - { - "neovim/nvim-lspconfig", - -- enabled = false, - event = { "BufReadPre", "BufNewFile" }, - dependencies = { - { "williamboman/mason.nvim", config = true, lazy = false }, - { "mason-org/mason-lspconfig.nvim", opts = { automatic_enable = false } }, - "hrsh7th/cmp-nvim-lsp", - }, - opts = { - diagnostics = { - }, - servers = { - lua_ls = { - settings = { - Lua = { - -- workspace = { - -- checkThirdParty = false, - -- library = { - -- vim.env.VIMRUNTIME, - -- }, - -- }, - completion = { - showWord = "Disable", - displayContext = 8, - }, - hint = { - enable = true, - }, - diagnostics = { "trailing-space" }, + "neovim/nvim-lspconfig", + -- enabled = false, + event = { "BufReadPre", "BufNewFile" }, + dependencies = { + { "williamboman/mason.nvim", config = true, lazy = false }, + { "mason-org/mason-lspconfig.nvim", opts = { automatic_enable = false } }, + -- "hrsh7th/cmp-nvim-lsp", + "saghen/blink.cmp", + }, + opts = { + diagnostics = {}, + servers = { + lua_ls = { + settings = { + Lua = { + workspace = { + checkThirdParty = false, + }, + completion = { + showWord = "Disable", + displayContext = 8, + }, + hint = { + enable = true, }, + diagnostics = { "trailing-space" }, }, }, - rust_analyzer = { settings = { completion = { fullFunctionSignatures = true } } }, - ts_ls = {}, }, - setup = {} + rust_analyzer = { settings = { completion = { fullFunctionSignatures = true } } }, + ts_ls = {}, }, - config = function(_, opts) - local function on_attach(fn) - vim.api.nvim_create_autocmd("LspAttach", { - callback = function(args) - local buffer = args.buf - local client = vim.lsp.get_client_by_id(args.data.client_id) - fn(client, buffer) - end, - }) + setup = {}, + inlay_hints = { + exclude = {}, + }, + }, + config = function(_, opts) + ---@param client vim.lsp.Client + local function _check_methods(client, buffer) + -- don't trigger on invalid buffers + if not vim.api.nvim_buf_is_valid(buffer) then + return end - - ---@param method string - ---@param fn fun(client:vim.lsp.Client, buffer) - local function on_supports_method(method, fn) - -- cache[method] = cache[method] or setmetatable({}, { __mode = "k" }) - return vim.api.nvim_create_autocmd("User", { - pattern = "LspSupportsMethod", - callback = function(args) - local client = vim.lsp.get_client_by_id(args.data.client_id) - local buffer = args.data.buffer ---@type number - if client and method == args.data.method then - return fn(client, buffer) - end - end, - }) + -- don't trigger on non-listed buffers + if not vim.bo[buffer].buflisted then + return end - - -- function _check_methods(client, buffer) - -- if - -- -- don't trigger on invalid buffers - -- not vim.api.nvim_buf_is_valid(buffer) or - -- -- don't trigger on non-listed buffers - -- vim.bo[buffer].buflisted or - -- -- don't trigger on nofile buffers - -- vim.bo[buffer].buftype == "nofile" - -- then - -- return - -- end - -- for method, clients in pairs(M._supports_method) do - -- clients[client] = clients[client] or {} - -- if not clients[client][buffer] then - -- if client.supports_method and client.supports_method(method, { bufnr = buffer }) then - -- clients[client][buffer] = true - -- vim.api.nvim_exec_autocmds("User", { - -- pattern = "LspSupportsMethod", - -- data = { client_id = client.id, buffer = buffer, method = method }, - -- }) - -- end - -- end - -- end - -- end - - on_supports_method("textDocument/inlayHint", function(client, buffer) - if - vim.api.nvim_buf_is_valid(buffer) - and vim.bo[buffer].buftype == "" - and not vim.tbl_contains(opts.inlay_hints.exclude, vim.bo[buffer].filetype) - then - vim.lsp.inlay_hint.enable(true, { bufnr = buffer }) + -- don't trigger on nofile buffers + if vim.bo[buffer].buftype == "nofile" then + return + end + for method, clients in pairs(_supports_method) do + clients[client] = clients[client] or {} + if not clients[client][buffer] then + if client.supports_method and client.supports_method(method, { bufnr = buffer }) then + clients[client][buffer] = true + vim.api.nvim_exec_autocmds("User", { + pattern = "LspSupportsMethod", + data = { client_id = client.id, buffer = buffer, method = method }, + }) + end end - end) + end + end - vim.diagnostic.config({ - underline = true, - update_in_insert = false, - virtual_text = { - spacing = 4, - source = "if_many", - prefix = function(diagnostic) - for name, icon in pairs(icons.diagnostics) do - if diagnostic.severity == vim.diagnostic.severity[name:upper()] then - return icon - end - end + ---@param on_attach fun(client:vim.lsp.Client, buffer) + ---@param name? string + local function on_attach(_on_attach, name) + return vim.api.nvim_create_autocmd("LspAttach", { + callback = function(args) + local buffer = args.buf ---@type number + local client = vim.lsp.get_client_by_id(args.data.client_id) + if client and (not name or client.name == name) then + return _on_attach(client, buffer) end + end, + }) + end - }, - severity_sort = true, + ---@param method string + ---@param fn fun(client:vim.lsp.Client, buffer) + local function on_supports_method(method, fn) + _supports_method[method] = _supports_method[method] or setmetatable({}, { __mode = "k" }) + return vim.api.nvim_create_autocmd("User", { + pattern = "LspSupportsMethod", + callback = function(args) + local client = vim.lsp.get_client_by_id(args.data.client_id) + local buffer = args.data.buffer ---@type number + if client and method == args.data.method then + return fn(client, buffer) + end + end, }) + end - local servers = opts.servers - local cmp_nvim_lsp = require "cmp_nvim_lsp" - local capabilities = vim.tbl_deep_extend( - "force", - {}, - vim.lsp.protocol.make_client_capabilities(), - cmp_nvim_lsp.default_capabilities() - ) + ---@param fn fun(client:vim.lsp.Client, buffer):boolean? + ---@param opts? {group?: integer} + local function on_dynamic_capability(fn, opts) + return vim.api.nvim_create_autocmd("User", { + pattern = "LspDynamicCapability", + group = opts and opts.group or nil, + callback = function(args) + local client = vim.lsp.get_client_by_id(args.data.client_id) + local buffer = args.data.buffer ---@type number + if client then + return fn(client, buffer) + end + end, + }) + end - local function setup(server) - local server_opts = vim.tbl_deep_extend("force", { - capabilities = vim.deepcopy(capabilities), - }, servers[server] or {}) + local register_capability = vim.lsp.handlers["client/registerCapability"] + vim.lsp.handlers["client/registerCapability"] = function(err, res, ctx) + ---@diagnostic disable-next-line: no-unknown + local ret = register_capability(err, res, ctx) + local client = vim.lsp.get_client_by_id(ctx.client_id) + if client then + for buffer in pairs(client.attached_buffers) do + vim.api.nvim_exec_autocmds("User", { + pattern = "LspDynamicCapability", + data = { client_id = client.id, buffer = buffer }, + }) + end + end + return ret + end + on_attach(_check_methods) + on_dynamic_capability(_check_methods) - if opts.setup[server] then - if opts.setup[server](server, server_opts) then - return - end - elseif opts.setup["*"] then - if opts.setup["*"](server, server_opts) then - return + -- on_supports_method("textDocument/inlayHint", function(client, buffer) + -- if + -- vim.api.nvim_buf_is_valid(buffer) + -- and vim.bo[buffer].buftype == "" + -- and not vim.tbl_contains(opts.inlay_hints.exclude, vim.bo[buffer].filetype) + -- then + -- vim.lsp.inlay_hint.enable(true, { bufnr = buffer }) + -- end + -- end) + + vim.diagnostic.config { + underline = true, + update_in_insert = false, + virtual_text = { + spacing = 4, + source = "if_many", + prefix = function(diagnostic) + for name, icon in pairs(icons.diagnostics) do + if diagnostic.severity == vim.diagnostic.severity[name:upper()] then + return "[" .. icon .. "]" + end end + end, + }, + severity_sort = true, + } + + local servers = opts.servers + local blink_cmp = require "blink.cmp" + local capabilities = vim.tbl_deep_extend( + "force", + {}, + vim.lsp.protocol.make_client_capabilities(), + blink_cmp.get_lsp_capabilities({}, false) + ) + + local function setup(server) + local server_opts = vim.tbl_deep_extend("force", { + capabilities = vim.deepcopy(capabilities), + }, servers[server] or {}) + + if opts.setup[server] then + if opts.setup[server](server, server_opts) then + return + end + elseif opts.setup["*"] then + if opts.setup["*"](server, server_opts) then + return end - require("lspconfig")[server].setup(server_opts) end + vim.lsp.config(server, server_opts) + vim.lsp.enable(server) + end - local mlsp = require "mason-lspconfig" - local all_mslp_servers = require("mason-lspconfig").get_mappings().lspconfig_to_package + local mlsp = require "mason-lspconfig" + local all_mslp_servers = require("mason-lspconfig").get_mappings().lspconfig_to_package - local ensure_installed = {} - for server, server_opts in pairs(servers) do - if server_opts then - server_opts = server_opts == true and {} or server_opts - if server_opts.mason == false or not vim.tbl_contains(all_mslp_servers, server) then - setup(server) - else - ensure_installed[#ensure_installed + 1] = server - end + local ensure_installed = {} + for server, server_opts in pairs(servers) do + if server_opts then + server_opts = server_opts == true and {} or server_opts + if server_opts.mason == false or not vim.tbl_contains(all_mslp_servers, server) then + setup(server) + else + ensure_installed[#ensure_installed + 1] = server end end + end - -- mlsp.setup { ensure_installed = ensure_installed, handlers = { setup } } - mlsp.setup { ensure_installed = ensure_installed } - end, - -- config = function(_, opts) - -- local cache = {} - -- - -- - -- - -- - -- on_attach(function(client, buffer) - -- -- TODO: add keybinds - -- end) - -- - -- local register_capability = vim.lsp.handlers["client/registerCapability"] - -- - -- vim.lsp.handlers["client/registerCapability"] = function(err, res, ctx) - -- local ret = register_capability(err, res, ctx) - -- local client_id = ctx.client_id - -- local client = vim.lsp.get_client_by_id(client_id) - -- local buffer = vim.api.nvim_get_current_buf() - -- -- TODO: add keybinds - -- return ret - -- end - -- - -- for name, icon in pairs(require("icons").diagnostics) do - -- name = "DiagnosticSign" .. name - -- vim.fn.sign_define(name, { text = icon, texthl = name, numhl = "" }) - -- end - -- - -- local inlay_hint = vim.lsp.buf.inlay_hint or vim.lsp.inlay_hint - -- - -- - -- if type(opts.diagnostics.virtual_text) == "table" and opts.diagnostics.virtual_text.prefix == "icons" then - -- opts.diagnostics.virtual_text.prefix = vim.fn.has "nvim-0.10.0" == 0 and "●" - -- end - -- --- ) - -- - -- local servers = opts.servers - -- local has_cmp, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp") - -- local capabilities = vim.tbl_deep_extend( - -- "force", - -- {}, - -- vim.lsp.protocol.make_client_capabilities(), - -- has_cmp and cmp_nvim_lsp.default_capabilities() or {}, - -- opts.capabilities or {} - -- ) - -- - -- local function setup(server) - -- local server_opts = vim.tbl_deep_extend("force", { - -- capabilities = vim.deepcopy(capabilities), - -- }, servers[server] or {}) - -- - -- if opts.setup[server] then - -- if opts.setup[server](server, server_opts) then - -- return - -- end - -- elseif opts.setup["*"] then - -- if opts.setup["*"](server, server_opts) then - -- return - -- end - -- end - -- require("lspconfig")[server].setup(server_opts) - -- end - -- - -- local have_mason, mlsp = pcall(require, "mason-lspconfig") - -- local all_mslp_servers = {} - -- if have_mason then - -- all_mslp_servers = vim.tbl_keys(require("mason-lspconfig.mappings.server").lspconfig_to_package) - -- end - -- - -- local ensure_installed = {} - -- for server, server_opts in pairs(servers) do - -- if server_opts then - -- server_opts = server_opts == true and {} or server_opts - -- if server_opts.mason == false or not vim.tbl_contains(all_mslp_servers, server) then - -- setup(server) - -- else - -- ensure_installed[#ensure_installed + 1] = server - -- end - -- end - -- end - -- - -- if have_mason then - -- mlsp.setup { ensure_installed = ensure_installed, handlers = { setup } } - -- end - -- end, - }, + mlsp.setup { ensure_installed = ensure_installed } + end, } -- cgit v1.2.3