Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

tui: instant @-file completion (eager index, inline answers, same-frame menu) (#3725)

Sebastian Müller623e8ff06293
diff --git a/crates/pa-tui/src/agents_view/render.rs b/crates/pa-tui/src/agents_view/render.rs
index aadc041983..de1d58184e 100644
--- a/crates/pa-tui/src/agents_view/render.rs
+++ b/crates/pa-tui/src/agents_view/render.rs
@@ -969,6 +969,7 @@ impl Renderer {
crate::input::ReaderInput::BurstPaste(text) => {
ui_tx.send(UiInput::Paste(text)).is_ok()
}
+ crate::input::ReaderInput::ChunkEnd => true,
// A guard-reassembled report: consumed unless tracking is active,
// the same contract as the terminal's own mouse events below.
crate::input::ReaderInput::Mouse(report) => {
diff --git a/crates/pa-tui/src/autocomplete.rs b/crates/pa-tui/src/autocomplete.rs
index 4dcd4db1d1..a2c1bd70da 100644
--- a/crates/pa-tui/src/autocomplete.rs
+++ b/crates/pa-tui/src/autocomplete.rs
@@ -779,6 +779,12 @@ impl CombinedAutocompleteProvider {
}
}
+ /// Start building the `@` file index in the background, so the first `@` query can
+ /// already answer from memory.
+ pub fn start_file_index(&self) {
+ self.file_index.start_build(&self.paths.base);
+ }
+
/// Replace the hidden-command set (the caller recomputes model
/// eligibility on every model switch).
pub fn set_hidden_commands(&mut self, hidden: std::collections::HashSet<String>) {
@@ -898,11 +904,7 @@ impl AutocompleteProvider for CombinedAutocompleteProvider {
let line: Vec<char> = lines.get(cursor_line)?.chars().collect();
let before: Vec<char> = line[..cursor_col.min(line.len())].to_vec();
if let Some(at_prefix) = extract_at_prefix(&before) {
- return Some(SuggestionLookup::Searching(fuzzy_file_search::spawn(
- &self.paths.base,
- self.file_index.clone(),
- at_prefix,
- )));
+ return fuzzy_file_search::lookup(&self.paths.base, &self.file_index, at_prefix);
}
if !force {
if let Some(context) = get_slash_command_context(lines, cursor_line, cursor_col) {
diff --git a/crates/pa-tui/src/autocomplete/file_index.rs b/crates/pa-tui/src/autocomplete/file_index.rs
index d4ea4ac49b..4bf133a020 100644
--- a/crates/pa-tui/src/autocomplete/file_index.rs
+++ b/crates/pa-tui/src/autocomplete/file_index.rs
@@ -1,15 +1,16 @@
//! The session file index: an in-memory mirror of the `@` completion's walk results
//! under the session base — fd printed-form paths, relative to the base, directories
-//! keeping their trailing `/` — so every query after the first answers from memory
-//! instead of walking the tree.
+//! keeping their trailing `/` — so a query answers from memory instead of walking the
+//! tree.
//!
-//! The first `@` query builds it with one full walk (fd's rules, no result cut), a
-//! watcher started before that walk buffers the changes it races with, and one applier
-//! thread keeps the entries current afterwards: batches applied by each path's current
-//! existence (event kinds differ per editor and platform), the shared walker deciding
-//! what may enter. Queries the index cannot serve — an absolute or `..` scope, a typed
-//! directory the walk rules exclude, a watcher that failed, or a tree over the entry
-//! cap — fall back to the per-query walk, which stays.
+//! A background thread builds it with one full walk (fd's rules, no result cut) —
+//! started with the session view, else by the first `@` query — and queries walk until
+//! it is ready. A watcher started before that walk buffers the changes it races with,
+//! and one applier thread keeps the entries current afterwards: batches applied by each
+//! path's current existence (event kinds differ per editor and platform), the shared
+//! walker deciding what may enter. Queries the index cannot serve — an absolute or `..`
+//! scope, a typed directory the walk rules exclude, a watcher that failed, or a tree
+//! over the entry cap — fall back to the per-query walk, which stays.
//!
//! Limitations: changes inside symlinked directories whose targets sit outside the
//! base are not observed (the build-time view stands until a rebuild); `.git/info/exclude`
@@ -62,9 +63,8 @@ pub(super) enum Answer {
Walk,
}
-/// The session's file index, one per autocomplete provider: lazily built on the first
-/// `@` query, then shared by every query thread. Cheap to clone; the last clone drops
-/// the index and its watcher.
+/// The session's file index, one per autocomplete provider. Cheap to clone; the last clone
+/// drops the index and its watcher.
#[derive(Clone)]
pub(super) struct FileIndex {
shared: Arc<Shared>,
@@ -76,25 +76,25 @@ struct Shared {
ready: Condvar,
}
-/// The index lifecycle. `Unbuilt` builds on the next query; `Building` makes later
-/// queries wait instead of starting their own walks; `Disabled` walks per query for
-/// the rest of the provider's life.
+/// The index lifecycle. `Unbuilt` starts a background build on the next query; queries
+/// walk while it is `Building`, and for the rest of the provider's life once `Disabled`.
+/// Only the applier changes a `Ready` state.
enum IndexState {
Unbuilt,
Building,
Ready {
- /// Sorted, no duplicates: a scope's entries are one contiguous slice.
- entries: Vec<Box<str>>,
+ /// Sorted, no duplicates: a scope's entries are one contiguous slice. Shared so a
+ /// query scans its snapshot with no lock held.
+ entries: Arc<Vec<Box<str>>>,
_watcher: RecommendedWatcher,
},
Disabled,
}
-enum Step {
- Build,
- Wait,
- Serve,
- Walk,
+impl Shared {
+ fn lock(&self) -> std::sync::MutexGuard<'_, IndexState> {
+ self.state.lock().unwrap_or_else(PoisonError::into_inner)
+ }
}
impl FileIndex {
@@ -107,61 +107,40 @@ impl FileIndex {
}
}
- fn lock(&self) -> std::sync::MutexGuard<'_, IndexState> {
- self.shared
- .state
- .lock()
- .unwrap_or_else(PoisonError::into_inner)
- }
-
- /// Answer one query from the index: build on first use, wait out a concurrent
- /// build, then rank the matched entries (under the lock — the candidates borrow
- /// the entries). `Answer::Served` is the index's own result (`None` inside it an
- /// empty menu); `Answer::Walk` means the index does not serve the query — it is
- /// disabled, or the scope sits outside it.
+ /// Answer one query from the ready index; never waits on a build or a batch.
+ /// `Answer::Served` is the index's own result (`None` inside it an empty menu);
+ /// `Answer::Walk` means the index does not serve the query — it is not built yet
+ /// (an unbuilt one starts building), disabled, or the scope sits outside it.
pub(super) fn query(&self, base: &Path, scope: &IndexScope, search: &AtSearch<'_>) -> Answer {
- let mut guard = self.lock();
- loop {
- let step = match &*guard {
- IndexState::Unbuilt => Step::Build,
- IndexState::Building => Step::Wait,
- IndexState::Ready { .. } => Step::Serve,
- IndexState::Disabled => Step::Walk,
- };
- match step {
- Step::Wait => {
- guard = self
- .shared
- .ready
- .wait(guard)
- .unwrap_or_else(PoisonError::into_inner);
- }
- Step::Build => {
- drop(guard);
- self.build(base, MAX_INDEX_ENTRIES);
- guard = self.lock();
- }
- Step::Walk => return Answer::Walk,
- Step::Serve => {
- let IndexState::Ready { entries, .. } = &*guard else {
- return Answer::Walk;
- };
- let prefix = match scope {
- // The typed directory is not in the index (the walk rules
- // exclude it): the walk lists its contents instead.
- IndexScope::Outside => return Answer::Walk,
- IndexScope::Prefix(prefix)
- if !prefix.is_empty() && !contains(entries, prefix) =>
- {
- return Answer::Walk;
- }
- IndexScope::Prefix(prefix) => prefix.as_str(),
- IndexScope::Unscoped => "",
- };
- return Answer::Served(suggestions(scan(entries, prefix, search), search));
- }
- }
+ let guard = self.shared.lock();
+ if matches!(*guard, IndexState::Unbuilt) {
+ drop(guard);
+ self.start_build(base);
+ return Answer::Walk;
}
+ let IndexState::Ready { entries, .. } = &*guard else {
+ return Answer::Walk;
+ };
+ let entries = Arc::clone(entries);
+ drop(guard);
+ let prefix = match scope {
+ // The typed directory is not in the index (the walk rules exclude it): the
+ // walk lists its contents instead.
+ IndexScope::Outside => return Answer::Walk,
+ IndexScope::Prefix(prefix) if !prefix.is_empty() && !contains(&entries, prefix) => {
+ return Answer::Walk;
+ }
+ IndexScope::Prefix(prefix) => prefix.as_str(),
+ IndexScope::Unscoped => "",
+ };
+ Answer::Served(suggestions(scan(&entries, prefix, search), search))
+ }
+
+ /// Run [`Self::build`] under `base` on a background thread.
+ pub(super) fn start_build(&self, base: &Path) {
+ let index = self.clone();
+ let base = base.to_path_buf();
+ thread::spawn(move || index.build(&base, MAX_INDEX_ENTRIES));
}
/// Build from `Unbuilt`: one full walk under `base`, at most `cap` entries, with the
@@ -170,7 +149,7 @@ impl FileIndex {
/// leaves `Disabled`.
pub(super) fn build(&self, base: &Path, cap: usize) -> bool {
{
- let mut guard = self.lock();
+ let mut guard = self.shared.lock();
if !matches!(&*guard, IndexState::Unbuilt) {
return matches!(&*guard, IndexState::Ready { .. });
}
@@ -189,7 +168,7 @@ impl FileIndex {
));
if let Some(entries) = walk_index_entries(base, cap) {
self.replace(IndexState::Ready {
- entries,
+ entries: Arc::new(entries),
_watcher: watcher,
});
true
@@ -199,9 +178,9 @@ impl FileIndex {
}
}
- /// Swap in a new state and wake every waiter.
+ /// Swap in a new state and wake the applier waiting out a build.
fn replace(&self, state: IndexState) {
- let mut guard = self.lock();
+ let mut guard = self.shared.lock();
*guard = state;
self.shared.ready.notify_all();
}
@@ -331,9 +310,11 @@ impl Drop for ThreadEntries<'_> {
}
/// The one watcher-draining thread: each batch — everything queued at a wake-up, the
-/// coalescing — applies to the index under the lock, and a batch that arrives during a
-/// build waits for the build to finish first. Holding the channel's receiver and only
-/// a weak index reference, it exits when the watcher drops with the provider.
+/// coalescing — is planned against a snapshot of the entries with no lock held (the
+/// filesystem reads and the merge stay off the queries' lock) and published under the
+/// lock as a pointer swap; a batch that arrives during a build waits for the build to
+/// finish first. Holding the channel's receiver and only a weak index reference, it exits
+/// when the watcher drops with the provider.
fn spawn_applier(
shared: Weak<Shared>,
events: Receiver<notify::Result<Event>>,
@@ -349,8 +330,8 @@ fn spawn_applier(
while let Ok(event) = events.try_recv() {
batch.push(event);
}
- let mut guard = shared.state.lock().unwrap_or_else(PoisonError::into_inner);
- loop {
+ let mut guard = shared.lock();
+ let entries = loop {
match &*guard {
IndexState::Building => {
guard = shared
@@ -358,48 +339,77 @@ fn spawn_applier(
.wait(guard)
.unwrap_or_else(PoisonError::into_inner);
}
- IndexState::Ready { .. } => {
- apply_batch(&mut guard, &batch, &base, &canonical_base);
- #[cfg(test)]
- shared.ready.notify_all();
- // Invalidation drops this watcher's ownership. Exit while
- // still holding the lock, before a query can rebuild and
- // expose its new state to any old queued callbacks.
- if !matches!(*guard, IndexState::Ready { .. }) {
- return;
- }
- break;
- }
+ IndexState::Ready { entries, .. } => break Arc::clone(entries),
// The watcher that fed this batch is gone: the state is a
// rebuilt or fallback one, and nothing applies to it.
IndexState::Unbuilt | IndexState::Disabled => return,
}
+ };
+ drop(guard);
+ let update = plan_batch(&entries, &batch, &base, &canonical_base);
+ if matches!(update, Update::Unchanged) {
+ continue;
+ }
+ let mut guard = shared.lock();
+ update.publish(&mut guard);
+ #[cfg(test)]
+ shared.ready.notify_all();
+ // Invalidation drops this watcher's ownership. Exit while still holding
+ // the lock, before a query can rebuild and expose its new state to any
+ // old queued callbacks.
+ if !matches!(*guard, IndexState::Ready { .. }) {
+ return;
}
}
})
}
-/// Apply one watcher batch using each path's current existence. A structural event on
+/// One watcher batch's effect on the ready index, planned against its entries.
+enum Update {
+ Unchanged,
+ /// Rebuild on the next query.
+ Rebuild,
+ Disable,
+ /// The batch's new entries: sorted, no duplicates.
+ Replace(Vec<Box<str>>),
+}
+
+impl Update {
+ /// Publish into the ready `state` the update was planned on.
+ fn publish(self, state: &mut IndexState) {
+ match self {
+ Update::Unchanged => {}
+ Update::Rebuild => *state = IndexState::Unbuilt,
+ Update::Disable => *state = IndexState::Disabled,
+ Update::Replace(new) => {
+ if let IndexState::Ready { entries, .. } = state {
+ *entries = Arc::new(new);
+ }
+ }
+ }
+ }
+}
+
+/// Plan one watcher batch using each path's current existence: every filesystem read
+/// happens here, against the batch's starting `entries`. A structural event on
/// an indexed directory re-walks its subtree; ordinary file saves keep the typed-form
/// fast path. A watcher error disables the index; rescan notices, a structural event on
/// the base or a `.git` marker, an event on a `.git` file, or changed ignore rules
/// trigger a lazy rebuild. A batch exceeding [`MAX_INDEX_ENTRIES`] disables the index.
-fn apply_batch(
- state: &mut IndexState,
+fn plan_batch(
+ entries: &[Box<str>],
batch: &[notify::Result<Event>],
base: &Path,
canonical_base: &Path,
-) {
+) -> Update {
if batch.iter().any(Result::is_err) {
- *state = IndexState::Disabled;
- return;
+ return Update::Disable;
}
let mut rels: BTreeSet<String> = BTreeSet::new();
let mut replaced: BTreeSet<String> = BTreeSet::new();
for event in batch.iter().flatten() {
if event.need_rescan() {
- *state = IndexState::Unbuilt;
- return;
+ return Update::Rebuild;
}
// Reads change nothing the index tracks — and the walker's own
// directory opens (plus any search over the session tree) arrive as
@@ -417,8 +427,7 @@ fn apply_batch(
// cannot locate every real filename (or distinguish all collisions).
// Rebuild through the shared walker instead of mutating a wrong key.
if Path::new(&rel) != raw_rel {
- *state = IndexState::Unbuilt;
- return;
+ return Update::Rebuild;
}
// A directory's name event may be the only event for an entire
// moved subtree. Its current existence cannot prove it is still
@@ -448,8 +457,7 @@ fn apply_batch(
rel.rsplit('/').next() == Some(GIT_DIR)
&& (replaced.contains(rel) || base.join(rel).is_file())
}) {
- *state = IndexState::Unbuilt;
- return;
+ return Update::Rebuild;
}
let rels: Vec<String> = rels
.iter()
@@ -457,12 +465,8 @@ fn apply_batch(
.cloned()
.collect();
if rels.iter().any(|rel| is_ignore_rules_name(rel)) {
- *state = IndexState::Unbuilt;
- return;
+ return Update::Rebuild;
}
- let IndexState::Ready { entries, .. } = state else {
- return;
- };
// Every decision reads the batch's starting entries; the edits apply once per
// batch, because each single edit of the sorted entries shifts their tail.
let mut removals: Vec<Range<usize>> = Vec::new();
@@ -496,8 +500,7 @@ fn apply_batch(
// metadata changes remain no-ops.
if is_directory && replaced.contains(rel) {
if rel.is_empty() {
- *state = IndexState::Unbuilt;
- return;
+ return Update::Rebuild;
}
if contains(entries, &form) {
removals.push(range_under(entries, &form));
@@ -531,17 +534,14 @@ fn apply_batch(
if rel.is_empty() {
// The base itself is gone: rebuild on the next query (into an empty
// or failing walk) instead of serving the dead tree.
- *state = IndexState::Unbuilt;
- return;
+ return Update::Rebuild;
}
remove_subtree(entries, rel, &mut removals);
}
- remove_ranges(entries, removals);
let mut additions: Vec<Box<str>> = Vec::new();
for rel in rewalked {
- if !walk_subtree(base, rel, entries.len(), &mut additions) {
- *state = IndexState::Disabled;
- return;
+ if !walk_subtree(base, rel, &mut additions) {
+ return Update::Disable;
}
}
for (parent, paths) in inserted {
@@ -573,34 +573,37 @@ fn apply_batch(
} else {
rel.clone()
}));
- if is_directory && !walk_subtree(base, &rel, entries.len(), &mut additions) {
- *state = IndexState::Disabled;
- return;
+ if is_directory && !walk_subtree(base, &rel, &mut additions) {
+ return Update::Disable;
}
}
}
- if !additions.is_empty() {
- entries.extend(additions);
- // The stable sort takes the sorted entries as one run, so the merge stays linear.
- entries.sort();
- entries.dedup();
+ if removals.iter().all(Range::is_empty) && additions.is_empty() {
+ return Update::Unchanged;
}
- if entries.len() > MAX_INDEX_ENTRIES {
- *state = IndexState::Disabled;
+ let mut merged = entries.to_vec();
+ remove_ranges(&mut merged, removals);
+ merged.extend(additions);
+ // The stable sort takes the sorted entries as one run, so the merge stays linear.
+ merged.sort();
+ merged.dedup();
+ if merged.len() > MAX_INDEX_ENTRIES {
+ return Update::Disable;
}
+ Update::Replace(merged)
}
/// Walk the directory `rel`'s subtree with the shared rules into `additions` — `false`
-/// once the index (`indexed` entries plus the additions) passes [`MAX_INDEX_ENTRIES`]:
-/// an over-cap batch disables the index, like an over-cap build does.
-fn walk_subtree(base: &Path, rel: &str, indexed: usize, additions: &mut Vec<Box<str>>) -> bool {
+/// once the additions alone pass [`MAX_INDEX_ENTRIES`]: an over-cap batch disables the
+/// index, like an over-cap build does (the merged total is checked after the walks).
+fn walk_subtree(base: &Path, rel: &str, additions: &mut Vec<Box<str>>) -> bool {
for form in walk_builder(&base.join(rel))
.build()
.filter_map(Result::ok)
.filter_map(|entry| fd_printed_form(&entry, base))
{
additions.push(Box::from(form));
- if indexed + additions.len() > MAX_INDEX_ENTRIES {
+ if additions.len() > MAX_INDEX_ENTRIES {
return false;
}
}
@@ -674,6 +677,27 @@ fn remove_ranges(entries: &mut Vec<Box<str>>, mut removals: Vec<Range<usize>>) {
});
}
+#[cfg(test)]
+mod query_tests {
+ use super::*;
+ use crate::autocomplete::fuzzy_file_search::resolve_search;
+ use std::sync::atomic::AtomicBool;
+
+ #[test]
+ fn query_walks_while_the_index_builds() {
+ let index = FileIndex::new();
+ index.replace(IndexState::Building);
+ let base = Path::new("base");
+ let cancel = AtomicBool::new(false);
+ let mut answer = None;
+ resolve_search(base, "@", &cancel, |search, scope| {
+ answer = Some(index.query(base, scope, search));
+ None
+ });
+ assert!(matches!(answer, Some(Answer::Walk)));
+ }
+}
+
#[cfg(test)]
mod freshness_tests {
use super::*;
@@ -681,15 +705,23 @@ mod freshness_tests {
fn snapshot(base: &Path) -> IndexState {
IndexState::Ready {
- entries: walk_index_entries(base, MAX_INDEX_ENTRIES).expect("walk"),
+ entries: Arc::new(walk_index_entries(base, MAX_INDEX_ENTRIES).expect("walk")),
_watcher: notify::recommended_watcher(|_: notify::Result<Event>| {}).expect("watcher"),
}
}
+ /// Plan and publish one batch, as the applier does.
+ fn apply_batch(state: &mut IndexState, batch: &[notify::Result<Event>], base: &Path) {
+ let IndexState::Ready { entries, .. } = state else {
+ panic!("ready state");
+ };
+ plan_batch(entries, batch, base, base).publish(state);
+ }
+
fn assert_matches_live_walk(state: &IndexState, base: &Path) {
let expected = walk_index_entries(base, MAX_INDEX_ENTRIES).expect("live walk");
let actual = match state {
- IndexState::Ready { entries, .. } => entries.clone(),
+ IndexState::Ready { entries, .. } => entries.to_vec(),
IndexState::Unbuilt => walk_index_entries(base, MAX_INDEX_ENTRIES).expect("rebuild"),
_ => panic!("unexpected state"),
};
@@ -749,7 +781,7 @@ mod freshness_tests {
std::fs::create_dir(base.join("dir")).expect("mkdir");
std::fs::write(base.join("dir/new.rs"), "new").expect("write");
let event = Event::new(notify::EventKind::Modify(kind)).add_path(base.join("dir"));
- apply_batch(&mut state, &[Ok(event)], base, base);
+ apply_batch(&mut state, &[Ok(event)], base);
assert_matches_live_walk(&state, base);
}
}
@@ -767,7 +799,7 @@ mod freshness_tests {
notify::event::DataChange::Content,
)))
.add_path(path);
- apply_batch(&mut state, &[Ok(event)], base, base);
+ apply_batch(&mut state, &[Ok(event)], base);
assert_matches_live_walk(&state, base);
}
@@ -787,7 +819,7 @@ mod freshness_tests {
notify::event::DataChange::Content,
)))
.add_path(path);
- apply_batch(&mut state, &[Ok(event)], base, base);
+ apply_batch(&mut state, &[Ok(event)], base);
assert_matches_live_walk(&state, base);
}
@@ -805,6 +837,7 @@ mod freshness_tests {
let IndexState::Ready { entries, .. } = &mut state else {
panic!("ready snapshot");
};
+ let entries = Arc::make_mut(entries);
for index in entries.len()..MAX_INDEX_ENTRIES {
entries.push(format!("unaffected-{index:07}.rs").into_boxed_str());
}
@@ -818,11 +851,11 @@ mod freshness_tests {
Ok(Event::new(notify::EventKind::Create(CreateKind::Folder))
.add_path(base.join("dir/nested"))),
];
- apply_batch(&mut state, &events, base, base);
+ apply_batch(&mut state, &events, base);
let IndexState::Ready { entries, .. } = state else {
panic!("overlapping refreshes must not disable an index within the cap");
};
- assert_eq!(entries, expected);
+ assert_eq!(*entries, expected);
}
#[test]
@@ -838,7 +871,7 @@ mod freshness_tests {
std::fs::rename(&source, base.join("dest")).expect("rename");
let event = Event::new(notify::EventKind::Modify(ModifyKind::Name(RenameMode::To)))
.add_path(base.join("dest"));
- apply_batch(&mut state, &[Ok(event)], &base, &base);
+ apply_batch(&mut state, &[Ok(event)], &base);
assert_matches_live_walk(&state, &base);
}
@@ -858,7 +891,7 @@ mod freshness_tests {
Ok(Event::new(notify::EventKind::Create(CreateKind::Folder))
.add_path(base.join("dest"))),
];
- apply_batch(&mut state, &events, &base, &base);
+ apply_batch(&mut state, &events, &base);
assert_matches_live_walk(&state, &base);
}
@@ -874,7 +907,7 @@ mod freshness_tests {
std::fs::write(base.join("new.rs"), "new").expect("write");
let event =
Event::new(notify::EventKind::Remove(RemoveKind::Folder)).add_path(base.clone());
- apply_batch(&mut state, &[Ok(event)], &base, &base);
+ apply_batch(&mut state, &[Ok(event)], &base);
assert_matches_live_walk(&state, &base);
}
@@ -889,7 +922,7 @@ mod freshness_tests {
notify::event::MetadataKind::WriteTime,
)))
.add_path(base.join(".git"));
- apply_batch(&mut state, &[Ok(event)], base, base);
+ apply_batch(&mut state, &[Ok(event)], base);
assert!(
matches!(state, IndexState::Ready { .. }),
"ordinary Git directory metadata must not rebuild the index"
@@ -908,13 +941,13 @@ mod freshness_tests {
std::fs::create_dir(base.join(".git")).expect("git init");
let event =
Event::new(notify::EventKind::Create(CreateKind::Folder)).add_path(base.join(".git"));
- apply_batch(&mut state, &[Ok(event)], base, base);
+ apply_batch(&mut state, &[Ok(event)], base);
assert_matches_live_walk(&state, base);
let mut state = snapshot(base);
std::fs::remove_dir(base.join(".git")).expect("remove git marker");
let event =
Event::new(notify::EventKind::Remove(RemoveKind::Folder)).add_path(base.join(".git"));
- apply_batch(&mut state, &[Ok(event)], base, base);
+ apply_batch(&mut state, &[Ok(event)], base);
assert_matches_live_walk(&state, base);
}
}
diff --git a/crates/pa-tui/src/autocomplete/fuzzy_file_search.rs b/crates/pa-tui/src/autocomplete/fuzzy_file_search.rs
index e366615ad1..e85c1e9282 100644
--- a/crates/pa-tui/src/autocomplete/fuzzy_file_search.rs
+++ b/crates/pa-tui/src/autocomplete/fuzzy_file_search.rs
@@ -1,9 +1,9 @@
//! The `@` fuzzy file search: fd's own `ignore`-crate walk — files and directories, links
//! followed, hidden entries included, `.git` pruned, fd's default gitignore rules — run
//! on one background thread, because a no-match walk of a large tree takes seconds; the
-//! result lands on the receiver. From the first query on, the session's file index
-//! (see [`file_index`]) answers instead; the walk stays as the fallback for scopes the
-//! index cannot serve.
+//! result lands on the receiver. Once the session's file index (see [`file_index`]) is
+//! built, it answers instead, on the caller's thread; the walk stays as the fallback
+//! until then and for scopes the index cannot serve.
use std::cmp::Ordering;
use std::path::{Path, PathBuf};
@@ -18,7 +18,7 @@ use regex::RegexBuilder;
use super::file_index::{Answer, FileIndex, IndexScope};
use super::{
build_completion_value, expand_home_path, parse_path_prefix, CompletionItem, SuggestionKind,
- Suggestions,
+ SuggestionLookup, Suggestions,
};
/// The `.git` directory name: pruned from every walk.
@@ -43,15 +43,38 @@ impl Drop for FileSearch {
}
}
-/// Start the background search for the typed `@` token against the
-/// session cwd.
-pub(super) fn spawn(base: &Path, index: FileIndex, at_prefix: String) -> FileSearch {
+/// Look up the typed `@` token against the session cwd: the file index answers on the
+/// caller's thread when it serves the query, else the walk runs in the background.
+pub(super) fn lookup(
+ base: &Path,
+ index: &FileIndex,
+ at_prefix: String,
+) -> Option<SuggestionLookup> {
+ let cancel = AtomicBool::new(false);
+ let mut walk = false;
+ let served = resolve_search(base, &at_prefix, &cancel, |search, scope| {
+ match index.query(base, scope, search) {
+ Answer::Served(answered) => answered,
+ Answer::Walk => {
+ walk = true;
+ None
+ }
+ }
+ });
+ if walk {
+ return Some(SuggestionLookup::Searching(spawn(base, at_prefix)));
+ }
+ served.map(SuggestionLookup::Ready)
+}
+
+/// Start the background walk for the typed `@` token.
+fn spawn(base: &Path, at_prefix: String) -> FileSearch {
let (tx, results) = mpsc::channel();
let cancel = Arc::new(AtomicBool::new(false));
let base = base.to_path_buf();
let thread_cancel = Arc::clone(&cancel);
thread::spawn(move || {
- let _ = tx.send(search(&base, &index, &at_prefix, &thread_cancel));
+ let _ = tx.send(search(&base, &at_prefix, &thread_cancel));
});
FileSearch { results, cancel }
}
@@ -243,27 +266,15 @@ fn walk_directory(search: &AtSearch<'_>) -> Vec<String> {
found.into_inner().unwrap_or_else(PoisonError::into_inner)
}
-/// The walk thread body: answer from the file index when it serves the query (walking
-/// once to build it on first use), fall back to the live walk otherwise, and rank.
-/// `None` means no menu: fd matched nothing, the query was an invalid regex (fd exits
-/// non-zero), or every entry scored zero.
-fn search(
- base: &Path,
- index: &FileIndex,
- at_prefix: &str,
- cancel: &AtomicBool,
-) -> Option<Suggestions> {
- resolve_search(base, at_prefix, cancel, |search, scope| {
- match index.query(base, scope, search) {
- Answer::Served(answered) => answered,
- Answer::Walk => {
- let found = walk_directory(search);
- suggestions(
- top_ranked(found.iter().filter_map(|form| search.score(form))),
- search,
- )
- }
- }
+/// The walk thread body: walk the typed scope and rank. `None` means no menu: fd
+/// matched nothing, or every entry scored zero.
+fn search(base: &Path, at_prefix: &str, cancel: &AtomicBool) -> Option<Suggestions> {
+ resolve_search(base, at_prefix, cancel, |search, _| {
+ let found = walk_directory(search);
+ suggestions(
+ top_ranked(found.iter().filter_map(|form| search.score(form))),
+ search,
+ )
})
}
diff --git a/crates/pa-tui/src/autocomplete/tests.rs b/crates/pa-tui/src/autocomplete/tests.rs
index 5cabb59eda..14f555a59e 100644
--- a/crates/pa-tui/src/autocomplete/tests.rs
+++ b/crates/pa-tui/src/autocomplete/tests.rs
@@ -256,18 +256,12 @@ fn at_prefix_lists_nested_fuzzy_matches() {
std::fs::write(base.join("other.md"), "x").expect("write");
let provider = provider(base.to_str().unwrap());
let values = |text: &str| -> Vec<String> {
- match provider.get_suggestions(&[text.to_string()], 0, text.chars().count(), false) {
- Some(SuggestionLookup::Searching(search)) => search
- .results
- .recv()
- .expect("the search finishes")
- .expect("suggestions")
- .items
- .into_iter()
- .map(|item| item.value)
- .collect(),
- other => panic!("expected a searching lookup, got {other:?}"),
- }
+ at_suggestions(&provider, text)
+ .expect("suggestions")
+ .items
+ .into_iter()
+ .map(|item| item.value)
+ .collect()
};
// The exact `.hidden/partial` name sorts ahead of the
// `partial_match.rs` prefix match; `.git` never lists.
@@ -601,19 +595,25 @@ fn render_shows_the_source_tag_as_a_trailing_segment() {
);
}
-/// The `@` suggestion items of one provider query, waiting for the background search.
-fn at_values(provider: &CombinedAutocompleteProvider, text: &str) -> Vec<CompletionItem> {
+/// One `@` provider query's answer: the built index's inline one, or the background
+/// walk's once it finishes.
+fn at_suggestions(provider: &CombinedAutocompleteProvider, text: &str) -> Option<Suggestions> {
match provider.get_suggestions(&[text.to_string()], 0, text.chars().count(), false) {
- Some(SuggestionLookup::Searching(search)) => search
- .results
- .recv()
- .expect("the search finishes")
- .map(|suggestions| suggestions.items)
- .unwrap_or_default(),
- other => panic!("expected a searching lookup, got {other:?}"),
+ Some(SuggestionLookup::Searching(search)) => {
+ search.results.recv().expect("the search finishes")
+ }
+ Some(SuggestionLookup::Ready(suggestions)) => Some(suggestions),
+ None => None,
}
}
+/// The `@` suggestion items of one provider query.
+fn at_values(provider: &CombinedAutocompleteProvider, text: &str) -> Vec<CompletionItem> {
+ at_suggestions(provider, text)
+ .map(|suggestions| suggestions.items)
+ .unwrap_or_default()
+}
+
/// The `@` item values of one provider query.
fn at_item_values(provider: &CombinedAutocompleteProvider, text: &str) -> Vec<String> {
at_values(provider, text)
@@ -747,6 +747,21 @@ fn at_index_matches_the_walk_suggestions() {
}
}
+#[test]
+fn at_lookup_answers_inline_from_a_built_index() {
+ let outer = parity_fixture();
+ let base = outer.path().join("base");
+ let provider = provider(base.to_str().expect("utf8"));
+ assert!(provider
+ .file_index
+ .build(&base, file_index::MAX_INDEX_ENTRIES));
+ let text = "@par";
+ assert!(matches!(
+ provider.get_suggestions(&[text.to_string()], 0, text.len(), false),
+ Some(SuggestionLookup::Ready(_))
+ ));
+}
+
/// A xorshift generator: the random trees need no dependency.
struct XorShift(u64);
diff --git a/crates/pa-tui/src/input.rs b/crates/pa-tui/src/input.rs
index 90e67eae51..aff2c41e50 100644
--- a/crates/pa-tui/src/input.rs
+++ b/crates/pa-tui/src/input.rs
@@ -66,6 +66,9 @@ pub(crate) enum ReaderInput {
Event(Event),
Mouse(crate::mouse::MouseEvent),
BurstPaste(String),
+ /// The end of one terminal write: every input of the chunk was delivered before it. Not sent
+ /// while the kitty probe shares crossterm's reader lock (the write boundary is unknown then).
+ ChunkEnd,
}
/// Start the terminal input reader: whole terminal writes that look like
@@ -114,8 +117,11 @@ where
// costs no wakeups. Two bounded exceptions: a held sequence's flush deadline, and the
// Kitty setup and its answer window: an indefinite park before the first draw
// would hold crossterm's reader lock and prevent the deferred probe from reading replies.
+ // While the probe shares the lock, a zero-timeout poll can miss it and end the drain
+ // mid-write: a round marks its chunk end only when no probe ran around it.
+ let probe_pending = crate::enhanced_keys::kitty_probe_pending();
let timeout = guard.poll_deadline(Instant::now()).or_else(|| {
- if thread_waker.is_none() || crate::enhanced_keys::kitty_probe_pending() {
+ if thread_waker.is_none() || probe_pending {
Some(Duration::from_millis(POLL_TIMEOUT_MS))
} else {
None
@@ -123,7 +129,9 @@ where
});
match crossterm::event::poll_opt(timeout) {
Ok(false) => {
- if !forward(guard.flush_expired(Instant::now()), &mut on_input) {
+ let outputs = guard.flush_expired(Instant::now());
+ let whole = !probe_pending && !crate::enhanced_keys::kitty_probe_pending();
+ if !forward_chunk(outputs, whole, &mut on_input) {
return;
}
}
@@ -158,7 +166,8 @@ where
for event in events {
outputs.extend(guard.feed(event, Instant::now()));
}
- if !forward(outputs, &mut on_input) {
+ let whole = !probe_pending && !crate::enhanced_keys::kitty_probe_pending();
+ if !forward_chunk(outputs, whole, &mut on_input) {
return;
}
}
@@ -173,6 +182,20 @@ where
});
}
+/// [`forward`] one drained round, then mark its end with [`ReaderInput::ChunkEnd`] when the round
+/// is a `whole` terminal write (an empty round delivers nothing). Returns `false` when the surface
+/// stopped the reader.
+fn forward_chunk(
+ outputs: Vec<GuardOutput>,
+ whole: bool,
+ on_input: &mut dyn FnMut(ReaderInput) -> bool,
+) -> bool {
+ if outputs.is_empty() {
+ return true;
+ }
+ forward(outputs, on_input) && (!whole || on_input(ReaderInput::ChunkEnd))
+}
+
/// Deliver one drained round to the surface: a whole-write burst that reconstructs to marker-less
/// multi-line text coalesces into one [`ReaderInput::BurstPaste`]; everything else passes the TS
/// dispatch filters and forwards input by input. Returns `false` when the surface stopped the
@@ -703,6 +726,29 @@ mod tests {
);
}
+ #[test]
+ fn a_forwarded_chunk_ends_with_its_marker() {
+ let outputs = vec![
+ GuardOutput::Event(key(KeyCode::Char('/'), KeyModifiers::NONE)),
+ GuardOutput::Event(key(KeyCode::Char('l'), KeyModifiers::NONE)),
+ GuardOutput::Event(key(KeyCode::Enter, KeyModifiers::NONE)),
+ ];
+ let mut inputs = Vec::new();
+ assert!(forward_chunk(outputs, true, &mut |input| {
+ inputs.push(input);
+ true
+ }));
+ assert_eq!(
+ inputs,
+ vec![
+ ReaderInput::Event(key(KeyCode::Char('/'), KeyModifiers::NONE)),
+ ReaderInput::Event(key(KeyCode::Char('l'), KeyModifiers::NONE)),
+ ReaderInput::Event(key(KeyCode::Enter, KeyModifiers::NONE)),
+ ReaderInput::ChunkEnd,
+ ]
+ );
+ }
+
/// `forward` against a capturing sink (the paste-aware surface).
fn collect_forwarded(outputs: Vec<GuardOutput>) -> Vec<ReaderInput> {
let mut inputs = Vec::new();
diff --git a/crates/pa-tui/src/interactive.rs b/crates/pa-tui/src/interactive.rs
index cf6a7eb703..0f71f9f038 100644
--- a/crates/pa-tui/src/interactive.rs
+++ b/crates/pa-tui/src/interactive.rs
@@ -489,6 +489,8 @@ enum UiInput {
},
/// One materialized input-idle tick (the headless `SettleIdle` step).
SettleIdle,
+ /// The end of one terminal write ([`crate::input::ReaderInput::ChunkEnd`]).
+ InputChunkEnd,
WaitIdle {
timeout_ms: u64,
},
diff --git a/crates/pa-tui/src/interactive/onboarding.rs b/crates/pa-tui/src/interactive/onboarding.rs
index 321b11046e..787a1feba1 100644
--- a/crates/pa-tui/src/interactive/onboarding.rs
+++ b/crates/pa-tui/src/interactive/onboarding.rs
@@ -263,6 +263,7 @@ async fn drive_onboarding_pane(
UiInput::Submit(_)
| UiInput::SubmitAndSettle { .. }
| UiInput::SettleIdle
+ | UiInput::InputChunkEnd
| UiInput::Mouse(_)
| UiInput::WaitIdle { .. }
| UiInput::ScrollTop
diff --git a/crates/pa-tui/src/interactive/render.rs b/crates/pa-tui/src/interactive/render.rs
index 7446b9e83b..8afa48095f 100644
--- a/crates/pa-tui/src/interactive/render.rs
+++ b/crates/pa-tui/src/interactive/render.rs
@@ -127,6 +127,7 @@ impl crate::suspend::SuspendTerminal for TerminalHandoff<'_> {
pub(super) fn spawn_session_reader(ui_tx: mpsc::UnboundedSender<UiInput>, exit_guard: ExitGuard) {
crate::input::spawn_paste_aware_reader(move |input| match input {
crate::input::ReaderInput::BurstPaste(text) => ui_tx.send(UiInput::Paste(text)).is_ok(),
+ crate::input::ReaderInput::ChunkEnd => ui_tx.send(UiInput::InputChunkEnd).is_ok(),
// A report the guard reassembled from a sequence crossterm's reader split at a
// committed-`ESC` read boundary: consumed unless tracking is active, like the terminal's
// mouse events.
diff --git a/crates/pa-tui/src/interactive/run.rs b/crates/pa-tui/src/interactive/run.rs
index 2a80daa300..6769ced763 100644
--- a/crates/pa-tui/src/interactive/run.rs
+++ b/crates/pa-tui/src/interactive/run.rs
@@ -223,9 +223,10 @@ async fn run_interactive_surface(
view.code_block_indent = options.code_block_indent.clone();
// The file-completion provider browses the SESSION cwd, not the process cwd: the completion
// menu must browse the directory the user sees.
- view.editor.set_autocomplete_provider(Box::new(
- crate::autocomplete::CombinedAutocompleteProvider::from_registry(options.cwd.clone()),
- ));
+ let provider =
+ crate::autocomplete::CombinedAutocompleteProvider::from_registry(options.cwd.clone());
+ provider.start_file_index();
+ view.editor.set_autocomplete_provider(Box::new(provider));
view.editor.set_keybindings(options.keybindings.clone());
view.show_images = options.show_images;
if let Some(settings) = &options.client_settings {
@@ -534,6 +535,9 @@ async fn run_interactive_surface(
}
opening_dirty = true;
}
+ // A write boundary carries no input: the echo continues, and queued
+ // typed-ahead materializes on the run loop's tick after the open.
+ UiInput::InputChunkEnd => {}
other => {
echoing = false;
pending.push_back(other);
@@ -992,7 +996,10 @@ async fn run_interactive_surface(
sender.send((Instant::now(), renderer.headless_renders()))?;
continue;
}
- session.dirty = true;
+ let chunk_end = matches!(input, UiInput::InputChunkEnd);
+ if !chunk_end {
+ session.dirty = true;
+ }
match input {
UiInput::Key(key) => {
session.stop_selection_auto_scroll();
@@ -1116,10 +1123,10 @@ async fn run_interactive_surface(
UiInput::Mouse(event) => {
session.handle_mouse(event, &mut view);
}
- // The headless plan's pause step: the queued keystroke batch ahead of this
- // barrier is fully handled, so the parked suggestions materialize now — the
- // same state the terminal loop's 50 ms idle tick produces.
- UiInput::SettleIdle => {
+ // The headless plan's pause step or the end of a terminal write: the queued
+ // keystroke batch ahead of this barrier is fully handled, so the parked
+ // suggestions materialize now (into this input's inline paint on a terminal).
+ UiInput::SettleIdle | UiInput::InputChunkEnd => {
session.materialize_editor_autocomplete(&mut view);
}
UiInput::Submit(text) => {
@@ -1165,7 +1172,10 @@ async fn run_interactive_surface(
// Paint the handled input in this iteration: the select below can otherwise wait
// out its 50ms tick before the next draw, felt as keystroke-to-render lag. A
// handoff paints nothing: the next surface owns the pane.
- if let Some(renderer) = renderer.is_terminal_mut() {
+ if let Some(renderer) = renderer
+ .is_terminal_mut()
+ .filter(|_| !chunk_end || session.dirty)
+ {
if !session.open_agents_view && session.pending_selection.is_none() {
// The inline paint must reflect tray state the handled key just armed (the
// Ctrl+C exit hint): the refresh below the select only reaches the frame