aboutsummaryrefslogtreecommitdiff
path: root/crates/rust-analyzer/src/main_loop.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-06-26 15:33:57 +0100
committerAleksey Kladov <[email protected]>2020-06-26 15:33:57 +0100
commit1893289e5c7cebeeb9705c031c996fc29d8c5b54 (patch)
tree19ff08cc4306fb4b0fbcb057927de3973d30eb33 /crates/rust-analyzer/src/main_loop.rs
parentbd903bf132dfc188e2f4c634a8b457ab4d7d4852 (diff)
Move progress reporting to utils
Diffstat (limited to 'crates/rust-analyzer/src/main_loop.rs')
-rw-r--r--crates/rust-analyzer/src/main_loop.rs68
1 files changed, 5 insertions, 63 deletions
diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs
index ed3d27750..ae3c7e30e 100644
--- a/crates/rust-analyzer/src/main_loop.rs
+++ b/crates/rust-analyzer/src/main_loop.rs
@@ -18,7 +18,7 @@ use crate::{
18 from_proto, 18 from_proto,
19 global_state::{file_id_to_url, url_to_file_id, GlobalState, Status}, 19 global_state::{file_id_to_url, url_to_file_id, GlobalState, Status},
20 handlers, lsp_ext, 20 handlers, lsp_ext,
21 lsp_utils::{apply_document_changes, is_canceled, notification_is, notification_new}, 21 lsp_utils::{apply_document_changes, is_canceled, notification_is, notification_new, Progress},
22 Result, 22 Result,
23}; 23};
24 24
@@ -181,12 +181,11 @@ impl GlobalState {
181 became_ready = true; 181 became_ready = true;
182 Progress::End 182 Progress::End
183 }; 183 };
184 report_progress( 184 self.report_progress(
185 self,
186 "roots scanned", 185 "roots scanned",
187 state, 186 state,
188 Some(format!("{}/{}", n_done, n_total)), 187 Some(format!("{}/{}", n_done, n_total)),
189 Some(percentage(n_done, n_total)), 188 Some(Progress::percentage(n_done, n_total)),
190 ) 189 )
191 } 190 }
192 }, 191 },
@@ -216,7 +215,7 @@ impl GlobalState {
216 flycheck::Progress::DidStart => { 215 flycheck::Progress::DidStart => {
217 self.diagnostics.clear_check(); 216 self.diagnostics.clear_check();
218 (Progress::Begin, None) 217 (Progress::Begin, None)
219 }, 218 }
220 flycheck::Progress::DidCheckCrate(target) => { 219 flycheck::Progress::DidCheckCrate(target) => {
221 (Progress::Report, Some(target)) 220 (Progress::Report, Some(target))
222 } 221 }
@@ -225,7 +224,7 @@ impl GlobalState {
225 } 224 }
226 }; 225 };
227 226
228 report_progress(self, "cargo check", state, message, None); 227 self.report_progress("cargo check", state, message, None);
229 } 228 }
230 }, 229 },
231 } 230 }
@@ -468,60 +467,3 @@ impl GlobalState {
468 }); 467 });
469 } 468 }
470} 469}
471
472#[derive(Debug, Eq, PartialEq)]
473enum Progress {
474 Begin,
475 Report,
476 End,
477}
478
479fn percentage(done: usize, total: usize) -> f64 {
480 (done as f64 / total.max(1) as f64) * 100.0
481}
482
483fn report_progress(
484 global_state: &mut GlobalState,
485 title: &str,
486 state: Progress,
487 message: Option<String>,
488 percentage: Option<f64>,
489) {
490 if !global_state.config.client_caps.work_done_progress {
491 return;
492 }
493 let token = lsp_types::ProgressToken::String(format!("rustAnalyzer/{}", title));
494 let work_done_progress = match state {
495 Progress::Begin => {
496 let work_done_progress_create = global_state.req_queue.outgoing.register(
497 lsp_types::request::WorkDoneProgressCreate::METHOD.to_string(),
498 lsp_types::WorkDoneProgressCreateParams { token: token.clone() },
499 |_, _| (),
500 );
501 global_state.send(work_done_progress_create.into());
502
503 lsp_types::WorkDoneProgress::Begin(lsp_types::WorkDoneProgressBegin {
504 title: title.into(),
505 cancellable: None,
506 message,
507 percentage,
508 })
509 }
510 Progress::Report => {
511 lsp_types::WorkDoneProgress::Report(lsp_types::WorkDoneProgressReport {
512 cancellable: None,
513 message,
514 percentage,
515 })
516 }
517 Progress::End => {
518 lsp_types::WorkDoneProgress::End(lsp_types::WorkDoneProgressEnd { message })
519 }
520 };
521 let notification =
522 notification_new::<lsp_types::notification::Progress>(lsp_types::ProgressParams {
523 token,
524 value: lsp_types::ProgressParamsValue::WorkDone(work_done_progress),
525 });
526 global_state.send(notification.into());
527}