Avoid endless loop of the diagnostic updates (#21209)

Follow-up of https://github.com/zed-industries/zed/pull/21173

Rust-analyzer with `checkOnSave` enabled will push diagnostics for a
file after each diagnostics refresh (e.g. save, file open, file close).

If there's a file that is not open in any pane and has only warnings,
and the diagnostics editor has warnings toggled off, then

0. rust-analyzer will push the corresponding warnings after initial load
1. the diagnostics editor code registers
`project::Event::DiagnosticsUpdated` for the corresponding file path and
opens the corresponding buffer to read its associated diagnostics from
the snapshot
2. opening the buffer would send `textDocument/didOpen` which would
trigger rust-analyzer to push the same diagnostics
3. meanwhile, the diagnostics editor would filter out all diagnostics
for that buffer, dropping the open buffer and effectively closing it
4. closing the buffer will send `textDocument/didClose` which would
trigger rust-analyzer to push the same diagnostics again, as those are
`cargo check` ones, still present in the file
5. GOTO 1

Release Notes:

- Fixed diagnostics editor not scrolling properly under certain
conditions
This commit is contained in:
Kirill Bulatov 2024-11-26 14:29:54 +02:00 committed by GitHub
parent 7d67bb4cf6
commit 9999c31859
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 43 additions and 13 deletions

View file

@ -2919,6 +2919,21 @@ impl LspStore {
})
}
pub fn diagnostics_for_buffer(
&self,
path: &ProjectPath,
) -> Option<
&[(
LanguageServerId,
Vec<DiagnosticEntry<Unclipped<PointUtf16>>>,
)],
> {
self.diagnostics
.get(&path.worktree_id)?
.get(&path.path)
.map(|diagnostics| diagnostics.as_slice())
}
pub fn started_language_servers(&self) -> Vec<(WorktreeId, LanguageServerName)> {
self.language_server_ids.keys().cloned().collect()
}