Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
editor: Add duplicate selection command (#21154)
CharlesChen0823b3de19a6bd19
diff --git a/crates/editor/src/actions.rs b/crates/editor/src/actions.rsindex eb0fcaa1e50..721b71ddecb 100644--- a/crates/editor/src/actions.rs+++ b/crates/editor/src/actions.rs@@ -251,6 +251,7 @@ gpui::actions!(DisplayCursorNames,DuplicateLineDown,DuplicateLineUp,+ DuplicateSelection,ExpandAllHunkDiffs,ExpandMacroRecursively,FindAllReferences,diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rsindex f86acc72b7f..38b34892323 100644--- a/crates/editor/src/editor.rs+++ b/crates/editor/src/editor.rs@@ -6119,6 +6119,28 @@ impl Editor {});}+ pub fn duplicate_selection(&mut self, _: &DuplicateSelection, cx: &mut ViewContext<Self>) {+ let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));+ let buffer = &display_map.buffer_snapshot;+ let selections = self.selections.all::<Point>(cx);++ let mut edits = Vec::new();+ for selection in selections.iter() {+ let start = selection.start;+ let end = selection.end;+ let text = buffer.text_for_range(start..end).collect::<String>();+ edits.push((selection.end..selection.end, text));+ }++ self.transact(cx, |this, cx| {+ this.buffer.update(cx, |buffer, cx| {+ buffer.edit(edits, None, cx);+ });++ this.request_autoscroll(Autoscroll::fit(), cx);+ });+ }+pub fn duplicate_line(&mut self, upwards: bool, cx: &mut ViewContext<Self>) {let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));let buffer = &display_map.buffer_snapshot;diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rsindex 65c5347d368..ce020c8ca81 100644--- a/crates/editor/src/editor_tests.rs+++ b/crates/editor/src/editor_tests.rs@@ -3895,6 +3895,28 @@ fn test_duplicate_line(cx: &mut TestAppContext) {]);});++ let view = cx.add_window(|cx| {+ let buffer = MultiBuffer::build_simple("abc\ndef\nghi\n", cx);+ build_editor(buffer, cx)+ });+ _ = view.update(cx, |view, cx| {+ view.change_selections(None, cx, |s| {+ s.select_display_ranges([+ DisplayPoint::new(DisplayRow(0), 1)..DisplayPoint::new(DisplayRow(1), 1),+ DisplayPoint::new(DisplayRow(1), 2)..DisplayPoint::new(DisplayRow(2), 1),+ ])+ });+ view.duplicate_selection(&DuplicateSelection, cx);+ assert_eq!(view.display_text(cx), "abc\ndbc\ndef\ngf\nghi\n");+ assert_eq!(+ view.selections.display_ranges(cx),+ vec![+ DisplayPoint::new(DisplayRow(0), 1)..DisplayPoint::new(DisplayRow(1), 1),+ DisplayPoint::new(DisplayRow(2), 2)..DisplayPoint::new(DisplayRow(3), 1),+ ]+ );+ });}#[gpui::test]diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rsindex 25c419fa62e..26b5cae32dc 100644--- a/crates/editor/src/element.rs+++ b/crates/editor/src/element.rs@@ -218,6 +218,7 @@ impl EditorElement {register_action(view, cx, Editor::cut_to_end_of_line);register_action(view, cx, Editor::duplicate_line_up);register_action(view, cx, Editor::duplicate_line_down);+ register_action(view, cx, Editor::duplicate_selection);register_action(view, cx, Editor::move_line_up);register_action(view, cx, Editor::move_line_down);register_action(view, cx, Editor::transpose);