From dc0076de12a1e77515c99b08ee74d0af37b250f8 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sun, 29 Mar 2020 06:33:16 +0800 Subject: Move roots_to_scan to LoopState --- crates/rust-analyzer/src/main_loop.rs | 35 +++++++++++++++++++---------------- crates/rust-analyzer/src/world.rs | 9 +++------ 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index 7825b0077..a96628235 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -208,6 +208,9 @@ pub fn main_loop( ) }; + loop_state.roots_to_scan = world_state.vfs.read().n_roots(); + loop_state.roots_total = loop_state.roots_to_scan; + let pool = ThreadPool::default(); let (task_sender, task_receiver) = unbounded::(); let (libdata_sender, libdata_receiver) = unbounded::(); @@ -333,7 +336,10 @@ struct LoopState { in_flight_libraries: usize, pending_libraries: Vec<(SourceRootId, Vec<(FileId, RelativePathBuf, Arc)>)>, workspace_loaded: bool, + roots_scanned_progress: Option, + roots_to_scan: usize, + roots_total: usize, } impl LoopState { @@ -377,6 +383,7 @@ fn loop_turn( world_state.add_lib(lib); world_state.maybe_collect_garbage(); loop_state.in_flight_libraries -= 1; + loop_state.roots_to_scan -= 1; } Event::CheckWatcher(task) => on_check_task(task, world_state, task_sender)?, Event::Msg(msg) => match msg { @@ -408,7 +415,7 @@ fn loop_turn( }; let mut state_changed = false; - if let Some(changes) = world_state.process_changes() { + if let Some(changes) = world_state.process_changes(&mut loop_state.roots_to_scan) { state_changed = true; loop_state.pending_libraries.extend(changes); } @@ -427,8 +434,11 @@ fn loop_turn( }); } + let show_progress = !loop_state.workspace_loaded + && world_state.feature_flags.get("notifications.workspace-loaded"); + if !loop_state.workspace_loaded - && world_state.roots_to_scan == 0 + && loop_state.roots_to_scan == 0 && loop_state.pending_libraries.is_empty() && loop_state.in_flight_libraries == 0 { @@ -439,9 +449,10 @@ fn loop_turn( let snap = world_state.snapshot(); move || snap.analysis().prime_caches(subs).unwrap_or_else(|_: Canceled| ()) }); - send_startup_progress(&connection.sender, loop_state, world_state); - } else if !loop_state.workspace_loaded { - send_startup_progress(&connection.sender, loop_state, world_state); + } + + if show_progress { + send_startup_progress(&connection.sender, loop_state); } if state_changed { @@ -706,18 +717,10 @@ fn on_diagnostic_task(task: DiagnosticTask, msg_sender: &Sender, state: } } -fn send_startup_progress( - sender: &Sender, - loop_state: &mut LoopState, - world_state: &WorldState, -) { - if !world_state.feature_flags.get("notifications.workspace-loaded") { - return; - } - - let total: usize = world_state.workspaces.iter().map(|it| it.n_packages()).sum(); +fn send_startup_progress(sender: &Sender, loop_state: &mut LoopState) { + let total: usize = loop_state.roots_total; let prev_progress = loop_state.roots_scanned_progress; - let progress = total - world_state.roots_to_scan; + let progress = total - loop_state.roots_to_scan; loop_state.roots_scanned_progress = Some(progress); match (prev_progress, loop_state.workspace_loaded) { diff --git a/crates/rust-analyzer/src/world.rs b/crates/rust-analyzer/src/world.rs index 64a7b907e..01008b09f 100644 --- a/crates/rust-analyzer/src/world.rs +++ b/crates/rust-analyzer/src/world.rs @@ -51,8 +51,6 @@ pub struct Options { pub struct WorldState { pub options: Options, pub feature_flags: Arc, - //FIXME: this belongs to `LoopState` rather than to `WorldState` - pub roots_to_scan: usize, pub roots: Vec, pub workspaces: Arc>, pub analysis_host: AnalysisHost, @@ -123,7 +121,7 @@ impl WorldState { let (task_sender, task_receiver) = unbounded(); let task_sender = Box::new(move |t| task_sender.send(t).unwrap()); let (mut vfs, vfs_roots) = Vfs::new(roots, task_sender, watch); - let roots_to_scan = vfs_roots.len(); + for r in vfs_roots { let vfs_root_path = vfs.root2path(r); let is_local = folder_roots.iter().any(|it| vfs_root_path.starts_with(it)); @@ -190,7 +188,6 @@ impl WorldState { WorldState { options, feature_flags: Arc::new(feature_flags), - roots_to_scan, roots: folder_roots, workspaces: Arc::new(workspaces), analysis_host, @@ -206,6 +203,7 @@ impl WorldState { /// FIXME: better API here pub fn process_changes( &mut self, + roots_to_scan: &mut usize, ) -> Option)>)>> { let changes = self.vfs.write().commit_changes(); if changes.is_empty() { @@ -219,7 +217,7 @@ impl WorldState { let root_path = self.vfs.read().root2path(root); let is_local = self.roots.iter().any(|r| root_path.starts_with(r)); if is_local { - self.roots_to_scan -= 1; + *roots_to_scan -= 1; for (file, path, text) in files { change.add_file(SourceRootId(root.0), FileId(file.0), path, text); } @@ -247,7 +245,6 @@ impl WorldState { } pub fn add_lib(&mut self, data: LibraryData) { - self.roots_to_scan -= 1; let mut change = AnalysisChange::new(); change.add_library(data); self.analysis_host.apply_change(change); -- cgit v1.2.3