refactor(lsp): cleanup lsp config

This commit is contained in:
Folke Lemaitre 2025-09-15 16:28:46 +02:00
parent 5e7da4384d
commit 0d27e89a36
No known key found for this signature in database
GPG key ID: 9B52594D560070AB
2 changed files with 31 additions and 42 deletions

View file

@ -21,9 +21,8 @@ return {
}, },
{ {
"neovim/nvim-lspconfig", "neovim/nvim-lspconfig",
---@class PluginLspOpts ---@type PluginLspOpts
opts = { opts = {
---@type lspconfig.options
servers = { servers = {
ruby_lsp = { ruby_lsp = {
enabled = lsp == "ruby_lsp", enabled = lsp == "ruby_lsp",

View file

@ -63,7 +63,7 @@ return {
timeout_ms = nil, timeout_ms = nil,
}, },
-- LSP Server Settings -- LSP Server Settings
---@type lspconfig.options ---@type table<string, vim.lsp.Config>
servers = { servers = {
lua_ls = { lua_ls = {
-- mason = false, -- set to false if you don't want this server to be installed with mason -- mason = false, -- set to false if you don't want this server to be installed with mason
@ -99,7 +99,7 @@ return {
}, },
-- you can do any additional lsp server setup here -- you can do any additional lsp server setup here
-- return true if you don't want this server to be setup with lspconfig -- return true if you don't want this server to be setup with lspconfig
---@type table<string, fun(server:string, opts:_.lspconfig.options):boolean?> ---@type table<string, fun(server:string, opts: vim.lsp.Config):boolean?>
setup = { setup = {
-- example to setup with typescript.nvim -- example to setup with typescript.nvim
-- tsserver = function(_, opts) -- tsserver = function(_, opts)
@ -149,6 +149,7 @@ return {
end) end)
end end
-- diagnostics
if type(opts.diagnostics.virtual_text) == "table" and opts.diagnostics.virtual_text.prefix == "icons" then if type(opts.diagnostics.virtual_text) == "table" and opts.diagnostics.virtual_text.prefix == "icons" then
opts.diagnostics.virtual_text.prefix = function(diagnostic) opts.diagnostics.virtual_text.prefix = function(diagnostic)
local icons = LazyVim.config.icons.diagnostics local icons = LazyVim.config.icons.diagnostics
@ -157,48 +158,42 @@ return {
return icon return icon
end end
end end
return ""
end end
end end
vim.diagnostic.config(vim.deepcopy(opts.diagnostics)) vim.diagnostic.config(vim.deepcopy(opts.diagnostics))
local servers = opts.servers
local has_cmp, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp")
local has_blink, blink = pcall(require, "blink.cmp")
local capabilities = vim.tbl_deep_extend( local capabilities = vim.tbl_deep_extend(
"force", "force",
{}, {},
vim.lsp.protocol.make_client_capabilities(), vim.lsp.protocol.make_client_capabilities(),
has_cmp and cmp_nvim_lsp.default_capabilities() or {}, LazyVim.has("nvim-cmp") and require("cmp_nvim_lsp").default_capabilities() or {},
has_blink and blink.get_lsp_capabilities() or {}, LazyVim.has("blink.nvim") and require("blink.cmp").get_lsp_capabilities() or {},
opts.capabilities or {} opts.capabilities or {}
) )
-- get all the servers that are available through mason-lspconfig -- get all the servers that are available through mason-lspconfig
local have_mason, mlsp = pcall(require, "mason-lspconfig") local have_mason = LazyVim.has("mason-lspconfig.nvim")
local all_mslp_servers = {} local mason_all = have_mason
all_mslp_servers = vim.tbl_keys(require("mason-lspconfig.mappings").get_mason_map().lspconfig_to_package) and vim.tbl_keys(require("mason-lspconfig.mappings").get_mason_map().lspconfig_to_package)
or {} --[[ @as string[] ]]
local exclude_automatic_enable = {} ---@type string[] local exclude_automatic_enable = {} ---@type string[]
local function configure(server) local function configure(server)
local server_opts = vim.tbl_deep_extend("force", { local server_opts = vim.tbl_deep_extend("force", {
capabilities = vim.deepcopy(capabilities), capabilities = vim.deepcopy(capabilities),
}, servers[server] or {}) }, opts.servers[server] or {})
if opts.setup[server] then local setup = opts.setup[server] or opts.setup["*"]
if opts.setup[server](server, server_opts) then if setup and setup(server, server_opts) then
return true return true -- lsp will be setup by the setup function
end
elseif opts.setup["*"] then
if opts.setup["*"](server, server_opts) then
return true
end
end end
vim.lsp.config(server, server_opts) vim.lsp.config(server, server_opts)
-- manually enable if mason=false or if this is a server that cannot be installed with mason-lspconfig -- manually enable if mason=false or if this is a server that cannot be installed with mason-lspconfig
if server_opts.mason == false or not vim.tbl_contains(all_mslp_servers, server) then if server_opts.mason == false or not vim.tbl_contains(mason_all, server) then
vim.lsp.enable(server) vim.lsp.enable(server)
return true return true
end end
@ -206,36 +201,31 @@ return {
end end
local ensure_installed = {} ---@type string[] local ensure_installed = {} ---@type string[]
for server, server_opts in pairs(servers) do for server, server_opts in pairs(opts.servers) do
if server_opts then server_opts = server_opts == true and {} or server_opts or false
server_opts = server_opts == true and {} or server_opts if server_opts and server_opts.enabled ~= false then
if server_opts.enabled ~= false then -- run manual setup if mason=false or if this is a server that cannot be installed with mason-lspconfig
-- run manual setup if mason=false or if this is a server that cannot be installed with mason-lspconfig if configure(server) then
if configure(server) then
exclude_automatic_enable[#exclude_automatic_enable + 1] = server
else
ensure_installed[#ensure_installed + 1] = server
end
else
exclude_automatic_enable[#exclude_automatic_enable + 1] = server exclude_automatic_enable[#exclude_automatic_enable + 1] = server
else
ensure_installed[#ensure_installed + 1] = server
end end
else
exclude_automatic_enable[#exclude_automatic_enable + 1] = server
end end
end end
if have_mason then if have_mason then
local setup_config = { require("mason-lspconfig").setup({
ensure_installed = vim.tbl_deep_extend( ensure_installed = vim.tbl_deep_extend(
"force", "force",
ensure_installed, ensure_installed,
LazyVim.opts("mason-lspconfig.nvim").ensure_installed or {} LazyVim.opts("mason-lspconfig.nvim").ensure_installed or {}
), ),
} automatic_enable = {
exclude = exclude_automatic_enable,
setup_config.automatic_enable = { },
exclude = exclude_automatic_enable, })
}
mlsp.setup(setup_config)
end end
if LazyVim.lsp.is_enabled("denols") and LazyVim.lsp.is_enabled("vtsls") then if LazyVim.lsp.is_enabled("denols") and LazyVim.lsp.is_enabled("vtsls") then