Better progress API

Percentage is a UI concern, the physical fact here is fraction. It's
sad that percentage bleeds into the protocol level, we even duplicated
this bad API ourselves!
This commit is contained in:
Aleksey Kladov 2020-10-07 12:15:37 +02:00
parent 67c76c35a3
commit 6219142c96
2 changed files with 9 additions and 4 deletions

View File

@ -25,8 +25,9 @@ pub(crate) enum Progress {
}
impl Progress {
pub(crate) fn percentage(done: usize, total: usize) -> f64 {
(done as f64 / total.max(1) as f64) * 100.0
pub(crate) fn fraction(done: usize, total: usize) -> f64 {
assert!(done <= total);
done as f64 / total.max(1) as f64
}
}
@ -43,11 +44,15 @@ pub(crate) fn report_progress(
title: &str,
state: Progress,
message: Option<String>,
percentage: Option<f64>,
fraction: Option<f64>,
) {
if !self.config.client_caps.work_done_progress {
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 work_done_progress = match state {
Progress::Begin => {

View File

@ -230,7 +230,7 @@ fn handle_event(&mut self, event: Event) -> Result<()> {
"roots scanned",
state,
Some(format!("{}/{}", n_done, n_total)),
Some(Progress::percentage(n_done, n_total)),
Some(Progress::fraction(n_done, n_total)),
)
}
}