Skip to content

Commit f476d37

Browse files
committed
Move state trackig of diagnostic clearing inside FlycheckActor
1 parent 4be8e5a commit f476d37

File tree

3 files changed

+31
-41
lines changed

3 files changed

+31
-41
lines changed

src/tools/rust-analyzer/crates/flycheck/src/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ pub enum Message {
163163
/// Request adding a diagnostic with fixes included to a file
164164
AddDiagnostic { id: usize, workspace_root: AbsPathBuf, diagnostic: Diagnostic },
165165

166+
/// Request clearing all previous diagnostics
167+
ClearDiagnostics { id: usize },
168+
166169
/// Request check progress notification to client
167170
Progress {
168171
/// Flycheck instance ID
@@ -180,6 +183,9 @@ impl fmt::Debug for Message {
180183
.field("workspace_root", workspace_root)
181184
.field("diagnostic_code", &diagnostic.code.as_ref().map(|it| &it.code))
182185
.finish(),
186+
Message::ClearDiagnostics { id } => {
187+
f.debug_struct("ClearDiagnostics").field("id", id).finish()
188+
}
183189
Message::Progress { id, progress } => {
184190
f.debug_struct("Progress").field("id", id).field("progress", progress).finish()
185191
}
@@ -220,13 +226,22 @@ struct FlycheckActor {
220226
command_handle: Option<CommandHandle<CargoCheckMessage>>,
221227
/// The receiver side of the channel mentioned above.
222228
command_receiver: Option<Receiver<CargoCheckMessage>>,
229+
230+
status: FlycheckStatus,
223231
}
224232

225233
enum Event {
226234
RequestStateChange(StateChange),
227235
CheckEvent(Option<CargoCheckMessage>),
228236
}
229237

238+
#[derive(PartialEq)]
239+
enum FlycheckStatus {
240+
Started,
241+
DiagnosticSent,
242+
Finished,
243+
}
244+
230245
const SAVED_FILE_PLACEHOLDER: &str = "$saved_file";
231246

232247
impl FlycheckActor {
@@ -248,6 +263,7 @@ impl FlycheckActor {
248263
manifest_path,
249264
command_handle: None,
250265
command_receiver: None,
266+
status: FlycheckStatus::Finished,
251267
}
252268
}
253269

@@ -298,12 +314,14 @@ impl FlycheckActor {
298314
self.command_handle = Some(command_handle);
299315
self.command_receiver = Some(receiver);
300316
self.report_progress(Progress::DidStart);
317+
self.status = FlycheckStatus::Started;
301318
}
302319
Err(error) => {
303320
self.report_progress(Progress::DidFailToRestart(format!(
304321
"Failed to run the following command: {} error={}",
305322
formatted_command, error
306323
)));
324+
self.status = FlycheckStatus::Finished;
307325
}
308326
}
309327
}
@@ -323,7 +341,11 @@ impl FlycheckActor {
323341
error
324342
);
325343
}
344+
if self.status == FlycheckStatus::Started {
345+
self.send(Message::ClearDiagnostics { id: self.id });
346+
}
326347
self.report_progress(Progress::DidFinish(res));
348+
self.status = FlycheckStatus::Finished;
327349
}
328350
Event::CheckEvent(Some(message)) => match message {
329351
CargoCheckMessage::CompilerArtifact(msg) => {
@@ -341,11 +363,15 @@ impl FlycheckActor {
341363
message = msg.message,
342364
"diagnostic received"
343365
);
366+
if self.status == FlycheckStatus::Started {
367+
self.send(Message::ClearDiagnostics { id: self.id });
368+
}
344369
self.send(Message::AddDiagnostic {
345370
id: self.id,
346371
workspace_root: self.root.clone(),
347372
diagnostic: msg,
348373
});
374+
self.status = FlycheckStatus::DiagnosticSent;
349375
}
350376
},
351377
}
@@ -362,6 +388,7 @@ impl FlycheckActor {
362388
);
363389
command_handle.cancel();
364390
self.report_progress(Progress::DidCancel);
391+
self.status = FlycheckStatus::Finished;
365392
}
366393
}
367394

src/tools/rust-analyzer/crates/rust-analyzer/src/global_state.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ pub(crate) struct GlobalState {
8787
pub(crate) flycheck_sender: Sender<flycheck::Message>,
8888
pub(crate) flycheck_receiver: Receiver<flycheck::Message>,
8989
pub(crate) last_flycheck_error: Option<String>,
90-
pub(crate) flycheck_status: FxHashMap<usize, FlycheckStatus>,
9190

9291
// Test explorer
9392
pub(crate) test_run_session: Option<Vec<flycheck::CargoTestHandle>>,
@@ -166,14 +165,6 @@ pub(crate) struct GlobalStateSnapshot {
166165
pub(crate) flycheck: Arc<[FlycheckHandle]>,
167166
}
168167

169-
#[derive(Debug, Clone)]
170-
pub(crate) enum FlycheckStatus {
171-
Unknown,
172-
Started,
173-
DiagnosticReceived,
174-
Finished,
175-
}
176-
177168
impl std::panic::UnwindSafe for GlobalStateSnapshot {}
178169

179170
impl GlobalState {
@@ -233,7 +224,6 @@ impl GlobalState {
233224
flycheck_sender,
234225
flycheck_receiver,
235226
last_flycheck_error: None,
236-
flycheck_status: FxHashMap::default(),
237227

238228
test_run_session: None,
239229
test_run_sender,
@@ -521,12 +511,3 @@ pub(crate) fn url_to_file_id(vfs: &vfs::Vfs, url: &Url) -> anyhow::Result<FileId
521511
let res = vfs.file_id(&path).ok_or_else(|| anyhow::format_err!("file not found: {path}"))?;
522512
Ok(res)
523513
}
524-
525-
impl FlycheckStatus {
526-
pub(crate) fn should_clear_old_diagnostics(&self) -> bool {
527-
match self {
528-
FlycheckStatus::Unknown | FlycheckStatus::Started => false,
529-
FlycheckStatus::DiagnosticReceived | FlycheckStatus::Finished => true,
530-
}
531-
}
532-
}

src/tools/rust-analyzer/crates/rust-analyzer/src/main_loop.rs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
config::Config,
2020
diagnostics::fetch_native_diagnostics,
2121
dispatch::{NotificationDispatcher, RequestDispatcher},
22-
global_state::{file_id_to_url, url_to_file_id, FlycheckStatus, GlobalState},
22+
global_state::{file_id_to_url, url_to_file_id, GlobalState},
2323
hack_recover_crate_name,
2424
lsp::{
2525
from_proto, to_proto,
@@ -804,13 +804,6 @@ impl GlobalState {
804804
fn handle_flycheck_msg(&mut self, message: flycheck::Message) {
805805
match message {
806806
flycheck::Message::AddDiagnostic { id, workspace_root, diagnostic } => {
807-
let flycheck_status =
808-
self.flycheck_status.entry(id).or_insert(FlycheckStatus::Unknown);
809-
810-
if !flycheck_status.should_clear_old_diagnostics() {
811-
self.diagnostics.clear_check(id);
812-
}
813-
*flycheck_status = FlycheckStatus::DiagnosticReceived;
814807
let snap = self.snapshot();
815808
let diagnostics = crate::diagnostics::to_proto::map_rust_diagnostic_to_lsp(
816809
&self.config.diagnostics_map(),
@@ -836,35 +829,24 @@ impl GlobalState {
836829
}
837830
}
838831

832+
flycheck::Message::ClearDiagnostics { id } => self.diagnostics.clear_check(id),
833+
839834
flycheck::Message::Progress { id, progress } => {
840835
let (state, message) = match progress {
841-
flycheck::Progress::DidStart => {
842-
self.flycheck_status.insert(id, FlycheckStatus::Started);
843-
(Progress::Begin, None)
844-
}
836+
flycheck::Progress::DidStart => (Progress::Begin, None),
845837
flycheck::Progress::DidCheckCrate(target) => (Progress::Report, Some(target)),
846838
flycheck::Progress::DidCancel => {
847839
self.last_flycheck_error = None;
848-
// We do not clear
849-
self.flycheck_status.insert(id, FlycheckStatus::Finished);
850840
(Progress::End, None)
851841
}
852842
flycheck::Progress::DidFailToRestart(err) => {
853843
self.last_flycheck_error =
854844
Some(format!("cargo check failed to start: {err}"));
855-
self.flycheck_status.insert(id, FlycheckStatus::Finished);
856845
return;
857846
}
858847
flycheck::Progress::DidFinish(result) => {
859848
self.last_flycheck_error =
860849
result.err().map(|err| format!("cargo check failed to start: {err}"));
861-
let flycheck_status =
862-
self.flycheck_status.entry(id).or_insert(FlycheckStatus::Unknown);
863-
864-
if !flycheck_status.should_clear_old_diagnostics() {
865-
self.diagnostics.clear_check(id);
866-
}
867-
*flycheck_status = FlycheckStatus::Finished;
868850
(Progress::End, None)
869851
}
870852
};

0 commit comments

Comments
 (0)