]> git.lizzy.rs Git - rust.git/commitdiff
Make flycheck workdone progress reports cancellable
authorLukas Wirth <lukastw97@gmail.com>
Mon, 17 Oct 2022 14:32:47 +0000 (16:32 +0200)
committerLukas Wirth <lukastw97@gmail.com>
Mon, 17 Oct 2022 14:32:47 +0000 (16:32 +0200)
crates/rust-analyzer/src/lsp_utils.rs
crates/rust-analyzer/src/main_loop.rs

index 5a37cbe2e334baa95346e4d522b600f2597a7545..b3cea64d4175e129672af57cf90ba647db346c21 100644 (file)
@@ -87,6 +87,7 @@ pub(crate) fn report_progress(
         state: Progress,
         message: Option<String>,
         fraction: Option<f64>,
+        cancel_token: Option<String>,
     ) {
         if !self.config.work_done_progress() {
             return;
@@ -95,7 +96,10 @@ pub(crate) fn report_progress(
             assert!((0.0..=1.0).contains(&f));
             (f * 100.0) as u32
         });
-        let token = lsp_types::ProgressToken::String(format!("rustAnalyzer/{}", title));
+        let cancellable = Some(cancel_token.is_some());
+        let token = lsp_types::ProgressToken::String(
+            cancel_token.unwrap_or_else(|| format!("rustAnalyzer/{}", title)),
+        );
         let work_done_progress = match state {
             Progress::Begin => {
                 self.send_request::<lsp_types::request::WorkDoneProgressCreate>(
@@ -105,14 +109,14 @@ pub(crate) fn report_progress(
 
                 lsp_types::WorkDoneProgress::Begin(lsp_types::WorkDoneProgressBegin {
                     title: title.into(),
-                    cancellable: None,
+                    cancellable,
                     message,
                     percentage,
                 })
             }
             Progress::Report => {
                 lsp_types::WorkDoneProgress::Report(lsp_types::WorkDoneProgressReport {
-                    cancellable: None,
+                    cancellable,
                     message,
                     percentage,
                 })
index 15922dac651cce834818de41cc479e50248632d6..3a379993ad650308c2056ddc37b410ef36f23c2a 100644 (file)
@@ -257,7 +257,7 @@ fn handle_event(&mut self, event: Event) -> Result<()> {
                         }
                     };
 
-                    self.report_progress("Indexing", state, message, Some(fraction));
+                    self.report_progress("Indexing", state, message, Some(fraction), None);
                 }
             }
             Event::Vfs(message) => {
@@ -465,7 +465,7 @@ fn handle_task(&mut self, prime_caches_progress: &mut Vec<PrimeCachesProgress>,
                     }
                 };
 
-                self.report_progress("Fetching", state, msg, None);
+                self.report_progress("Fetching", state, msg, None, None);
             }
             Task::FetchBuildData(progress) => {
                 let (state, msg) = match progress {
@@ -481,7 +481,7 @@ fn handle_task(&mut self, prime_caches_progress: &mut Vec<PrimeCachesProgress>,
                 };
 
                 if let Some(state) = state {
-                    self.report_progress("Loading", state, msg, None);
+                    self.report_progress("Loading", state, msg, None, None);
                 }
             }
         }
@@ -518,6 +518,7 @@ fn handle_vfs_msg(&mut self, message: vfs::loader::Message) {
                     state,
                     Some(format!("{}/{}", n_done, n_total)),
                     Some(Progress::fraction(n_done, n_total)),
+                    None,
                 )
             }
         }
@@ -584,7 +585,13 @@ fn handle_flycheck_msg(&mut self, message: flycheck::Message) {
                 } else {
                     format!("cargo check (#{})", id + 1)
                 };
-                self.report_progress(&title, state, message, None);
+                self.report_progress(
+                    &title,
+                    state,
+                    message,
+                    None,
+                    Some(format!("rust-analyzer/checkOnSave/{}", id)),
+                );
             }
         }
     }
@@ -698,7 +705,16 @@ fn on_notification(&mut self, not: Notification) -> Result<()> {
                 this.cancel(id);
                 Ok(())
             })?
-            .on::<lsp_types::notification::WorkDoneProgressCancel>(|_this, _params| {
+            .on::<lsp_types::notification::WorkDoneProgressCancel>(|this, params| {
+                if let lsp_types::NumberOrString::String(s) = &params.token {
+                    if let Some(id) = s.strip_prefix("rust-analyzer/checkOnSave/") {
+                        if let Ok(id) = u32::from_str_radix(id, 10) {
+                            if let Some(flycheck) = this.flycheck.get(id as usize) {
+                                flycheck.cancel();
+                            }
+                        }
+                    }
+                }
                 // Just ignore this. It is OK to continue sending progress
                 // notifications for this token, as the client can't know when
                 // we accepted notification.