nvim1/lua/functions/lsp.lua

87 lines
2.3 KiB
Lua

-- Get all LSP servers to configure
local function get_all_servers()
local servers = {}
local commons = require("commons")
-- Use configured LSP servers from commons
for _, server in ipairs(commons.lsp_servers.code) do
servers[server] = true
end
-- Auto-detect LSP servers installed by Mason using mason-lspconfig
local ok, mason_lspconfig = pcall(require, "mason-lspconfig")
if ok then
local installed_servers = mason_lspconfig.get_installed_servers()
for _, server in ipairs(installed_servers) do
servers[server] = true
end
end
return servers
end
-- Configure a single LSP server
local function configure_server(server, lspconfig, capabilities)
-- Only attempt to load .lua files from lsp folder
local lsp_config_path = "lsp." .. server
local lua_file_path = vim.fn.stdpath("config") .. "/lua/lsp/" .. server .. ".lua"
-- Check if the .lua file exists
if vim.fn.filereadable(lua_file_path) == 1 then
local ok, settings = pcall(require, lsp_config_path)
if ok then
if lspconfig then
settings.capabilities = capabilities
lspconfig[server].setup(settings)
else
vim.lsp.config(server, settings)
vim.lsp.enable(server)
end
else
-- Fallback to default config if .lua file exists but fails to load
if lspconfig then
lspconfig[server].setup({ capabilities = capabilities })
else
vim.lsp.config(server, {})
vim.lsp.enable(server)
end
end
else
if lspconfig then
lspconfig[server].setup({ capabilities = capabilities })
else
vim.lsp.config(server, {})
vim.lsp.enable(server)
end
end
end
if require("commons").tools.is_version_gte_0_11() then
-- Neovim 0.11+ native LSP configuration
vim.lsp.config("*", {
capabilities = require("blink.cmp").get_lsp_capabilities(),
root_markers = { ".git" },
})
-- Configure all servers
for server in pairs(get_all_servers()) do
configure_server(server)
end
vim.diagnostic.config({
update_in_insert = true,
severity_sort = true,
float = { source = true },
virtual_text = true,
})
else
-- Legacy nvim-lspconfig setup for Neovim < 0.11
local lspconfig = require("lspconfig")
local capabilities = require("blink.cmp").get_lsp_capabilities()
-- Configure all servers
for server in pairs(get_all_servers()) do
configure_server(server, lspconfig, capabilities)
end
end