project_panel: Add Alt/Opt+Click
to expand/collapse a directory and all its contents (#22896)
Closes #15966 This PR adds `Alt/Opt+Click` to expand or collapse a directory and all its contents. Context: The current `expand_entry` scans immediate child subdirectories if they aren’t loaded, while `expand_all_for_entry` scans the entire subtree. The latter takes longer, so we wait for it to complete to ensure accurate results. For full directory scan, instead of using `refresh_entries_for_paths(vec![path])`, which requires specifying all explicit paths to refresh, we use `add_path_prefix_to_scan`, which eliminates the need to list every path. Both methods internally call `reload_entries_for_paths`, which invokes `should_scan_directory`. This determines whether to scan deeper based on a path prefix match between the given directory and its subdirectories, returning `true` for `add_path_prefix_to_scan`. The existing code handles scanning, removing path prefixes after scans complete, and managing ignored directories. How it works (Expand): 1. Alt clicking on non-ignored closed directory, expands it and all its subdirectories, except ignored subdirectories. This helps while working on mono repos, where you might not want to expand dirs like `node_modules`, `dist`, etc or git submodules, when you expand any root dir. In example, `draft` and `posts` dir are ignored dir. [expand-1.webm](https://github.com/user-attachments/assets/07d3f724-0757-408f-b349-5beb4ee8440e) 2. Alt clicking on ignored closed directory, expands it and all its subdirectories. This is when you explicitly want to do it, on dirs like `node_modules`, `dist`, etc. In example, `dist` dir is ignored dir. [expand-2.webm](https://github.com/user-attachments/assets/99e55883-ab1a-4a9c-a0f0-48026991a922) 3. In case of auto folded subdirectories, expand all action will take precedence over it. That is, it will unfold all the subdirectories inside clicked dir. This is intentional, as user explicitly wants to reveal as much content as possible. (This is my personal opinion on how it should work). [expand-3.webm](https://github.com/user-attachments/assets/f20b0311-e92a-4e34-b640-1469b0d6fa16) How it works (Collapse): 1. Alt clicking any opened directory will collapse it and all its children, whether ignored or not. This is when you want to start from a fresh state. 2. When auto fold is enabled in settings, collapse action will also fold all subdirectories that it can fold. This is to bring it back to its fresh state as mentioned above. [collapse-1-2.webm](https://github.com/user-attachments/assets/74db6cee-0afa-406b-a9a2-7421083a2c2a) Future: - Using keybinding to expand/collapse all for selected entry - Handle expand/collapse all for folded entry Todos: - [x] Expand entries logic - [x] Handle remote worktree for expand - [x] Figure out scan complete status - [x] Move expansion logic to status update event - [x] Collapse entries logic - [x] Handle fold/unfold subdirs interaction - [x] Do not expand git ignored sub-dirs - [x] Tests - [x] Test Remote Release Notes: - Added Alt/Opt+Click functionality to expand or collapse a directory and all its contents.
This commit is contained in:
parent
793873bdc9
commit
5c650cdcb2
7 changed files with 640 additions and 13 deletions
|
@ -117,7 +117,7 @@ pub struct LoadedBinaryFile {
|
|||
pub struct LocalWorktree {
|
||||
snapshot: LocalSnapshot,
|
||||
scan_requests_tx: channel::Sender<ScanRequest>,
|
||||
path_prefixes_to_scan_tx: channel::Sender<Arc<Path>>,
|
||||
path_prefixes_to_scan_tx: channel::Sender<PathPrefixScanRequest>,
|
||||
is_scanning: (watch::Sender<bool>, watch::Receiver<bool>),
|
||||
_background_scanner_tasks: Vec<Task<()>>,
|
||||
update_observer: Option<UpdateObservationState>,
|
||||
|
@ -129,6 +129,11 @@ pub struct LocalWorktree {
|
|||
share_private_files: bool,
|
||||
}
|
||||
|
||||
pub struct PathPrefixScanRequest {
|
||||
path: Arc<Path>,
|
||||
done: SmallVec<[barrier::Sender; 1]>,
|
||||
}
|
||||
|
||||
struct ScanRequest {
|
||||
relative_paths: Vec<Arc<Path>>,
|
||||
done: SmallVec<[barrier::Sender; 1]>,
|
||||
|
@ -1097,6 +1102,32 @@ impl Worktree {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn expand_all_for_entry(
|
||||
&mut self,
|
||||
entry_id: ProjectEntryId,
|
||||
cx: &Context<Worktree>,
|
||||
) -> Option<Task<Result<()>>> {
|
||||
match self {
|
||||
Worktree::Local(this) => this.expand_all_for_entry(entry_id, cx),
|
||||
Worktree::Remote(this) => {
|
||||
let response = this.client.request(proto::ExpandAllForProjectEntry {
|
||||
project_id: this.project_id,
|
||||
entry_id: entry_id.to_proto(),
|
||||
});
|
||||
Some(cx.spawn(move |this, mut cx| async move {
|
||||
let response = response.await?;
|
||||
this.update(&mut cx, |this, _| {
|
||||
this.as_remote_mut()
|
||||
.unwrap()
|
||||
.wait_for_snapshot(response.worktree_scan_id as usize)
|
||||
})?
|
||||
.await?;
|
||||
Ok(())
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn handle_create_entry(
|
||||
this: Entity<Self>,
|
||||
request: proto::CreateProjectEntry,
|
||||
|
@ -1154,6 +1185,21 @@ impl Worktree {
|
|||
})
|
||||
}
|
||||
|
||||
pub async fn handle_expand_all_for_entry(
|
||||
this: Entity<Self>,
|
||||
request: proto::ExpandAllForProjectEntry,
|
||||
mut cx: AsyncAppContext,
|
||||
) -> Result<proto::ExpandAllForProjectEntryResponse> {
|
||||
let task = this.update(&mut cx, |this, cx| {
|
||||
this.expand_all_for_entry(ProjectEntryId::from_proto(request.entry_id), cx)
|
||||
})?;
|
||||
task.ok_or_else(|| anyhow!("no such entry"))?.await?;
|
||||
let scan_id = this.read_with(&cx, |this, _| this.scan_id())?;
|
||||
Ok(proto::ExpandAllForProjectEntryResponse {
|
||||
worktree_scan_id: scan_id as u64,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn handle_rename_entry(
|
||||
this: Entity<Self>,
|
||||
request: proto::RenameProjectEntry,
|
||||
|
@ -1238,7 +1284,7 @@ impl LocalWorktree {
|
|||
fn start_background_scanner(
|
||||
&mut self,
|
||||
scan_requests_rx: channel::Receiver<ScanRequest>,
|
||||
path_prefixes_to_scan_rx: channel::Receiver<Arc<Path>>,
|
||||
path_prefixes_to_scan_rx: channel::Receiver<PathPrefixScanRequest>,
|
||||
cx: &Context<Worktree>,
|
||||
) {
|
||||
let snapshot = self.snapshot();
|
||||
|
@ -1961,6 +2007,19 @@ impl LocalWorktree {
|
|||
}))
|
||||
}
|
||||
|
||||
fn expand_all_for_entry(
|
||||
&self,
|
||||
entry_id: ProjectEntryId,
|
||||
cx: &Context<Worktree>,
|
||||
) -> Option<Task<Result<()>>> {
|
||||
let path = self.entry_for_id(entry_id).unwrap().path.clone();
|
||||
let mut rx = self.add_path_prefix_to_scan(path.clone());
|
||||
Some(cx.background_executor().spawn(async move {
|
||||
rx.next().await;
|
||||
Ok(())
|
||||
}))
|
||||
}
|
||||
|
||||
fn refresh_entries_for_paths(&self, paths: Vec<Arc<Path>>) -> barrier::Receiver {
|
||||
let (tx, rx) = barrier::channel();
|
||||
self.scan_requests_tx
|
||||
|
@ -1972,8 +2031,15 @@ impl LocalWorktree {
|
|||
rx
|
||||
}
|
||||
|
||||
pub fn add_path_prefix_to_scan(&self, path_prefix: Arc<Path>) {
|
||||
self.path_prefixes_to_scan_tx.try_send(path_prefix).ok();
|
||||
pub fn add_path_prefix_to_scan(&self, path_prefix: Arc<Path>) -> barrier::Receiver {
|
||||
let (tx, rx) = barrier::channel();
|
||||
self.path_prefixes_to_scan_tx
|
||||
.try_send(PathPrefixScanRequest {
|
||||
path: path_prefix,
|
||||
done: smallvec![tx],
|
||||
})
|
||||
.ok();
|
||||
rx
|
||||
}
|
||||
|
||||
fn refresh_entry(
|
||||
|
@ -4007,7 +4073,7 @@ struct BackgroundScanner {
|
|||
status_updates_tx: UnboundedSender<ScanState>,
|
||||
executor: BackgroundExecutor,
|
||||
scan_requests_rx: channel::Receiver<ScanRequest>,
|
||||
path_prefixes_to_scan_rx: channel::Receiver<Arc<Path>>,
|
||||
path_prefixes_to_scan_rx: channel::Receiver<PathPrefixScanRequest>,
|
||||
next_entry_id: Arc<AtomicUsize>,
|
||||
phase: BackgroundScannerPhase,
|
||||
watcher: Arc<dyn Watcher>,
|
||||
|
@ -4132,23 +4198,24 @@ impl BackgroundScanner {
|
|||
}
|
||||
}
|
||||
|
||||
path_prefix = self.path_prefixes_to_scan_rx.recv().fuse() => {
|
||||
let Ok(path_prefix) = path_prefix else { break };
|
||||
log::trace!("adding path prefix {:?}", path_prefix);
|
||||
path_prefix_request = self.path_prefixes_to_scan_rx.recv().fuse() => {
|
||||
let Ok(request) = path_prefix_request else { break };
|
||||
log::trace!("adding path prefix {:?}", request.path);
|
||||
|
||||
let did_scan = self.forcibly_load_paths(&[path_prefix.clone()]).await;
|
||||
let did_scan = self.forcibly_load_paths(&[request.path.clone()]).await;
|
||||
if did_scan {
|
||||
let abs_path =
|
||||
{
|
||||
let mut state = self.state.lock();
|
||||
state.path_prefixes_to_scan.insert(path_prefix.clone());
|
||||
state.snapshot.abs_path.as_path().join(&path_prefix)
|
||||
state.path_prefixes_to_scan.insert(request.path.clone());
|
||||
state.snapshot.abs_path.as_path().join(&request.path)
|
||||
};
|
||||
|
||||
if let Some(abs_path) = self.fs.canonicalize(&abs_path).await.log_err() {
|
||||
self.process_events(vec![abs_path]).await;
|
||||
}
|
||||
}
|
||||
self.send_status_update(false, request.done);
|
||||
}
|
||||
|
||||
paths = fs_events_rx.next().fuse() => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue