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", "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 = {}, 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 -- don't trigger on non-listed buffers if not vim.bo[buffer].buflisted then return end -- 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 ---@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 ---@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 ---@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 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) -- 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 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 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 } end, }