lsp: Store diagnostics for unloaded buffers (#13102)

To avoid loading buffers https://github.com/neovim/neovim/pull/12440
changed the logic to not process diagnostics for unloaded buffers.

This is problematic for language servers where compile errors or build
errors are reported via diagnostics. These errors may prevent the
language server from providing all functions and it is difficult for
users to debug it without having access to the errors.

For example, with eclipse.jdt.ls there may be a problem with gradle (the
build tool for java), it results in a diagnostics like this:

    org.gradle.toolingapi/build.gradle|1 col 1| Could not run build action using Gradle distribution 'https://services.gradle.org/distributions/gradle-4.8.1-bin.zip'.

This would be invisible to users unless the user happens to open the
right file. In this case the user would actually never see the error,
because the language server isn't attached to the build configuration
files.

This changes the behaviour to at least store the diagnostics. The other
operations which are more expensive are still skipped.
This commit is contained in:
Mathias Fußenegger 2020-10-25 19:27:11 +01:00 committed by GitHub
parent c984a888b8
commit 7fef16e1d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -82,18 +82,6 @@ M['textDocument/publishDiagnostics'] = function(_, _, result)
return return
end end
-- Unloaded buffers should not handle diagnostics.
-- When the buffer is loaded, we'll call on_attach, which sends textDocument/didOpen.
-- This should trigger another publish of the diagnostics.
--
-- In particular, this stops a ton of spam when first starting a server for current
-- unloaded buffers.
if not api.nvim_buf_is_loaded(bufnr) then
return
end
util.buf_clear_diagnostics(bufnr)
-- https://microsoft.github.io/language-server-protocol/specifications/specification-current/#diagnostic -- https://microsoft.github.io/language-server-protocol/specifications/specification-current/#diagnostic
-- The diagnostic's severity. Can be omitted. If omitted it is up to the -- The diagnostic's severity. Can be omitted. If omitted it is up to the
-- client to interpret diagnostics as error, warning, info or hint. -- client to interpret diagnostics as error, warning, info or hint.
@ -104,7 +92,23 @@ M['textDocument/publishDiagnostics'] = function(_, _, result)
end end
end end
util.buf_clear_diagnostics(bufnr)
-- Always save the diagnostics, even if the buf is not loaded.
-- Language servers may report compile or build errors via diagnostics
-- Users should be able to find these, even if they're in files which
-- are not loaded.
util.buf_diagnostics_save_positions(bufnr, result.diagnostics) util.buf_diagnostics_save_positions(bufnr, result.diagnostics)
-- Unloaded buffers should not handle diagnostics.
-- When the buffer is loaded, we'll call on_attach, which sends textDocument/didOpen.
-- This should trigger another publish of the diagnostics.
--
-- In particular, this stops a ton of spam when first starting a server for current
-- unloaded buffers.
if not api.nvim_buf_is_loaded(bufnr) then
return
end
util.buf_diagnostics_underline(bufnr, result.diagnostics) util.buf_diagnostics_underline(bufnr, result.diagnostics)
util.buf_diagnostics_virtual_text(bufnr, result.diagnostics) util.buf_diagnostics_virtual_text(bufnr, result.diagnostics)
util.buf_diagnostics_signs(bufnr, result.diagnostics) util.buf_diagnostics_signs(bufnr, result.diagnostics)