Skip to content

Commit 79a3dd0

Browse files
committed
Clear diagnostics for known file version on close
1 parent 9792714 commit 79a3dd0

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

crates/rust-analyzer/src/main_loop.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,19 +432,26 @@ impl GlobalState {
432432
Ok(())
433433
})?
434434
.on::<lsp_types::notification::DidCloseTextDocument>(|this, params| {
435+
let mut version = None;
435436
if let Ok(path) = from_proto::vfs_path(&params.text_document.uri) {
436-
if this.mem_docs.remove(&path).is_none() {
437-
log::error!("orphan DidCloseTextDocument: {}", path)
437+
match this.mem_docs.remove(&path) {
438+
Some(entry) => version = entry,
439+
None => log::error!("orphan DidCloseTextDocument: {}", path),
438440
}
441+
439442
if let Some(path) = path.as_path() {
440443
this.loader.handle.invalidate(path.to_path_buf());
441444
}
442445
}
446+
447+
// Clear the diagnostics for the previously known version of the file.
448+
// This prevents stale "cargo check" diagnostics if the file is
449+
// closed, "cargo check" is run and then the file is reopened.
443450
this.send_notification::<lsp_types::notification::PublishDiagnostics>(
444451
lsp_types::PublishDiagnosticsParams {
445452
uri: params.text_document.uri,
446453
diagnostics: Vec::new(),
447-
version: None,
454+
version,
448455
},
449456
);
450457
Ok(())

0 commit comments

Comments
 (0)