add FlycheckStatus to global state

This commit is contained in:
David Mládek 2024-05-28 20:34:51 +02:00
parent eeda60ddd8
commit 4be8e5a372
2 changed files with 34 additions and 7 deletions

View File

@ -87,7 +87,7 @@ pub(crate) struct GlobalState {
pub(crate) flycheck_sender: Sender<flycheck::Message>, pub(crate) flycheck_sender: Sender<flycheck::Message>,
pub(crate) flycheck_receiver: Receiver<flycheck::Message>, pub(crate) flycheck_receiver: Receiver<flycheck::Message>,
pub(crate) last_flycheck_error: Option<String>, pub(crate) last_flycheck_error: Option<String>,
pub(crate) diagnostics_received: FxHashMap<usize, bool>, pub(crate) flycheck_status: FxHashMap<usize, FlycheckStatus>,
// Test explorer // Test explorer
pub(crate) test_run_session: Option<Vec<flycheck::CargoTestHandle>>, pub(crate) test_run_session: Option<Vec<flycheck::CargoTestHandle>>,
@ -166,6 +166,14 @@ pub(crate) struct GlobalStateSnapshot {
pub(crate) flycheck: Arc<[FlycheckHandle]>, pub(crate) flycheck: Arc<[FlycheckHandle]>,
} }
#[derive(Debug, Clone)]
pub(crate) enum FlycheckStatus {
Unknown,
Started,
DiagnosticReceived,
Finished,
}
impl std::panic::UnwindSafe for GlobalStateSnapshot {} impl std::panic::UnwindSafe for GlobalStateSnapshot {}
impl GlobalState { impl GlobalState {
@ -225,7 +233,7 @@ impl GlobalState {
flycheck_sender, flycheck_sender,
flycheck_receiver, flycheck_receiver,
last_flycheck_error: None, last_flycheck_error: None,
diagnostics_received: FxHashMap::default(), flycheck_status: FxHashMap::default(),
test_run_session: None, test_run_session: None,
test_run_sender, test_run_sender,
@ -513,3 +521,12 @@ pub(crate) fn url_to_file_id(vfs: &vfs::Vfs, url: &Url) -> anyhow::Result<FileId
let res = vfs.file_id(&path).ok_or_else(|| anyhow::format_err!("file not found: {path}"))?; let res = vfs.file_id(&path).ok_or_else(|| anyhow::format_err!("file not found: {path}"))?;
Ok(res) Ok(res)
} }
impl FlycheckStatus {
pub(crate) fn should_clear_old_diagnostics(&self) -> bool {
match self {
FlycheckStatus::Unknown | FlycheckStatus::Started => false,
FlycheckStatus::DiagnosticReceived | FlycheckStatus::Finished => true,
}
}
}

View File

@ -19,7 +19,7 @@ use crate::{
config::Config, config::Config,
diagnostics::fetch_native_diagnostics, diagnostics::fetch_native_diagnostics,
dispatch::{NotificationDispatcher, RequestDispatcher}, dispatch::{NotificationDispatcher, RequestDispatcher},
global_state::{file_id_to_url, url_to_file_id, GlobalState}, global_state::{file_id_to_url, url_to_file_id, FlycheckStatus, GlobalState},
hack_recover_crate_name, hack_recover_crate_name,
lsp::{ lsp::{
from_proto, to_proto, from_proto, to_proto,
@ -804,10 +804,13 @@ impl GlobalState {
fn handle_flycheck_msg(&mut self, message: flycheck::Message) { fn handle_flycheck_msg(&mut self, message: flycheck::Message) {
match message { match message {
flycheck::Message::AddDiagnostic { id, workspace_root, diagnostic } => { flycheck::Message::AddDiagnostic { id, workspace_root, diagnostic } => {
if !self.diagnostics_received.get(&id).copied().unwrap_or_default() { let flycheck_status =
self.flycheck_status.entry(id).or_insert(FlycheckStatus::Unknown);
if !flycheck_status.should_clear_old_diagnostics() {
self.diagnostics.clear_check(id); self.diagnostics.clear_check(id);
self.diagnostics_received.insert(id, true);
} }
*flycheck_status = FlycheckStatus::DiagnosticReceived;
let snap = self.snapshot(); let snap = self.snapshot();
let diagnostics = crate::diagnostics::to_proto::map_rust_diagnostic_to_lsp( let diagnostics = crate::diagnostics::to_proto::map_rust_diagnostic_to_lsp(
&self.config.diagnostics_map(), &self.config.diagnostics_map(),
@ -836,25 +839,32 @@ impl GlobalState {
flycheck::Message::Progress { id, progress } => { flycheck::Message::Progress { id, progress } => {
let (state, message) = match progress { let (state, message) = match progress {
flycheck::Progress::DidStart => { flycheck::Progress::DidStart => {
self.diagnostics_received.insert(id, false); self.flycheck_status.insert(id, FlycheckStatus::Started);
(Progress::Begin, None) (Progress::Begin, None)
} }
flycheck::Progress::DidCheckCrate(target) => (Progress::Report, Some(target)), flycheck::Progress::DidCheckCrate(target) => (Progress::Report, Some(target)),
flycheck::Progress::DidCancel => { flycheck::Progress::DidCancel => {
self.last_flycheck_error = None; self.last_flycheck_error = None;
// We do not clear
self.flycheck_status.insert(id, FlycheckStatus::Finished);
(Progress::End, None) (Progress::End, None)
} }
flycheck::Progress::DidFailToRestart(err) => { flycheck::Progress::DidFailToRestart(err) => {
self.last_flycheck_error = self.last_flycheck_error =
Some(format!("cargo check failed to start: {err}")); Some(format!("cargo check failed to start: {err}"));
self.flycheck_status.insert(id, FlycheckStatus::Finished);
return; return;
} }
flycheck::Progress::DidFinish(result) => { flycheck::Progress::DidFinish(result) => {
self.last_flycheck_error = self.last_flycheck_error =
result.err().map(|err| format!("cargo check failed to start: {err}")); result.err().map(|err| format!("cargo check failed to start: {err}"));
if !self.diagnostics_received.get(&id).copied().unwrap_or_default() { let flycheck_status =
self.flycheck_status.entry(id).or_insert(FlycheckStatus::Unknown);
if !flycheck_status.should_clear_old_diagnostics() {
self.diagnostics.clear_check(id); self.diagnostics.clear_check(id);
} }
*flycheck_status = FlycheckStatus::Finished;
(Progress::End, None) (Progress::End, None)
} }
}; };