feat(lsp): support diagnostic refresh request

This commit is contained in:
Pig Fang
2025-11-26 16:28:23 +08:00
committed by Lewis Russell
parent fa8e4e65ca
commit 02067a9892
6 changed files with 43 additions and 1 deletions

View File

@@ -360,6 +360,7 @@ They are also listed below.
- `'workspace/applyEdit'`
- `'workspace/configuration'`
- `'workspace/executeCommand'`
- `'workspace/diagnostic/refresh'`
- `'workspace/inlayHint/refresh'`
- `'workspace/semanticTokens/refresh'`
- `'workspace/symbol'`

View File

@@ -282,6 +282,8 @@ LSP
• |vim.lsp.ClientConfig| has an `exit_timeout` field to control the timeout of
client force stopping. Defaults to `false`.
• |Client:stop()| now uses the `Client.exit_timeout` field to control the default of `force`.
• Support for `workspace/diagnostic/refresh`:
https://microsoft.github.io/language-server-protocol/specification/#diagnostic_refresh
LUA

View File

@@ -388,6 +388,26 @@ local function refresh(bufnr, client_id, only_visible)
end
end
--- |lsp-handler| for the method `workspace/diagnostic/refresh`
---@param ctx lsp.HandlerContext
---@private
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
for _, winid in ipairs(api.nvim_list_wins()) do
if api.nvim_win_get_buf(winid) == bufnr then
if bufstates[bufnr] and bufstates[bufnr].pull_kind == 'document' then
refresh(bufnr)
end
end
end
end
return vim.NIL
end
--- Enable pull diagnostics for a buffer
---@param bufnr (integer) Buffer handle, or 0 for current
function M._enable(bufnr)

View File

@@ -649,6 +649,11 @@ RSC['window/showDocument'] = function(_, params, ctx)
return { success = success or false }
end
---@see https://microsoft.github.io/language-server-protocol/specification/#diagnostic_refresh
RSC['workspace/diagnostic/refresh'] = function(err, result, ctx)
return vim.lsp.diagnostic.on_refresh(err, result, ctx)
end
---@see https://microsoft.github.io/language-server-protocol/specification/#workspace_inlayHint_refresh
RSC['workspace/inlayHint/refresh'] = function(err, result, ctx)
return vim.lsp.inlay_hint.on_refresh(err, result, ctx)

View File

@@ -609,7 +609,7 @@ function protocol.make_client_capabilities()
refreshSupport = true,
},
diagnostics = {
refreshSupport = false,
refreshSupport = true,
},
},
experimental = nil,

View File

@@ -607,5 +607,19 @@ describe('vim.lsp.diagnostic', function()
eq('related bad!', related_diagnostics[1].message)
eq('spongebob', relatedPreviousResultId)
end)
it('refreshes diagnostics on request', function()
eq(
1,
exec_lua(function()
vim.lsp.diagnostic.on_refresh(nil, nil, {
method = 'workspace/diagnostic/refresh',
client_id = client_id,
})
return _G.requests
end)
)
end)
end)
end)