Move roots_to_scan to LoopState

This commit is contained in:
Edwin Cheng 2020-03-29 06:33:16 +08:00
parent 1c2d4135db
commit dc0076de12
2 changed files with 22 additions and 22 deletions

View file

@ -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::<Task>();
let (libdata_sender, libdata_receiver) = unbounded::<LibraryData>();
@ -333,7 +336,10 @@ struct LoopState {
in_flight_libraries: usize,
pending_libraries: Vec<(SourceRootId, Vec<(FileId, RelativePathBuf, Arc<String>)>)>,
workspace_loaded: bool,
roots_scanned_progress: Option<usize>,
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<Message>, state:
}
}
fn send_startup_progress(
sender: &Sender<Message>,
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<Message>, 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) {

View file

@ -51,8 +51,6 @@ pub struct Options {
pub struct WorldState {
pub options: Options,
pub feature_flags: Arc<FeatureFlags>,
//FIXME: this belongs to `LoopState` rather than to `WorldState`
pub roots_to_scan: usize,
pub roots: Vec<PathBuf>,
pub workspaces: Arc<Vec<ProjectWorkspace>>,
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<Vec<(SourceRootId, Vec<(FileId, RelativePathBuf, Arc<String>)>)>> {
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);