From ff72c6358e6c5262527a8b79558e51c4fcd45066 Mon Sep 17 00:00:00 2001 From: William Blazer <82427064+wblazer@users.noreply.github.com> Date: Wed, 29 Jan 2025 16:50:12 -0600 Subject: [PATCH] Fix project_panel::NewSearchInDirectory to work on files (#23696) Closes #23383 This PR changes `project_panel::NewSearchInDirectory` to open project search filtered by the parent directory when triggered on a file, rather than doing nothing. Release Notes: - Improved `project_panel::NewSearchInDirectory` to search the parent directory when triggered on a file --- crates/project_panel/src/project_panel.rs | 55 ++++++++++++++++------- 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/crates/project_panel/src/project_panel.rs b/crates/project_panel/src/project_panel.rs index 1807275cbcd4d4..a5fbfed689ff51 100644 --- a/crates/project_panel/src/project_panel.rs +++ b/crates/project_panel/src/project_panel.rs @@ -2257,24 +2257,45 @@ impl ProjectPanel { cx: &mut Context, ) { if let Some((worktree, entry)) = self.selected_sub_entry(cx) { - if entry.is_dir() { - let include_root = self.project.read(cx).visible_worktrees(cx).count() > 1; - let dir_path = if include_root { - let mut full_path = PathBuf::from(worktree.read(cx).root_name()); - full_path.push(&entry.path); - Arc::from(full_path) - } else { - entry.path.clone() - }; + let dir_path = if entry.is_dir() { + entry.path.clone() + } else { + // entry is a file, use its parent directory + match entry.path.parent() { + Some(parent) => Arc::from(parent), + None => { + // File at root, open search with empty filter + self.workspace + .update(cx, |workspace, cx| { + search::ProjectSearchView::new_search_in_directory( + workspace, + Path::new(""), + window, + cx, + ); + }) + .ok(); + return; + } + } + }; - self.workspace - .update(cx, |workspace, cx| { - search::ProjectSearchView::new_search_in_directory( - workspace, &dir_path, window, cx, - ); - }) - .ok(); - } + let include_root = self.project.read(cx).visible_worktrees(cx).count() > 1; + let dir_path = if include_root { + let mut full_path = PathBuf::from(worktree.read(cx).root_name()); + full_path.push(&dir_path); + Arc::from(full_path) + } else { + dir_path + }; + + self.workspace + .update(cx, |workspace, cx| { + search::ProjectSearchView::new_search_in_directory( + workspace, &dir_path, window, cx, + ); + }) + .ok(); } }