Skip to content

Commit 25391e6

Browse files
committed
Only clear diagnostics of workspaces who have been flychecked
1 parent a63b5d3 commit 25391e6

File tree

6 files changed

+33
-14
lines changed

6 files changed

+33
-14
lines changed

crates/flycheck/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl FlycheckHandle {
8686

8787
pub enum Message {
8888
/// Request adding a diagnostic with fixes included to a file
89-
AddDiagnostic { workspace_root: AbsPathBuf, diagnostic: Diagnostic },
89+
AddDiagnostic { id: usize, workspace_root: AbsPathBuf, diagnostic: Diagnostic },
9090

9191
/// Request check progress notification to client
9292
Progress {
@@ -99,8 +99,9 @@ pub enum Message {
9999
impl fmt::Debug for Message {
100100
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101101
match self {
102-
Message::AddDiagnostic { workspace_root, diagnostic } => f
102+
Message::AddDiagnostic { id, workspace_root, diagnostic } => f
103103
.debug_struct("AddDiagnostic")
104+
.field("id", id)
104105
.field("workspace_root", workspace_root)
105106
.field("diagnostic_code", &diagnostic.code.as_ref().map(|it| &it.code))
106107
.finish(),
@@ -186,7 +187,7 @@ impl FlycheckActor {
186187
}
187188
}
188189
Event::CheckEvent(None) => {
189-
tracing::debug!("flycheck finished");
190+
tracing::debug!(flycheck_id = self.id, "flycheck finished");
190191

191192
// Watcher finished
192193
let cargo_handle = self.cargo_handle.take().unwrap();
@@ -206,6 +207,7 @@ impl FlycheckActor {
206207

207208
CargoMessage::Diagnostic(msg) => {
208209
self.send(Message::AddDiagnostic {
210+
id: self.id,
209211
workspace_root: self.workspace_root.clone(),
210212
diagnostic: msg,
211213
});

crates/rust-analyzer/src/diagnostics.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
88

99
use crate::lsp_ext;
1010

11-
pub(crate) type CheckFixes = Arc<FxHashMap<FileId, Vec<Fix>>>;
11+
pub(crate) type CheckFixes = Arc<FxHashMap<usize, FxHashMap<FileId, Vec<Fix>>>>;
1212

1313
#[derive(Debug, Default, Clone)]
1414
pub struct DiagnosticsMapConfig {
@@ -22,7 +22,7 @@ pub(crate) struct DiagnosticCollection {
2222
// FIXME: should be FxHashMap<FileId, Vec<ra_id::Diagnostic>>
2323
pub(crate) native: FxHashMap<FileId, Vec<lsp_types::Diagnostic>>,
2424
// FIXME: should be Vec<flycheck::Diagnostic>
25-
pub(crate) check: FxHashMap<FileId, Vec<lsp_types::Diagnostic>>,
25+
pub(crate) check: FxHashMap<usize, FxHashMap<FileId, Vec<lsp_types::Diagnostic>>>,
2626
pub(crate) check_fixes: CheckFixes,
2727
changes: FxHashSet<FileId>,
2828
}
@@ -35,9 +35,19 @@ pub(crate) struct Fix {
3535
}
3636

3737
impl DiagnosticCollection {
38-
pub(crate) fn clear_check(&mut self) {
38+
pub(crate) fn clear_check(&mut self, flycheck_id: usize) {
39+
if let Some(it) = Arc::make_mut(&mut self.check_fixes).get_mut(&flycheck_id) {
40+
it.clear();
41+
}
42+
if let Some(it) = self.check.get_mut(&flycheck_id) {
43+
self.changes.extend(it.drain().map(|(key, _value)| key));
44+
}
45+
}
46+
47+
pub(crate) fn clear_check_all(&mut self) {
3948
Arc::make_mut(&mut self.check_fixes).clear();
40-
self.changes.extend(self.check.drain().map(|(key, _value)| key))
49+
self.changes
50+
.extend(self.check.values_mut().flat_map(|it| it.drain().map(|(key, _value)| key)))
4151
}
4252

4353
pub(crate) fn clear_native_for(&mut self, file_id: FileId) {
@@ -47,20 +57,22 @@ impl DiagnosticCollection {
4757

4858
pub(crate) fn add_check_diagnostic(
4959
&mut self,
60+
flycheck_id: usize,
5061
file_id: FileId,
5162
diagnostic: lsp_types::Diagnostic,
5263
fix: Option<Fix>,
5364
) {
54-
let diagnostics = self.check.entry(file_id).or_default();
65+
let diagnostics = self.check.entry(flycheck_id).or_default().entry(file_id).or_default();
5566
for existing_diagnostic in diagnostics.iter() {
5667
if are_diagnostics_equal(existing_diagnostic, &diagnostic) {
5768
return;
5869
}
5970
}
6071

6172
let check_fixes = Arc::make_mut(&mut self.check_fixes);
62-
check_fixes.entry(file_id).or_default().extend(fix);
73+
check_fixes.entry(flycheck_id).or_default().entry(file_id).or_default().extend(fix);
6374
diagnostics.push(diagnostic);
75+
tracing::warn!(?flycheck_id, ?file_id, "add_check_diagnostic changes pushed");
6476
self.changes.insert(file_id);
6577
}
6678

@@ -89,7 +101,8 @@ impl DiagnosticCollection {
89101
file_id: FileId,
90102
) -> impl Iterator<Item = &lsp_types::Diagnostic> {
91103
let native = self.native.get(&file_id).into_iter().flatten();
92-
let check = self.check.get(&file_id).into_iter().flatten();
104+
let check =
105+
self.check.values().filter_map(move |it| it.get(&file_id)).into_iter().flatten();
93106
native.chain(check)
94107
}
95108

crates/rust-analyzer/src/global_state.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ impl GlobalState {
201201
}
202202
}
203203

204+
// Clear native diagnostics when their file gets deleted
204205
if !file.exists() {
205206
self.diagnostics.clear_native_for(file.file_id);
206207
}

crates/rust-analyzer/src/handlers.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,9 @@ pub(crate) fn handle_code_action(
10941094
}
10951095

10961096
// Fixes from `cargo check`.
1097-
for fix in snap.check_fixes.get(&frange.file_id).into_iter().flatten() {
1097+
for fix in
1098+
snap.check_fixes.values().filter_map(|it| it.get(&frange.file_id)).into_iter().flatten()
1099+
{
10981100
// FIXME: this mapping is awkward and shouldn't exist. Refactor
10991101
// `snap.check_fixes` to not convert to LSP prematurely.
11001102
let intersect_fix_range = fix

crates/rust-analyzer/src/main_loop.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ impl GlobalState {
372372
let _p = profile::span("GlobalState::handle_event/flycheck");
373373
loop {
374374
match task {
375-
flycheck::Message::AddDiagnostic { workspace_root, diagnostic } => {
375+
flycheck::Message::AddDiagnostic { id, workspace_root, diagnostic } => {
376376
let snap = self.snapshot();
377377
let diagnostics =
378378
crate::diagnostics::to_proto::map_rust_diagnostic_to_lsp(
@@ -384,6 +384,7 @@ impl GlobalState {
384384
for diag in diagnostics {
385385
match url_to_file_id(&self.vfs.read().0, &diag.url) {
386386
Ok(file_id) => self.diagnostics.add_check_diagnostic(
387+
id,
387388
file_id,
388389
diag.diagnostic,
389390
diag.fix,
@@ -401,7 +402,7 @@ impl GlobalState {
401402
flycheck::Message::Progress { id, progress } => {
402403
let (state, message) = match progress {
403404
flycheck::Progress::DidStart => {
404-
self.diagnostics.clear_check();
405+
self.diagnostics.clear_check(id);
405406
(Progress::Begin, None)
406407
}
407408
flycheck::Progress::DidCheckCrate(target) => {

crates/rust-analyzer/src/reload.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ impl GlobalState {
417417
Some(it) => it,
418418
None => {
419419
self.flycheck = Vec::new();
420-
self.diagnostics.clear_check();
420+
self.diagnostics.clear_check_all();
421421
return;
422422
}
423423
};

0 commit comments

Comments
 (0)