fix project settings regression

This commit is contained in:
Ray Elliott 2025-12-07 19:50:54 +00:00
commit 71beb0fb78
4 changed files with 184 additions and 26 deletions

View file

@ -9,6 +9,9 @@ return {
-- Load project-local LSP settings from .nvim.lua
local function load_project_lsp_settings(server_name, root_dir)
if not root_dir then
return {}
end
local config_file = root_dir .. "/.nvim.lua"
if vim.fn.filereadable(config_file) == 1 then
local ok, project_config = pcall(dofile, config_file)
@ -19,14 +22,8 @@ return {
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
-- Setup keymaps on attach
local function setup_keymaps(bufnr)
local map = function(mode, lhs, rhs, desc)
vim.keymap.set(mode, lhs, rhs, { buffer = bufnr, silent = true, noremap = true, desc = desc })
end
@ -37,6 +34,36 @@ return {
map('n', 'K', vim.lsp.buf.hover, 'LSP: Hover')
end
-- Global LspAttach handler to load project settings and setup keymaps
vim.api.nvim_create_autocmd('LspAttach', {
group = vim.api.nvim_create_augroup('lsp_project_settings', { clear = true }),
callback = function(args)
local client = vim.lsp.get_client_by_id(args.data.client_id)
if not client then
return
end
-- Setup keymaps for this buffer
setup_keymaps(args.buf)
-- Load and send project-specific settings
local project_settings = load_project_lsp_settings(client.name, client.root_dir)
if project_settings.settings then
-- Merge with existing settings
client.settings = vim.tbl_deep_extend(
"force",
client.settings or {},
project_settings.settings
)
-- Send didChangeConfiguration to update the server
vim.schedule(function()
client.notify("workspace/didChangeConfiguration", { settings = client.settings })
end)
end
end,
})
-- Configure servers using vim.lsp.config (Neovim 0.11+ API)
local function configure(server_name, opts)
opts = opts or {}
@ -44,18 +71,6 @@ return {
-- 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