feat(lsp): support refreshing workspace diagnostics

This commit is contained in:
Pig Fang
2025-12-01 08:56:25 +08:00
committed by Lewis Russell
parent a7c21f4b17
commit 016335a7d0
2 changed files with 58 additions and 3 deletions

View File

@@ -395,9 +395,17 @@ function M.on_refresh(err, _, ctx)
if err then
return vim.NIL
end
for bufnr in pairs(vim.lsp.get_client_by_id(ctx.client_id).attached_buffers or {}) do
if bufstates[bufnr] and bufstates[bufnr].pull_kind == 'document' then
refresh(bufnr)
local client = vim.lsp.get_client_by_id(ctx.client_id)
if
client.server_capabilities.diagnosticProvider
and client.server_capabilities.diagnosticProvider.workspaceDiagnostics
then
M._workspace_diagnostics(ctx)
else
for bufnr in pairs(client.attached_buffers or {}) do
if bufstates[bufnr] and bufstates[bufnr].pull_kind == 'document' then
refresh(bufnr)
end
end
end

View File

@@ -673,5 +673,52 @@ describe('vim.lsp.diagnostic', function()
eq(1, #diags)
eq(2, diags[1].severity)
end)
it('refreshes workspace diagnostics', function()
local fake_uri_3 = 'file:///fake/uri3'
exec_lua(create_server_definition)
exec_lua(function()
_G.requests = 0
_G.server = _G._create_server({
capabilities = {
diagnosticProvider = { workspaceDiagnostics = true },
},
handlers = {
['workspace/diagnostic'] = function(_, _, callback)
_G.requests = _G.requests + 1
callback(nil, {
items = {
{
kind = 'full',
uri = fake_uri_3,
items = {
_G.make_error('Workspace Diagnostic', 4, 4, 4, 4),
},
},
},
})
end,
},
})
client_id = vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd })
end)
local diags = exec_lua(function()
return vim.diagnostic.get()
end)
eq(0, #diags)
local requests, diags = exec_lua(function()
vim.lsp.diagnostic.on_refresh(nil, nil, {
method = 'workspace/diagnostic/refresh',
client_id = client_id,
})
return _G.requests, vim.diagnostic.get()
end)
eq(1, requests)
eq(1, #diags)
eq(1, diags[1].severity)
end)
end)
end)