feat(lsp): implement vim.lsp.diagnostic.redraw() (#15203)

Add a new function to redraw diagnostics from the current diagnostic
cache, without receiving a "publishDiagnostics" message from the server.
This is already being done in two places in the Lua stdlib, so this
function unifies that functionality in addition to providing it to third
party plugins.

An example use case for this could be a command or key-binding for
toggling diagnostics virtual text. The virtual text configuration option
can be toggled using `vim.lsp.with` followed by
`vim.lsp.diagnostic.redraw()` to immediately redraw the diagnostics
with the updated setting.
This commit is contained in:
Gregory Anders 2021-07-29 10:02:17 -06:00 committed by GitHub
parent 3b6d95b5f6
commit 3521bf7672
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 21 deletions

View File

@ -1487,6 +1487,22 @@ on_publish_diagnostics({_}, {_}, {params}, {client_id}, {_}, {config})
• Sort diagnostics (and thus signs and virtual • Sort diagnostics (and thus signs and virtual
text) text)
redraw({bufnr}, {client_id}) *vim.lsp.diagnostic.redraw()*
Redraw diagnostics for the given buffer and client
This calls the "textDocument/publishDiagnostics" handler
manually using the cached diagnostics already received from
the server. This can be useful for redrawing diagnostics after
making changes in diagnostics configuration.
|lsp-handler-configuration|
Parameters: ~
{bufnr} (optional, number): Buffer handle, defaults
to current
{client_id} (optional, number): Redraw diagnostics for
the given client. The default is to redraw
diagnostics for all attached clients.
reset({client_id}, {buffer_client_map}) *vim.lsp.diagnostic.reset()* reset({client_id}, {buffer_client_map}) *vim.lsp.diagnostic.reset()*
Clear diagnotics and diagnostic cache Clear diagnotics and diagnostic cache

View File

@ -453,15 +453,7 @@ local function text_document_did_open_handler(bufnr, client)
-- Next chance we get, we should re-do the diagnostics -- Next chance we get, we should re-do the diagnostics
vim.schedule(function() vim.schedule(function()
vim.lsp.handlers["textDocument/publishDiagnostics"]( vim.lsp.diagnostic.redraw(bufnr, client.id)
nil,
"textDocument/publishDiagnostics",
{
diagnostics = vim.lsp.diagnostic.get(bufnr, client.id),
uri = vim.uri_from_bufnr(bufnr),
},
client.id
)
end) end)
end end

View File

@ -1168,6 +1168,40 @@ function M.display(diagnostics, bufnr, client_id, config)
save_extmarks(bufnr, client_id) save_extmarks(bufnr, client_id)
end end
--- Redraw diagnostics for the given buffer and client
---
--- This calls the "textDocument/publishDiagnostics" handler manually using
--- the cached diagnostics already received from the server. This can be useful
--- for redrawing diagnostics after making changes in diagnostics
--- configuration. |lsp-handler-configuration|
---
--- @param bufnr (optional, number): Buffer handle, defaults to current
--- @param client_id (optional, number): Redraw diagnostics for the given
--- client. The default is to redraw diagnostics for all attached
--- clients.
function M.redraw(bufnr, client_id)
bufnr = get_bufnr(bufnr)
if not client_id then
return vim.lsp.for_each_buffer_client(bufnr, function(client)
M.redraw(bufnr, client.id)
end)
end
-- We need to invoke the publishDiagnostics handler directly instead of just
-- calling M.display so that we can preserve any custom configuration options
-- the user may have set with vim.lsp.with.
vim.lsp.handlers["textDocument/publishDiagnostics"](
nil,
"textDocument/publishDiagnostics",
{
uri = vim.uri_from_bufnr(bufnr),
diagnostics = M.get(bufnr, client_id),
},
client_id,
bufnr
)
end
-- }}} -- }}}
-- Diagnostic User Functions {{{ -- Diagnostic User Functions {{{
@ -1361,18 +1395,7 @@ function M.enable(bufnr, client_id)
diagnostic_disabled[bufnr][client_id] = nil diagnostic_disabled[bufnr][client_id] = nil
-- We need to invoke the publishDiagnostics handler directly instead of just M.redraw(bufnr, client_id)
-- calling M.display so that we can preserve any custom configuration options
-- the user may have set with vim.lsp.with.
vim.lsp.handlers["textDocument/publishDiagnostics"](
nil,
"textDocument/publishDiagnostics",
{
diagnostics = M.get(bufnr, client_id),
uri = vim.uri_from_bufnr(bufnr),
},
client_id
)
end end
-- }}} -- }}}