Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Fix project panel requiring two Left presses to collapse opened files’ parent (#64733)

Kirill Bulatova84858acb953
diff --git a/crates/project_panel/src/project_panel.rs b/crates/project_panel/src/project_panel.rs
index 36a25ec5f7d..3769fec8bea 100644
--- a/crates/project_panel/src/project_panel.rs
+++ b/crates/project_panel/src/project_panel.rs
@@ -1544,7 +1544,7 @@ impl ProjectPanel {
loop {
let entry_id = entry.id;
match expanded_dir_ids.binary_search(&entry_id) {
- Ok(ix) => {
+ Ok(ix) if entry.is_dir() => {
expanded_dir_ids.remove(ix);
self.selection = Some(SelectedEntry {
worktree_id,
@@ -1560,7 +1560,7 @@ impl ProjectPanel {
cx.notify();
break;
}
- Err(_) => {
+ Ok(_) | Err(_) => {
if let Some(parent_entry) =
entry.path.parent().and_then(|p| worktree.entry_for_path(p))
{
@@ -1722,7 +1722,9 @@ impl ProjectPanel {
};
let include_ignored_dirs = !entry.is_ignored;
- if let Err(ix) = expanded_dir_ids.binary_search(&entry_id) {
+ if entry.is_dir()
+ && let Err(ix) = expanded_dir_ids.binary_search(&entry_id)
+ {
expanded_dir_ids.insert(ix, entry_id);
}
@@ -1814,7 +1816,9 @@ impl ProjectPanel {
if let Some(mut entry) = worktree.entry_for_id(entry_id) {
loop {
- if let Err(ix) = expanded_dir_ids.binary_search(&entry.id) {
+ if entry.is_dir()
+ && let Err(ix) = expanded_dir_ids.binary_search(&entry.id)
+ {
expanded_dir_ids.insert(ix, entry.id);
}
@@ -4853,7 +4857,9 @@ impl ProjectPanel {
if let Some(mut entry) = worktree.entry_for_id(entry_id) {
loop {
- if let Err(ix) = expanded_dir_ids.binary_search(&entry.id) {
+ if entry.is_dir()
+ && let Err(ix) = expanded_dir_ids.binary_search(&entry.id)
+ {
expanded_dir_ids.insert(ix, entry.id);
}
diff --git a/crates/project_panel/src/project_panel_tests.rs b/crates/project_panel/src/project_panel_tests.rs
index d75248288bc..999dd13a49b 100644
--- a/crates/project_panel/src/project_panel_tests.rs
+++ b/crates/project_panel/src/project_panel_tests.rs
@@ -4130,6 +4130,112 @@ async fn test_collapse_selected_entry_scrolls_into_view(cx: &mut TestAppContext)
}
}
+#[gpui::test]
+async fn test_collapse_opened_file_scrolls_to_parent(cx: &mut TestAppContext) {
+ init_test_with_editor(cx);
+ let (panel, mut cx) = open_panel_with_tree(
+ json!({
+ "src": {
+ "a.rs": "",
+ "b.rs": "",
+ "c.rs": "",
+ "d.rs": "",
+ "e.rs": "",
+ "f.rs": "",
+ "g.rs": "",
+ "h.rs": "",
+ },
+ "tests": {
+ "a.rs": "",
+ "b.rs": "",
+ "c.rs": "",
+ "d.rs": "",
+ "e.rs": "",
+ "f.rs": "",
+ "g.rs": "",
+ "h.rs": "",
+ }
+ }),
+ size(px(800.), px(160.)),
+ cx,
+ )
+ .await;
+ let cx = &mut cx;
+ toggle_expand_dir(&panel, "root/src", cx);
+ toggle_expand_dir(&panel, "root/tests", cx);
+ select_path(&panel, "root/src/h.rs", cx);
+ panel.update_in(cx, |panel, window, cx| panel.open(&Open, window, cx));
+ cx.run_until_parked();
+
+ let workspace = panel.read_with(cx, |panel, _| {
+ panel.workspace.upgrade().expect("workspace is open")
+ });
+ ensure_single_file_is_opened(&workspace, "src/h.rs", cx);
+ let parent_id = find_project_entry(&panel, "root/src", cx).expect("src exists");
+ let opened_entry = panel.read_with(cx, |panel, _| panel.selection.expect("file is selected"));
+ let mut expected_expanded_dir_ids = ["root", "root/src", "root/tests"]
+ .map(|path| find_project_entry(&panel, path, cx).expect("directory exists"));
+ expected_expanded_dir_ids.sort_unstable();
+ for expand_children in [false, true] {
+ if expand_children {
+ toggle_expand_dir(&panel, "root/src", cx);
+ select_path(&panel, "root/src/h.rs", cx);
+ panel.update_in(cx, |panel, window, cx| {
+ panel.expand_selected_entry_and_children(
+ &ExpandSelectedEntryAndChildren,
+ window,
+ cx,
+ );
+ });
+ cx.run_until_parked();
+ }
+ panel.update_in(cx, |panel, window, cx| {
+ panel.scroll_cursor_center(&ScrollCursorCenter, window, cx);
+ });
+ cx.run_until_parked();
+ panel.read_with(cx, |panel, _| {
+ assert_eq!(
+ panel.state.expanded_dir_ids[&opened_entry.worktree_id], expected_expanded_dir_ids,
+ "expand_children={expand_children}"
+ );
+ assert!(
+ entry_row_bounds(panel, parent_id).bottom() < panel.scroll_handle.viewport().top()
+ );
+ });
+
+ panel.update_in(cx, |panel, window, cx| {
+ panel.collapse_selected_entry(&CollapseSelectedEntry, window, cx);
+ });
+ cx.run_until_parked();
+
+ assert_eq!(
+ visible_entries_as_strings(&panel, 0..usize::MAX, cx),
+ &[
+ "v root",
+ " > src <== selected",
+ " v tests",
+ " a.rs",
+ " b.rs",
+ " c.rs",
+ " d.rs",
+ " e.rs",
+ " f.rs",
+ " g.rs",
+ " h.rs",
+ ],
+ "expand_children={expand_children}"
+ );
+ panel.read_with(cx, |panel, _| {
+ assert_eq!(panel.marked_entries, [opened_entry]);
+ let parent_bounds = entry_row_bounds(panel, parent_id);
+ let viewport = panel.scroll_handle.viewport();
+ let sticky_height = parent_bounds.size.height * panel.sticky_items_count;
+ assert!(parent_bounds.top() >= viewport.top() + sticky_height);
+ assert!(parent_bounds.bottom() <= viewport.bottom());
+ });
+ }
+}
+
#[gpui::test]
async fn test_collapse_selected_entry_does_not_scroll_visible_parent(cx: &mut TestAppContext) {
init_test(cx);