M1.13: watch open buffers' folders, bufferFocused, truncated tree row
- The parent directory of every open buffer is pinned in the project
watcher (workspace::sync_buffer_dirs after every command): it counts
against the cap but is never evicted while a buffer in it is open, so
reloads and fileChangedOnDisk arrive for never-expanded folders.
- file.open on an already-open buffer emits the new bufferFocused {id};
the editor activates that tab and it outranks pending adoptions.
- The project tree shows a "Listing truncated" note row
(project.tree.truncated) after a partly listed directory.
- Tests: engine (pinning, eviction, focus), control channel
(bufferFocused), Vitest (tab switch, race, truncated row).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
7b7273a947
commit
8a71f7bd08
19 changed files with 312 additions and 30 deletions
|
|
@ -11,8 +11,17 @@
|
|||
//! interval. When the limit is reached the directory listed least recently
|
||||
//! is unwatched; listing it again watches it again. Changes inside an
|
||||
//! unwatched directory are not reported until then.
|
||||
//!
|
||||
//! The parent directory of every open buffer is pinned by
|
||||
//! [`sync_buffer_dirs`]: it is watched even if no client listed it, so
|
||||
//! [`Event::FileChangedOnDisk`] and reloads arrive for files in folders the
|
||||
//! tree never expanded. Pinned directories count against the limit but are
|
||||
//! never unwatched while a buffer in them is open, so with more pinned
|
||||
//! directories than the limit allows, the watched set exceeds it. A
|
||||
//! directory the watcher refuses (hidden, ignored or excluded) stays
|
||||
//! unwatched even when pinned.
|
||||
|
||||
use std::collections::VecDeque;
|
||||
use std::collections::{BTreeSet, VecDeque};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::mpsc::{Receiver, TryRecvError};
|
||||
|
||||
|
|
@ -42,6 +51,8 @@ pub struct Workspace {
|
|||
events: Option<Receiver<VfsEvent>>,
|
||||
/// Directories watched besides the root, least recently listed first.
|
||||
watched: VecDeque<PathBuf>,
|
||||
/// Parent directories of open buffers; never unwatched while pinned.
|
||||
pinned: BTreeSet<PathBuf>,
|
||||
}
|
||||
|
||||
impl Workspace {
|
||||
|
|
@ -76,39 +87,73 @@ impl Workspace {
|
|||
watcher,
|
||||
events: receiver,
|
||||
watched: VecDeque::new(),
|
||||
pinned: BTreeSet::new(),
|
||||
},
|
||||
events,
|
||||
))
|
||||
}
|
||||
|
||||
/// Records that a client listed `dir` and watches it, unwatching the
|
||||
/// least recently listed directory if the limit is reached. Watching is
|
||||
/// best effort: a directory the watcher refuses or cannot read is simply
|
||||
/// not watched, because listing it worked and that is what the caller
|
||||
/// asked for.
|
||||
/// least recently listed unpinned directory if the limit is reached.
|
||||
/// Watching is best effort: a directory the watcher refuses or cannot
|
||||
/// read is simply not watched, because listing it worked and that is
|
||||
/// what the caller asked for.
|
||||
pub fn touch_dir(&mut self, dir: &Path) {
|
||||
let Some(watcher) = &self.watcher else {
|
||||
if self.watch(dir) {
|
||||
self.evict();
|
||||
}
|
||||
}
|
||||
|
||||
/// Makes `dirs` the pinned directories: each is watched and kept watched
|
||||
/// until a later call leaves it out, after which it ages out like a
|
||||
/// listed directory. The root (`""`) is always watched and is skipped.
|
||||
pub fn pin_dirs(&mut self, dirs: BTreeSet<PathBuf>) {
|
||||
if self.watcher.is_none() || dirs == self.pinned {
|
||||
return;
|
||||
}
|
||||
for dir in &dirs {
|
||||
if !self.pinned.contains(dir) {
|
||||
self.watch(dir);
|
||||
}
|
||||
}
|
||||
self.pinned = dirs;
|
||||
self.evict();
|
||||
}
|
||||
|
||||
/// Watches `dir` and moves it to the most recently used end. Returns
|
||||
/// whether it is watched now.
|
||||
fn watch(&mut self, dir: &Path) -> bool {
|
||||
let Some(watcher) = &self.watcher else {
|
||||
return false;
|
||||
};
|
||||
if dir.as_os_str().is_empty() {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
if let Some(at) = self.watched.iter().position(|d| d == dir) {
|
||||
if let Some(d) = self.watched.remove(at) {
|
||||
self.watched.push_back(d);
|
||||
}
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
if !matches!(watcher.watch_dir(dir), Ok(true)) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
self.watched.push_back(dir.to_path_buf());
|
||||
true
|
||||
}
|
||||
|
||||
/// Unwatches the least recently used unpinned directories until the
|
||||
/// watched set fits the limit or only pinned directories remain.
|
||||
fn evict(&mut self) {
|
||||
let limit = self.watch_limit();
|
||||
let Some(watcher) = &self.watcher else {
|
||||
return;
|
||||
};
|
||||
while self.watched.len() > limit {
|
||||
if let Some(old) = self.watched.pop_front() {
|
||||
let Some(at) = self.watched.iter().position(|d| !self.pinned.contains(d)) else {
|
||||
return;
|
||||
};
|
||||
if let Some(old) = self.watched.remove(at) {
|
||||
watcher.unwatch_dir(&old);
|
||||
}
|
||||
}
|
||||
|
|
@ -129,6 +174,11 @@ impl Workspace {
|
|||
self.watched.iter().cloned().collect()
|
||||
}
|
||||
|
||||
/// The pinned directories (parents of open buffers), sorted.
|
||||
pub fn pinned_dirs(&self) -> Vec<PathBuf> {
|
||||
self.pinned.iter().cloned().collect()
|
||||
}
|
||||
|
||||
/// Whether the project is watched.
|
||||
pub fn is_watching(&self) -> bool {
|
||||
self.watcher.is_some()
|
||||
|
|
@ -160,11 +210,30 @@ impl Workspace {
|
|||
self.watcher = None;
|
||||
self.events = None;
|
||||
self.watched.clear();
|
||||
self.pinned.clear();
|
||||
}
|
||||
(out, failure)
|
||||
}
|
||||
}
|
||||
|
||||
/// Pins the parent directory of every open buffer in the workspace watcher
|
||||
/// (see the module docs). The engine calls it after every command, so
|
||||
/// opening, restoring and closing buffers keep the pins current.
|
||||
pub fn sync_buffer_dirs(state: &mut State) {
|
||||
let Some(workspace) = state.project.as_mut() else {
|
||||
return;
|
||||
};
|
||||
let dirs: BTreeSet<PathBuf> = state
|
||||
.buffers
|
||||
.ids()
|
||||
.into_iter()
|
||||
.filter_map(|id| state.buffers.get(id))
|
||||
.filter_map(|b| b.path.parent().map(Path::to_path_buf))
|
||||
.filter(|d| !d.as_os_str().is_empty())
|
||||
.collect();
|
||||
workspace.pin_dirs(dirs);
|
||||
}
|
||||
|
||||
/// Applies pending changes on disk to the state: queues
|
||||
/// [`Event::TreeChanged`] for affected directories, reloads clean buffers
|
||||
/// whose file changed, and queues [`Event::FileChangedOnDisk`] for dirty
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue