From 1893289e5c7cebeeb9705c031c996fc29d8c5b54 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 26 Jun 2020 16:33:57 +0200 Subject: [PATCH] Move progress reporting to utils --- crates/rust-analyzer/src/global_state.rs | 4 -- crates/rust-analyzer/src/lib.rs | 2 +- crates/rust-analyzer/src/lsp_utils.rs | 85 ++++++++++++++++++++---- crates/rust-analyzer/src/main_loop.rs | 68 ++----------------- crates/rust-analyzer/src/reload.rs | 24 ++++--- 5 files changed, 91 insertions(+), 92 deletions(-) diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs index 4da09408317..41659f99d2d 100644 --- a/crates/rust-analyzer/src/global_state.rs +++ b/crates/rust-analyzer/src/global_state.rs @@ -21,7 +21,6 @@ use crate::{ main_loop::Task, reload::SourceRootConfig, request_metrics::{LatestRequests, RequestMetrics}, - show_message, thread_pool::TaskPool, to_proto::url_from_abs_path, Result, @@ -182,9 +181,6 @@ impl GlobalState { self.send(response.into()); } } - pub(crate) fn show_message(&self, typ: lsp_types::MessageType, message: String) { - show_message(typ, message, &self.sender) - } } impl Drop for GlobalState { diff --git a/crates/rust-analyzer/src/lib.rs b/crates/rust-analyzer/src/lib.rs index a24dfe58c63..407944d852c 100644 --- a/crates/rust-analyzer/src/lib.rs +++ b/crates/rust-analyzer/src/lib.rs @@ -39,7 +39,7 @@ pub mod config; use serde::de::DeserializeOwned; pub type Result> = std::result::Result; -pub use crate::{caps::server_capabilities, lsp_utils::show_message, main_loop::main_loop}; +pub use crate::{caps::server_capabilities, main_loop::main_loop}; use std::fmt; pub fn from_json(what: &'static str, json: serde_json::Value) -> Result { diff --git a/crates/rust-analyzer/src/lsp_utils.rs b/crates/rust-analyzer/src/lsp_utils.rs index 35917030c8d..fd793a17cfa 100644 --- a/crates/rust-analyzer/src/lsp_utils.rs +++ b/crates/rust-analyzer/src/lsp_utils.rs @@ -1,24 +1,13 @@ //! Utilities for LSP-related boilerplate code. use std::{error::Error, ops::Range}; -use crossbeam_channel::Sender; -use lsp_server::{Message, Notification}; +use lsp_server::Notification; +use lsp_types::request::Request; use ra_db::Canceled; use ra_ide::LineIndex; use serde::Serialize; -use crate::from_proto; - -pub fn show_message( - typ: lsp_types::MessageType, - message: impl Into, - sender: &Sender, -) { - let message = message.into(); - let params = lsp_types::ShowMessageParams { typ, message }; - let not = notification_new::(params); - sender.send(not.into()).unwrap(); -} +use crate::{from_proto, global_state::GlobalState}; pub(crate) fn is_canceled(e: &(dyn Error + 'static)) -> bool { e.downcast_ref::().is_some() @@ -38,6 +27,74 @@ where Notification::new(N::METHOD.to_string(), params) } +#[derive(Debug, Eq, PartialEq)] +pub(crate) enum Progress { + Begin, + Report, + End, +} + +impl Progress { + pub(crate) fn percentage(done: usize, total: usize) -> f64 { + (done as f64 / total.max(1) as f64) * 100.0 + } +} + +impl GlobalState { + pub(crate) fn show_message(&mut self, typ: lsp_types::MessageType, message: String) { + let message = message.into(); + let params = lsp_types::ShowMessageParams { typ, message }; + let not = notification_new::(params); + self.send(not.into()); + } + + pub(crate) fn report_progress( + &mut self, + title: &str, + state: Progress, + message: Option, + percentage: Option, + ) { + if !self.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 = self.req_queue.outgoing.register( + lsp_types::request::WorkDoneProgressCreate::METHOD.to_string(), + lsp_types::WorkDoneProgressCreateParams { token: token.clone() }, + |_, _| (), + ); + self.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::ProgressParams { + token, + value: lsp_types::ProgressParamsValue::WorkDone(work_done_progress), + }); + self.send(notification.into()); + } +} + pub(crate) fn apply_document_changes( old_text: &mut String, content_changes: Vec, diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index ed3d2775012..ae3c7e30eb0 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -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, - percentage: Option, -) { - 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::ProgressParams { - token, - value: lsp_types::ProgressParamsValue::WorkDone(work_done_progress), - }); - global_state.send(notification.into()); -} diff --git a/crates/rust-analyzer/src/reload.rs b/crates/rust-analyzer/src/reload.rs index a22d3e26234..fece6176e72 100644 --- a/crates/rust-analyzer/src/reload.rs +++ b/crates/rust-analyzer/src/reload.rs @@ -36,27 +36,31 @@ impl GlobalState { self.config .linked_projects .iter() - .filter_map(|project| match project { + .map(|project| match project { LinkedProject::ProjectManifest(manifest) => { ra_project_model::ProjectWorkspace::load( manifest.clone(), &self.config.cargo, self.config.with_sysroot, ) - .map_err(|err| { - log::error!("failed to load workspace: {:#}", err); - self.show_message( - lsp_types::MessageType::Error, - format!("rust-analyzer failed to load workspace: {:#}", err), - ); - }) - .ok() } LinkedProject::InlineJsonProject(it) => { - Some(ra_project_model::ProjectWorkspace::Json { project: it.clone() }) + Ok(ra_project_model::ProjectWorkspace::Json { project: it.clone() }) } }) .collect::>() + .into_iter() + .filter_map(|res| { + res.map_err(|err| { + log::error!("failed to load workspace: {:#}", err); + self.show_message( + lsp_types::MessageType::Error, + format!("rust-analyzer failed to load workspace: {:#}", err), + ); + }) + .ok() + }) + .collect::>() }; if let FilesWatcher::Client = self.config.files.watcher {