diff --git a/crates/ide-index/src/tests.rs b/crates/ide-index/src/tests.rs index 3116b9d74f..8c579cbd87 100644 --- a/crates/ide-index/src/tests.rs +++ b/crates/ide-index/src/tests.rs @@ -1246,3 +1246,38 @@ fn queries_during_streaming_scan_do_not_deadlock() { done_rx.recv_timeout(Duration::from_secs(60)).expect("concurrent index queries deadlocked with streaming scan"); } + +#[test] +fn reconciling_a_few_changed_files_does_not_show_indexing() { + let fx = sample(); + let events = Arc::new(Mutex::new(Vec::new())); + let sink: SharedSink = { + let events = events.clone(); + Arc::new(move |e: IdeEvent| events.lock().push(e)) + }; + let idx = fx.open_with(sink); + let announced = |events: &Mutex>| { + events + .lock() + .iter() + .filter(|e| matches!(e, IdeEvent::IndexState { indexing: true, .. } | IdeEvent::Task { done: false, .. })) + .count() + }; + let settle = || { + assert!(idx.wait_idle(WAIT)); + // The touched folders are reconciled once the snapshot delay has passed. + std::thread::sleep(Duration::from_millis(300)); + assert!(idx.wait_idle(WAIT)); + }; + + // A program appending to its log in the project, as a debugged Spring app does. + events.lock().clear(); + let log = fx.write("logs/app.log", "start\n"); + for i in 0..3 { + std::fs::write(&log, format!("line {i}\n").repeat(i + 1)).unwrap(); + idx.update(std::slice::from_ref(&log), &[]); + settle(); + } + assert_eq!(announced(&events), 0, "a log being written must not show Indexing"); + assert!(files(&idx, "app.log").contains(&"logs/app.log".to_owned()), "the log is still indexed"); +} diff --git a/crates/ide-index/src/worker.rs b/crates/ide-index/src/worker.rs index 0219b814b9..3851d4f576 100644 --- a/crates/ide-index/src/worker.rs +++ b/crates/ide-index/src/worker.rs @@ -36,6 +36,8 @@ const PROGRESS_EVERY: Duration = Duration::from_millis(250); const SNAPSHOT_DELAY: Duration = Duration::from_secs(2); #[cfg(test)] const SNAPSHOT_DELAY: Duration = Duration::from_millis(50); +/// A subtree scan with fewer files to parse runs without showing "Indexing" (see `scan`). +const QUIET_SCAN_FILES: usize = 200; pub(crate) const MAX_RECENT: usize = 50; pub(crate) enum Job { @@ -444,7 +446,8 @@ impl Shared { } /// A cancelled scan (project closed) still ends its progress task and the indexing state. - fn end_scan(&self, task: TaskId) { + fn end_scan(&self, task: Option) { + let Some(task) = task else { return }; self.indexing.store(false, Ordering::SeqCst); self.sink.emit(IdeEvent::Task { id: task, @@ -476,8 +479,8 @@ impl Shared { ide_core::memory::release_free_pages(); } - fn scan(&self, dirs: Option<&[PathBuf]>, prewalked: Option>, guard: SystemTime) -> Option { - let started = Instant::now(); + /// Shows "Indexing" (the status flag and a background task) for a scan. + fn start_scan(&self) -> TaskId { self.indexing.store(true, Ordering::SeqCst); self.emit_state(); let task = TaskId::next(); @@ -489,6 +492,16 @@ impl Shared { done: false, cancellable: false, }); + task + } + + /// A full scan shows "Indexing" from the start. A subtree scan (folders the watcher reported, + /// reconciled after each burst of changes) shows it only once it has + /// [`QUIET_SCAN_FILES`] or more files to parse: a program writing its log inside the project + /// would otherwise keep "Indexing" in the status bar for as long as it runs. + fn scan(&self, dirs: Option<&[PathBuf]>, prewalked: Option>, guard: SystemTime) -> Option { + let started = Instant::now(); + let mut task = dirs.is_none().then(|| self.start_scan()); let filter = self.filter(); let walk_started = Instant::now(); @@ -524,6 +537,9 @@ impl Shared { self.persist(&[], &stale, false); let total = todo.len(); + if task.is_none() && total >= QUIET_SCAN_FILES { + task = Some(self.start_scan()); + } let update_started = Instant::now(); let mut laps = ChunkLaps::default(); { @@ -564,7 +580,9 @@ impl Shared { } done += batch.len(); laps.add(self.publish(batch.drain(..))); - if last_progress.elapsed() >= PROGRESS_EVERY { + if let Some(task) = task + && last_progress.elapsed() >= PROGRESS_EVERY + { last_progress = Instant::now(); self.sink.emit(IdeEvent::Task { id: task, @@ -606,15 +624,18 @@ impl Shared { } let snapshot_elapsed = snapshot_started.elapsed(); *self.snapshot_due.lock() = None; - self.indexing.store(false, Ordering::SeqCst); - self.sink.emit(IdeEvent::Task { - id: task, - title: "Indexing".into(), - message: None, - fraction: Some(1.0), - done: true, - cancellable: false, - }); + if let Some(task) = task { + self.indexing.store(false, Ordering::SeqCst); + self.sink.emit(IdeEvent::Task { + id: task, + title: "Indexing".into(), + message: None, + fraction: Some(1.0), + done: true, + cancellable: false, + }); + } + // The counts may have changed either way. self.emit_state(); if self.is_cancelled() { return None;