Skip to content

Commit 4be8e5a

Browse files
committed
add FlycheckStatus to global state
1 parent eeda60d commit 4be8e5a

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ 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) diagnostics_received: FxHashMap<usize, bool>,
90+
pub(crate) flycheck_status: FxHashMap<usize, FlycheckStatus>,
9191

9292
// Test explorer
9393
pub(crate) test_run_session: Option<Vec<flycheck::CargoTestHandle>>,
@@ -166,6 +166,14 @@ pub(crate) struct GlobalStateSnapshot {
166166
pub(crate) flycheck: Arc<[FlycheckHandle]>,
167167
}
168168

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

171179
impl GlobalState {
@@ -225,7 +233,7 @@ impl GlobalState {
225233
flycheck_sender,
226234
flycheck_receiver,
227235
last_flycheck_error: None,
228-
diagnostics_received: FxHashMap::default(),
236+
flycheck_status: FxHashMap::default(),
229237

230238
test_run_session: None,
231239
test_run_sender,
@@ -513,3 +521,12 @@ pub(crate) fn url_to_file_id(vfs: &vfs::Vfs, url: &Url) -> anyhow::Result<FileId
513521
let res = vfs.file_id(&path).ok_or_else(|| anyhow::format_err!("file not found: {path}"))?;
514522
Ok(res)
515523
}
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: 15 additions & 5 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, GlobalState},
22+
global_state::{file_id_to_url, url_to_file_id, FlycheckStatus, GlobalState},
2323
hack_recover_crate_name,
2424
lsp::{
2525
from_proto, to_proto,
@@ -804,10 +804,13 @@ impl GlobalState {
804804
fn handle_flycheck_msg(&mut self, message: flycheck::Message) {
805805
match message {
806806
flycheck::Message::AddDiagnostic { id, workspace_root, diagnostic } => {
807-
if !self.diagnostics_received.get(&id).copied().unwrap_or_default() {
807+
let flycheck_status =
808+
self.flycheck_status.entry(id).or_insert(FlycheckStatus::Unknown);
809+
810+
if !flycheck_status.should_clear_old_diagnostics() {
808811
self.diagnostics.clear_check(id);
809-
self.diagnostics_received.insert(id, true);
810812
}
813+
*flycheck_status = FlycheckStatus::DiagnosticReceived;
811814
let snap = self.snapshot();
812815
let diagnostics = crate::diagnostics::to_proto::map_rust_diagnostic_to_lsp(
813816
&self.config.diagnostics_map(),
@@ -836,25 +839,32 @@ impl GlobalState {
836839
flycheck::Message::Progress { id, progress } => {
837840
let (state, message) = match progress {
838841
flycheck::Progress::DidStart => {
839-
self.diagnostics_received.insert(id, false);
842+
self.flycheck_status.insert(id, FlycheckStatus::Started);
840843
(Progress::Begin, None)
841844
}
842845
flycheck::Progress::DidCheckCrate(target) => (Progress::Report, Some(target)),
843846
flycheck::Progress::DidCancel => {
844847
self.last_flycheck_error = None;
848+
// We do not clear
849+
self.flycheck_status.insert(id, FlycheckStatus::Finished);
845850
(Progress::End, None)
846851
}
847852
flycheck::Progress::DidFailToRestart(err) => {
848853
self.last_flycheck_error =
849854
Some(format!("cargo check failed to start: {err}"));
855+
self.flycheck_status.insert(id, FlycheckStatus::Finished);
850856
return;
851857
}
852858
flycheck::Progress::DidFinish(result) => {
853859
self.last_flycheck_error =
854860
result.err().map(|err| format!("cargo check failed to start: {err}"));
855-
if !self.diagnostics_received.get(&id).copied().unwrap_or_default() {
861+
let flycheck_status =
862+
self.flycheck_status.entry(id).or_insert(FlycheckStatus::Unknown);
863+
864+
if !flycheck_status.should_clear_old_diagnostics() {
856865
self.diagnostics.clear_check(id);
857866
}
867+
*flycheck_status = FlycheckStatus::Finished;
858868
(Progress::End, None)
859869
}
860870
};

0 commit comments

Comments
 (0)