nvim/lua/plugins/lsp.lua

88 lines
2.9 KiB
Lua

return {
"neovim/nvim-lspconfig",
event = { "BufReadPre", "BufNewFile" },
dependencies = {
"hrsh7th/cmp-nvim-lsp",
},
config = function()
local capabilities = require("cmp_nvim_lsp").default_capabilities()
-- Load project-local LSP settings from .nvim.lua
local function load_project_lsp_settings(server_name, root_dir)
local config_file = root_dir .. "/.nvim.lua"
if vim.fn.filereadable(config_file) == 1 then
local ok, project_config = pcall(dofile, config_file)
if ok and project_config and project_config.lsp and project_config.lsp[server_name] then
return project_config.lsp[server_name]
end
end
return {}
end
local function on_attach(client, bufnr)
-- Send settings to server after attach (many servers need didChangeConfiguration)
if client.config.settings and next(client.config.settings) then
vim.schedule(function()
client.notify("workspace/didChangeConfiguration", { settings = client.config.settings })
end)
end
local map = function(mode, lhs, rhs, desc)
vim.keymap.set(mode, lhs, rhs, { buffer = bufnr, silent = true, noremap = true, desc = desc })
end
map('n', 'gd', vim.lsp.buf.definition, 'LSP: Go to definition')
map('n', 'gr', vim.lsp.buf.references, 'LSP: References')
map('n', 'gD', vim.lsp.buf.declaration, 'LSP: Go to declaration')
map('n', 'gI', vim.lsp.buf.implementation, 'LSP: Go to implementation')
map('n', 'K', vim.lsp.buf.hover, 'LSP: Hover')
end
-- Configure servers using vim.lsp.config (Neovim 0.11+ API)
local function configure(server_name, opts)
opts = opts or {}
-- Merge with defaults
local config = vim.tbl_deep_extend("force", {
capabilities = capabilities,
on_attach = on_attach,
on_new_config = function(new_config, root_dir)
-- Load project-specific settings based on actual root_dir
local project_settings = load_project_lsp_settings(server_name, root_dir)
if project_settings.settings then
new_config.settings = vim.tbl_deep_extend(
"force",
new_config.settings or {},
project_settings.settings
)
end
end,
}, opts)
-- Configure the server using the new API
vim.lsp.config(server_name, config)
end
-- Configure all servers
configure("lua_ls", {
settings = {
Lua = {
diagnostics = { globals = { "vim" } },
},
},
})
configure("ts_ls")
configure("html")
configure("cssls")
configure("jsonls")
configure("bashls")
configure("marksman")
configure("intelephense", {
single_file_support = false,
})
-- Enable all configured servers
vim.lsp.enable({ "lua_ls", "ts_ls", "html", "cssls", "jsonls", "bashls", "marksman", "intelephense" })
end,
}