From 016335a7d09fca82cd6065ca4f54101c27249d01 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Mon, 1 Dec 2025 08:56:25 +0800 Subject: [PATCH] feat(lsp): support refreshing workspace diagnostics --- runtime/lua/vim/lsp/diagnostic.lua | 14 ++++-- .../functional/plugin/lsp/diagnostic_spec.lua | 47 +++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/runtime/lua/vim/lsp/diagnostic.lua b/runtime/lua/vim/lsp/diagnostic.lua index 9b62cbc55f..80c3501467 100644 --- a/runtime/lua/vim/lsp/diagnostic.lua +++ b/runtime/lua/vim/lsp/diagnostic.lua @@ -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 diff --git a/test/functional/plugin/lsp/diagnostic_spec.lua b/test/functional/plugin/lsp/diagnostic_spec.lua index b984540448..adb7397150 100644 --- a/test/functional/plugin/lsp/diagnostic_spec.lua +++ b/test/functional/plugin/lsp/diagnostic_spec.lua @@ -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)