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.

project_panel: Fix shift-click and shift-arrow selection for folded directories (#65266)

tsutoringo3190b1f5e328
diff --git a/crates/project_panel/src/project_panel.rs b/crates/project_panel/src/project_panel.rs
index 7fb5e6d7efa..551cd6c5fbd 100644
--- a/crates/project_panel/src/project_panel.rs
+++ b/crates/project_panel/src/project_panel.rs
@@ -1886,9 +1886,10 @@ impl ProjectPanel {
});
return;
}
- if let Some(selection) = self.selection {
- let (mut worktree_ix, mut entry_ix, _) =
- self.index_for_selection(selection).unwrap_or_default();
+ if let Some(previous_selection) = self.selection {
+ let (mut worktree_ix, mut entry_ix, _) = self
+ .index_for_selection(previous_selection)
+ .unwrap_or_default();
if entry_ix > 0 {
entry_ix -= 1;
} else if worktree_ix > 0 {
@@ -1909,7 +1910,7 @@ impl ProjectPanel {
};
self.selection = Some(selection);
if window.modifiers().shift {
- self.marked_entries.push(selection);
+ self.extend_marked_entries(previous_selection, selection);
}
self.autoscroll(cx);
cx.notify();
@@ -1918,6 +1919,30 @@ impl ProjectPanel {
}
}
+ fn extend_marked_entries(
+ &mut self,
+ previous_selection: SelectedEntry,
+ selection: SelectedEntry,
+ ) {
+ // Mark the entry the selection started from too, so that extending a selection
+ // with the keyboard includes it, the same as a shift-click range does.
+ if !self.marked_entries.contains(&previous_selection) {
+ self.marked_entries.push(previous_selection);
+ }
+ if !self.marked_entries.contains(&selection) {
+ self.marked_entries.push(selection);
+ }
+ self.select_outermost_folded_ancestor(selection.entry_id);
+ }
+
+ // A vertical selection gesture says nothing about which segment of a folded
+ // directory is meant, so select the whole folded directory.
+ fn select_outermost_folded_ancestor(&mut self, entry_id: ProjectEntryId) {
+ if let Some(folds) = self.state.ancestors.get_mut(&entry_id) {
+ folds.set_active_index(0);
+ }
+ }
+
fn confirm(&mut self, _: &Confirm, window: &mut Window, cx: &mut Context<Self>) {
if let Some(task) = self.confirm_edit(true, window, cx) {
task.detach_and_notify_err(self.workspace.clone(), window, cx);
@@ -3210,9 +3235,10 @@ impl ProjectPanel {
});
return;
}
- if let Some(selection) = self.selection {
- let (mut worktree_ix, mut entry_ix, _) =
- self.index_for_selection(selection).unwrap_or_default();
+ if let Some(previous_selection) = self.selection {
+ let (mut worktree_ix, mut entry_ix, _) = self
+ .index_for_selection(previous_selection)
+ .unwrap_or_default();
if let Some(worktree_entries) = self
.state
.visible_entries
@@ -3240,7 +3266,7 @@ impl ProjectPanel {
};
self.selection = Some(selection);
if window.modifiers().shift {
- self.marked_entries.push(selection);
+ self.extend_marked_entries(previous_selection, selection);
}
self.autoscroll(cx);
@@ -6356,9 +6382,18 @@ impl ProjectPanel {
},
);
- for selection in &new_selections {
- if !project_panel.marked_entries.contains(selection) {
- project_panel.marked_entries.push(*selection);
+ for new_selection in &new_selections {
+ if !project_panel.marked_entries.contains(new_selection) {
+ project_panel.marked_entries.push(*new_selection);
+ }
+
+ let is_intermediate_entry = new_selection.entry_id
+ != selection.entry_id
+ && new_selection.entry_id != clicked_entry.entry_id;
+
+ if is_intermediate_entry {
+ project_panel
+ .select_outermost_folded_ancestor(new_selection.entry_id);
}
}
diff --git a/crates/project_panel/src/project_panel_tests.rs b/crates/project_panel/src/project_panel_tests.rs
index 999dd13a49b..23e6f60b76b 100644
--- a/crates/project_panel/src/project_panel_tests.rs
+++ b/crates/project_panel/src/project_panel_tests.rs
@@ -10,6 +10,7 @@ use gpui::{
AnyWindowHandle, Empty, Entity, InputEvent as _, KeyDownEvent, Keystroke, Size, TestAppContext,
VisualTestContext,
};
+use itertools::iproduct;
use language::{
Diagnostic, DiagnosticEntry, DiagnosticMessage, DiagnosticSourceKind, LanguageServerId,
PointUtf16, Unclipped,
@@ -5355,7 +5356,7 @@ async fn test_multiple_marked_entries(cx: &mut gpui::TestAppContext) {
"v project_root",
" v dir_1",
" v nested_dir",
- " file_a.py",
+ " file_a.py <== marked",
" file_1.py <== selected <== marked",
]
);
@@ -5474,6 +5475,77 @@ async fn test_multiple_marked_entries(cx: &mut gpui::TestAppContext) {
);
}
+#[gpui::test]
+async fn test_shift_arrow_marks_starting_entry(cx: &mut gpui::TestAppContext) {
+ init_test(cx);
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree(
+ "/root",
+ json!({
+ "a": {},
+ "b": {},
+ "c.txt": "",
+ "d.txt": "",
+ }),
+ )
+ .await;
+
+ let project = Project::test(fs.clone(), ["/root".as_ref()], cx).await;
+ let window = cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
+ let workspace = window
+ .read_with(cx, |mw, _| mw.workspace().clone())
+ .unwrap();
+ let cx = &mut VisualTestContext::from_window(window.into(), cx);
+ let panel = workspace.update_in(cx, ProjectPanel::new);
+ cx.run_until_parked();
+
+ select_path(&panel, "root/b", cx);
+ cx.simulate_modifiers_change(gpui::Modifiers {
+ shift: true,
+ ..Default::default()
+ });
+ cx.update(|window, cx| {
+ panel.update(cx, |panel, cx| {
+ panel.select_next(&SelectNext, window, cx);
+ panel.select_next(&SelectNext, window, cx);
+ })
+ });
+ assert_eq!(
+ visible_entries_as_strings(&panel, 0..10, cx),
+ &[
+ "v root",
+ " > a",
+ " > b <== marked",
+ " c.txt <== marked",
+ " d.txt <== selected <== marked",
+ ]
+ );
+
+ cx.simulate_modifiers_change(Default::default());
+ cx.update(|window, cx| panel.update(cx, |panel, cx| panel.cancel(&menu::Cancel, window, cx)));
+ select_path(&panel, "root/c.txt", cx);
+ cx.simulate_modifiers_change(gpui::Modifiers {
+ shift: true,
+ ..Default::default()
+ });
+ cx.update(|window, cx| {
+ panel.update(cx, |panel, cx| {
+ panel.select_previous(&SelectPrevious, window, cx);
+ panel.select_previous(&SelectPrevious, window, cx);
+ })
+ });
+ assert_eq!(
+ visible_entries_as_strings(&panel, 0..10, cx),
+ &[
+ "v root",
+ " > a <== selected <== marked",
+ " > b <== marked",
+ " c.txt <== marked",
+ " d.txt",
+ ]
+ );
+}
+
#[gpui::test]
async fn test_dragged_selection_resolve_entry(cx: &mut gpui::TestAppContext) {
init_test(cx);
@@ -5723,6 +5795,172 @@ async fn test_drag_marked_entries_in_folded_directories(cx: &mut gpui::TestAppCo
);
}
+#[gpui::test]
+async fn test_range_selection_drag_moves_entire_folded_directories(cx: &mut TestAppContext) {
+ init_test_with_editor(cx);
+ cx.update(|cx| {
+ let settings = *ProjectPanelSettings::get_global(cx);
+ ProjectPanelSettings::override_global(
+ ProjectPanelSettings {
+ auto_fold_dirs: true,
+ ..settings
+ },
+ cx,
+ );
+ });
+
+ // Cmd/ctrl-clicking the source keeps the folded rows marked with their inner
+ // directory, which the range selection must still widen to the whole row.
+ for (range_selection, source_click_modifiers, copy, (source_path, target_path)) in iproduct!(
+ [RangeSelection::ShiftClick, RangeSelection::ShiftArrows],
+ [Modifiers::none(), Modifiers::secondary_key()],
+ [false, true],
+ [
+ ("project/root/a", "project/root/other"),
+ ("project/root/other", "project/root/a"),
+ ]
+ ) {
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree(
+ path!("/project"),
+ json!({
+ "root": {
+ "a": { "sample.txt": "sample" },
+ "c": { "d": { "sample2.txt": "sample2" } },
+ "e": { "f": {} },
+ "other": {}
+ }
+ }),
+ )
+ .await;
+ let original_paths = fs.paths(false);
+ let project = Project::test(fs.clone(), [path!("/project").as_ref()], cx).await;
+ let (window, panel) = create_drag_test_panel(&project, cx);
+ let cx = &mut VisualTestContext::from_window(window, cx);
+ toggle_expand_dir(&panel, "project/root", cx);
+
+ assert_eq!(
+ visible_entries_as_strings(&panel, 0..10, cx),
+ &[
+ "v project",
+ " v root <== selected",
+ " > a",
+ " > c/d",
+ " > e/f",
+ " > other",
+ ]
+ );
+
+ select_folded_path_with_mark(&panel, "project/root/c/d", "project/root/c/d", cx);
+ select_folded_path_with_mark(&panel, "project/root/e/f", "project/root/e/f", cx);
+ click_project_entry(&panel, source_path, source_click_modifiers, cx);
+ extend_range_selection_to(&panel, range_selection, target_path, cx);
+
+ cx.simulate_modifiers_change(Modifiers {
+ alt: copy && cfg!(target_os = "macos"),
+ control: copy && cfg!(not(target_os = "macos")),
+ ..Modifiers::none()
+ });
+ drag_selection_to(&panel, "project", false, cx);
+
+ let mut expected_paths = [
+ path!("/"),
+ path!("/project"),
+ path!("/project/a"),
+ path!("/project/a/sample.txt"),
+ path!("/project/c"),
+ path!("/project/c/d"),
+ path!("/project/c/d/sample2.txt"),
+ path!("/project/e"),
+ path!("/project/e/f"),
+ path!("/project/other"),
+ path!("/project/root"),
+ ]
+ .into_iter()
+ .map(PathBuf::from)
+ .collect::<BTreeSet<_>>();
+ if copy {
+ expected_paths.extend(original_paths);
+ }
+ assert_eq!(
+ fs.paths(false).into_iter().collect::<BTreeSet<_>>(),
+ expected_paths,
+ "dragging the range {source_path} to {target_path} with {range_selection:?}, source click {source_click_modifiers:?} and copy={copy} should include the entire folded directories"
+ );
+ }
+}
+
+#[gpui::test]
+async fn test_range_selection_folded_endpoints(cx: &mut TestAppContext) {
+ init_test_with_editor(cx);
+ cx.update(|cx| {
+ let settings = *ProjectPanelSettings::get_global(cx);
+ ProjectPanelSettings::override_global(
+ ProjectPanelSettings {
+ auto_fold_dirs: true,
+ ..settings
+ },
+ cx,
+ );
+ });
+
+ // (leaf, segment clicked before selecting, outermost directory)
+ let folded_rows = [
+ ("root/a/b/c", "root/a/b", "root/a"),
+ ("root/d/e/f", "root/d/e/f", "root/d"),
+ ("root/g/h/i", "root/g/h", "root/g"),
+ ];
+
+ for (range_selection, (source_path, target_path)) in iproduct!(
+ [RangeSelection::ShiftClick, RangeSelection::ShiftArrows],
+ [("root/a/b/c", "root/g/h/i"), ("root/g/h/i", "root/a/b/c")]
+ ) {
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree(
+ path!("/root"),
+ json!({
+ "a": { "b": { "c": {} } },
+ "d": { "e": { "f": {} } },
+ "g": { "h": { "i": {} } }
+ }),
+ )
+ .await;
+ let project = Project::test(fs, [path!("/root").as_ref()], cx).await;
+ let (window, panel) = create_drag_test_panel(&project, cx);
+ let cx = &mut VisualTestContext::from_window(window, cx);
+
+ for (leaf_path, clicked_path, _) in folded_rows {
+ select_folded_path_with_mark(&panel, leaf_path, clicked_path, cx);
+ }
+ click_project_entry(&panel, source_path, Modifiers::none(), cx);
+ extend_range_selection_to(&panel, range_selection, target_path, cx);
+
+ for (leaf_path, clicked_path, outermost_path) in folded_rows {
+ // A click points at an exact segment, so shift-click keeps both endpoints'
+ // segments. Shift-arrows only keep the segment of the row they started from.
+ let keeps_clicked_segment = leaf_path == source_path
+ || (leaf_path == target_path
+ && matches!(range_selection, RangeSelection::ShiftClick));
+ let expected_path = if keeps_clicked_segment {
+ clicked_path
+ } else {
+ outermost_path
+ };
+ let leaf_entry =
+ find_project_entry(&panel, leaf_path, cx).expect("folded directory should exist");
+ let expected_entry = find_project_entry(&panel, expected_path, cx)
+ .expect("expected directory should exist");
+ panel.read_with(cx, |panel, _| {
+ assert_eq!(
+ panel.resolve_entry(leaf_entry),
+ expected_entry,
+ "range {source_path} to {target_path} with {range_selection:?} should select {expected_path}"
+ );
+ });
+ }
+ }
+}
+
#[gpui::test]
async fn test_dragging_same_named_files_preserves_one_source_on_conflict(
cx: &mut gpui::TestAppContext,
@@ -10816,6 +11054,110 @@ pub(crate) fn select_path(panel: &Entity<ProjectPanel>, path: &str, cx: &mut Vis
cx.run_until_parked();
}
+#[derive(Debug, Clone, Copy)]
+enum RangeSelection {
+ ShiftClick,
+ ShiftArrows,
+}
+
+fn extend_range_selection_to(
+ panel: &Entity<ProjectPanel>,
+ range_selection: RangeSelection,
+ target_path: &str,
+ cx: &mut VisualTestContext,
+) {
+ match range_selection {
+ RangeSelection::ShiftClick => click_project_entry(
+ panel,
+ target_path,
+ Modifiers {
+ shift: true,
+ ..Modifiers::none()
+ },
+ cx,
+ ),
+ RangeSelection::ShiftArrows => shift_select_with_keyboard_to(panel, target_path, cx),
+ }
+}
+
+fn shift_select_with_keyboard_to(
+ panel: &Entity<ProjectPanel>,
+ target_path: &str,
+ cx: &mut VisualTestContext,
+) {
+ let target_entry_id =
+ find_project_entry(panel, target_path, cx).expect("target entry should exist");
+ cx.simulate_modifiers_change(Modifiers {
+ shift: true,
+ ..Modifiers::none()
+ });
+ cx.update(|window, cx| {
+ panel.update(cx, |panel, cx| {
+ let selection = panel
+ .selection
+ .expect("a selection is required before extending it");
+ let (_, _, target_index) = panel
+ .index_for_selection(SelectedEntry {
+ worktree_id: selection.worktree_id,
+ entry_id: target_entry_id,
+ })
+ .expect("target entry should be visible");
+ while let Some(selection) = panel.selection
+ && selection.entry_id != target_entry_id
+ {
+ let (_, _, index) = panel
+ .index_for_selection(selection)
+ .expect("selected entry should be visible");
+ if index < target_index {
+ panel.select_next(&SelectNext, window, cx);
+ } else {
+ panel.select_previous(&SelectPrevious, window, cx);
+ }
+ }
+ })
+ });
+ cx.simulate_modifiers_change(Modifiers::none());
+}
+
+fn click_project_entry(
+ panel: &Entity<ProjectPanel>,
+ path: &str,
+ modifiers: Modifiers,
+ cx: &mut VisualTestContext,
+) {
+ let entry_id = find_project_entry(panel, path, cx).expect("clicked entry should exist");
+ cx.update(|window, cx| window.draw(cx).clear(cx));
+ let position = panel.read_with(cx, |panel, cx| {
+ let worktree_id = panel
+ .project
+ .read(cx)
+ .worktree_id_for_entry(entry_id, cx)
+ .expect("clicked entry should belong to a worktree");
+ let (_, _, index) = panel
+ .index_for_entry(entry_id, worktree_id)
+ .expect("clicked entry should be visible");
+ let item_count = panel
+ .state
+ .visible_entries
+ .iter()
+ .map(|worktree| worktree.entries.len())
+ .sum::<usize>();
+ let scroll = panel.scroll_handle.0.borrow();
+ let bounds = scroll.base_handle.bounds();
+ let item_height = scroll
+ .last_item_size
+ .expect("project entries should be rendered")
+ .contents
+ .height
+ / item_count as f32;
+ bounds.origin
+ + scroll.base_handle.offset()
+ + point(bounds.size.width / 2., item_height * (index as f32 + 0.5))
+ });
+ cx.simulate_click(position, modifiers);
+ cx.run_until_parked();
+}
+
pub(crate) fn select_path_with_mark(
panel: &Entity<ProjectPanel>,
path: &str,