Move progress reporting to utils

This commit is contained in:
Aleksey Kladov
2020-06-26 16:33:57 +02:00
parent bd903bf132
commit 1893289e5c
5 changed files with 91 additions and 92 deletions

View File

@@ -18,7 +18,7 @@ use crate::{
from_proto,
global_state::{file_id_to_url, url_to_file_id, GlobalState, Status},
handlers, lsp_ext,
lsp_utils::{apply_document_changes, is_canceled, notification_is, notification_new},
lsp_utils::{apply_document_changes, is_canceled, notification_is, notification_new, Progress},
Result,
};
@@ -181,12 +181,11 @@ impl GlobalState {
became_ready = true;
Progress::End
};
report_progress(
self,
self.report_progress(
"roots scanned",
state,
Some(format!("{}/{}", n_done, n_total)),
Some(percentage(n_done, n_total)),
Some(Progress::percentage(n_done, n_total)),
)
}
},
@@ -216,7 +215,7 @@ impl GlobalState {
flycheck::Progress::DidStart => {
self.diagnostics.clear_check();
(Progress::Begin, None)
},
}
flycheck::Progress::DidCheckCrate(target) => {
(Progress::Report, Some(target))
}
@@ -225,7 +224,7 @@ impl GlobalState {
}
};
report_progress(self, "cargo check", state, message, None);
self.report_progress("cargo check", state, message, None);
}
},
}
@@ -468,60 +467,3 @@ impl GlobalState {
});
}
}
#[derive(Debug, Eq, PartialEq)]
enum Progress {
Begin,
Report,
End,
}
fn percentage(done: usize, total: usize) -> f64 {
(done as f64 / total.max(1) as f64) * 100.0
}
fn report_progress(
global_state: &mut GlobalState,
title: &str,
state: Progress,
message: Option<String>,
percentage: Option<f64>,
) {
if !global_state.config.client_caps.work_done_progress {
return;
}
let token = lsp_types::ProgressToken::String(format!("rustAnalyzer/{}", title));
let work_done_progress = match state {
Progress::Begin => {
let work_done_progress_create = global_state.req_queue.outgoing.register(
lsp_types::request::WorkDoneProgressCreate::METHOD.to_string(),
lsp_types::WorkDoneProgressCreateParams { token: token.clone() },
|_, _| (),
);
global_state.send(work_done_progress_create.into());
lsp_types::WorkDoneProgress::Begin(lsp_types::WorkDoneProgressBegin {
title: title.into(),
cancellable: None,
message,
percentage,
})
}
Progress::Report => {
lsp_types::WorkDoneProgress::Report(lsp_types::WorkDoneProgressReport {
cancellable: None,
message,
percentage,
})
}
Progress::End => {
lsp_types::WorkDoneProgress::End(lsp_types::WorkDoneProgressEnd { message })
}
};
let notification =
notification_new::<lsp_types::notification::Progress>(lsp_types::ProgressParams {
token,
value: lsp_types::ProgressParamsValue::WorkDone(work_done_progress),
});
global_state.send(notification.into());
}