refactor(lsp): convert diagnostics to capability framework #40433

Problem:
Diagnostic tracking used a separate bufstates table and manual
LspDetach/LspNotify autocmd management via _enable()/_refresh(),
duplicating the lifecycle logic already provided by the Capability
framework. This caused inconsistencies in how client attach/detach and
buffer teardown were handled compared to other LSP features.

Solution:
Replace the ad-hoc bufstate tracking and _enable/_refresh pattern in
vim.lsp.diagnostic with a proper Diagnostics subclass of Capability.
This cleans up a few random places in the main lsp module and client
module that were poking the diagnostics. It also fixes some pre-existing
bugs and inconsistencies that were discovered:

- Refresh diagnostics immediately on attach instead of lazily by the
  first didOpen/didChange notification
- Fix Capability.active lookup in M.enable() to key by it_bufnr instead
  of the filter bufnr
- Set lsp defaults before calling the _text_document_did_open_handler in
  Client:on_attach() so defaults are there before any lsp notification
  occurs
- Log (and return early) on any error from a diagnostic request result
  instead of only returning early for server cancelled errors
This commit is contained in:
jdrouhard
2026-06-26 13:41:26 -05:00
committed by GitHub
parent 85718f9874
commit c98c93fcf8
7 changed files with 142 additions and 136 deletions

View File

@@ -871,9 +871,6 @@ function lsp._set_defaults(client, bufnr)
end, { buf = bufnr, desc = 'vim.lsp.buf.hover()' })
end
end)
if client:supports_method('textDocument/diagnostic') then
lsp.diagnostic._enable(bufnr)
end
end
--- @deprecated