feat(lsp): allow diagnostics to be disabled for a buffer (#15134)

Add two new methods to allow diagnostics to be disabled (and re-enabled)
in the current buffer. When diagnostics are disabled they are simply not
displayed to the user, but they are still sent by the server and
processed by the client.

Disabling diagnostics can be helpful in a number of scenarios. For
example, if one is working on a buffer with an overwhelming amount of
diagnostic warnings it can be helpful to simply disable diagnostics
without disabling the LSP client entirely. This also allows users more
flexibility on when and how they may want diagnostic information to be
displayed. For example, some users may not want to display diagnostic
information until after the buffer is first written.
This commit is contained in:
Gregory Anders 2021-07-19 12:49:55 -06:00 committed by GitHub
parent c487a73fa2
commit 1aeb945553
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 90 additions and 0 deletions

View File

@ -208,6 +208,9 @@ local diagnostic_cache_lines = setmetatable({}, bufnr_and_client_cacher_mt)
local diagnostic_cache_counts = setmetatable({}, bufnr_and_client_cacher_mt)
local diagnostic_attached_buffers = {}
-- Disabled buffers and clients
local diagnostic_disabled = setmetatable({}, bufnr_and_client_cacher_mt)
local _bufs_waiting_to_update = setmetatable({}, bufnr_and_client_cacher_mt)
--- Store Diagnostic[] by line
@ -1092,6 +1095,10 @@ end
--@private
--- Display diagnostics for the buffer, given a configuration.
function M.display(diagnostics, bufnr, client_id, config)
if diagnostic_disabled[bufnr][client_id] then
return
end
config = vim.lsp._with_extend('vim.lsp.diagnostic.on_publish_diagnostics', {
signs = true,
underline = true,
@ -1282,6 +1289,57 @@ function M.set_loclist(opts)
vim.cmd [[lopen]]
end
end
--- Disable diagnostics for the given buffer and client
--- @param bufnr (optional, number): Buffer handle, defaults to current
--- @param client_id (optional, number): Disable diagnostics for the given
--- client. The default is to disable diagnostics for all attached
--- clients.
-- Note that when diagnostics are disabled for a buffer, the server will still
-- send diagnostic information and the client will still process it. The
-- diagnostics are simply not displayed to the user.
function M.disable(bufnr, client_id)
if not client_id then
return vim.lsp.for_each_buffer_client(bufnr, function(client)
M.disable(bufnr, client.id)
end)
end
diagnostic_disabled[bufnr][client_id] = true
M.clear(bufnr, client_id)
end
--- Enable diagnostics for the given buffer and client
--- @param bufnr (optional, number): Buffer handle, defaults to current
--- @param client_id (optional, number): Enable diagnostics for the given
--- client. The default is to enable diagnostics for all attached
--- clients.
function M.enable(bufnr, client_id)
if not client_id then
return vim.lsp.for_each_buffer_client(bufnr, function(client)
M.enable(bufnr, client.id)
end)
end
if not diagnostic_disabled[bufnr][client_id] then
return
end
diagnostic_disabled[bufnr][client_id] = nil
-- 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",
{
diagnostics = M.get(bufnr, client_id),
uri = vim.uri_from_bufnr(bufnr),
},
client_id
)
end
-- }}}
return M

View File

@ -241,6 +241,38 @@ describe('vim.lsp.diagnostic', function()
]]))
end)
it('should not display diagnostics when disabled', function()
eq({0, 2}, exec_lua [[
local server_1_diags = {
make_error("Error 1", 1, 1, 1, 5),
make_warning("Warning on Server 1", 2, 1, 2, 5),
}
local server_2_diags = {
make_warning("Warning 1", 2, 1, 2, 5),
}
vim.lsp.diagnostic.on_publish_diagnostics(nil, nil, { uri = fake_uri, diagnostics = server_1_diags }, 1)
vim.lsp.diagnostic.on_publish_diagnostics(nil, nil, { uri = fake_uri, diagnostics = server_2_diags }, 2)
vim.lsp.diagnostic.disable(diagnostic_bufnr, 1)
return {
count_of_extmarks_for_client(diagnostic_bufnr, 1),
count_of_extmarks_for_client(diagnostic_bufnr, 2),
}
]])
eq({4, 0}, exec_lua [[
vim.lsp.diagnostic.enable(diagnostic_bufnr, 1)
vim.lsp.diagnostic.disable(diagnostic_bufnr, 2)
return {
count_of_extmarks_for_client(diagnostic_bufnr, 1),
count_of_extmarks_for_client(diagnostic_bufnr, 2),
}
]])
end)
describe('reset', function()
it('diagnostic count is 0 and displayed diagnostics are 0 after call', function()
-- 1 Error (1)