6166: Better progress API r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-10-07 10:17:14 +00:00 committed by GitHub
commit 2aa46034c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 4 deletions

View file

@ -25,8 +25,9 @@ pub(crate) enum Progress {
} }
impl Progress { impl Progress {
pub(crate) fn percentage(done: usize, total: usize) -> f64 { pub(crate) fn fraction(done: usize, total: usize) -> f64 {
(done as f64 / total.max(1) as f64) * 100.0 assert!(done <= total);
done as f64 / total.max(1) as f64
} }
} }
@ -43,11 +44,15 @@ impl GlobalState {
title: &str, title: &str,
state: Progress, state: Progress,
message: Option<String>, message: Option<String>,
percentage: Option<f64>, fraction: Option<f64>,
) { ) {
if !self.config.client_caps.work_done_progress { if !self.config.client_caps.work_done_progress {
return; return;
} }
let percentage = fraction.map(|f| {
assert!(0.0 <= f && f <= 1.0);
f * 100.0
});
let token = lsp_types::ProgressToken::String(format!("rustAnalyzer/{}", title)); let token = lsp_types::ProgressToken::String(format!("rustAnalyzer/{}", title));
let work_done_progress = match state { let work_done_progress = match state {
Progress::Begin => { Progress::Begin => {

View file

@ -230,7 +230,7 @@ impl GlobalState {
"roots scanned", "roots scanned",
state, state,
Some(format!("{}/{}", n_done, n_total)), Some(format!("{}/{}", n_done, n_total)),
Some(Progress::percentage(n_done, n_total)), Some(Progress::fraction(n_done, n_total)),
) )
} }
} }