From f5823f9942c37ed4e4f2735d786e4f1f5700091a Mon Sep 17 00:00:00 2001 From: Daniel Zhu Date: Thu, 28 Mar 2024 03:52:08 -0700 Subject: [PATCH] Split `DuplicateLine` into `DuplicateLineUp` and `DuplicateLineDown` (#9715) Fixes #9601 Release Notes: - `DuplicateLine` is now split into `DuplicateLineUp` and `DuplicateLineDown` --- assets/keymaps/default-linux.json | 2 ++ assets/keymaps/default-macos.json | 9 ++------- assets/keymaps/jetbrains.json | 2 +- assets/keymaps/textmate.json | 2 +- crates/editor/src/actions.rs | 9 ++------- crates/editor/src/editor.rs | 12 ++++++++++-- crates/editor/src/editor_tests.rs | 8 ++++---- crates/editor/src/element.rs | 3 ++- crates/zed/src/zed/app_menus.rs | 5 +---- 9 files changed, 25 insertions(+), 27 deletions(-) diff --git a/assets/keymaps/default-linux.json b/assets/keymaps/default-linux.json index 22da693baf3c1d8652e30c84b6305a756a63fd02..e58aaca3b887128c252fd405f2b2e1404e1645ef 100644 --- a/assets/keymaps/default-linux.json +++ b/assets/keymaps/default-linux.json @@ -444,6 +444,8 @@ { "context": "Editor", "bindings": { + "ctrl-shift-k": "editor::DeleteLine", + "ctrl-shift-d": "editor::DuplicateLineDown", "ctrl-j": "editor::JoinLines", "ctrl-alt-backspace": "editor::DeleteToPreviousSubwordStart", "ctrl-alt-h": "editor::DeleteToPreviousSubwordStart", diff --git a/assets/keymaps/default-macos.json b/assets/keymaps/default-macos.json index 8c0353a4211c004210362f8bf635a92d4253d09d..450238d2f991d84c2c4fbcb85c62491661209cdb 100644 --- a/assets/keymaps/default-macos.json +++ b/assets/keymaps/default-macos.json @@ -318,13 +318,8 @@ "cmd-shift-k": "editor::DeleteLine", "alt-up": "editor::MoveLineUp", "alt-down": "editor::MoveLineDown", - "alt-shift-up": [ - "editor::DuplicateLine", - { - "move_upwards": true - } - ], - "alt-shift-down": "editor::DuplicateLine", + "alt-shift-up": "editor::DuplicateLineUp", + "alt-shift-down": "editor::DuplicateLineDown", "ctrl-shift-right": "editor::SelectLargerSyntaxNode", "ctrl-shift-left": "editor::SelectSmallerSyntaxNode", "cmd-d": [ diff --git a/assets/keymaps/jetbrains.json b/assets/keymaps/jetbrains.json index c1998d6cd125b3a17c81b94f60aed9e8e0d810db..4e31ec9678b15e4c3150bfd931b498e7e7b23431 100644 --- a/assets/keymaps/jetbrains.json +++ b/assets/keymaps/jetbrains.json @@ -11,7 +11,7 @@ "ctrl->": "zed::IncreaseBufferFontSize", "ctrl-<": "zed::DecreaseBufferFontSize", "ctrl-shift-j": "editor::JoinLines", - "cmd-d": "editor::DuplicateLine", + "cmd-d": "editor::DuplicateLineDown", "cmd-backspace": "editor::DeleteLine", "cmd-pagedown": "editor::MovePageDown", "cmd-pageup": "editor::MovePageUp", diff --git a/assets/keymaps/textmate.json b/assets/keymaps/textmate.json index dd3e217ae9cb3d2c856163b0df7767a3ffc3b325..3926e4e94206b5fcfd2e3c777a80d06cdbfc9516 100644 --- a/assets/keymaps/textmate.json +++ b/assets/keymaps/textmate.json @@ -9,7 +9,7 @@ "context": "Editor", "bindings": { "cmd-l": "go_to_line::Toggle", - "ctrl-shift-d": "editor::DuplicateLine", + "ctrl-shift-d": "editor::DuplicateLineDown", "cmd-b": "editor::GoToDefinition", "cmd-j": "editor::ScrollCursorCenter", "cmd-enter": "editor::NewlineBelow", diff --git a/crates/editor/src/actions.rs b/crates/editor/src/actions.rs index f36d24967d88ec0ccb367b98c7307c166a5d98dd..1a5bee8f5d94c90c2794ae5400c1c60247dc8c25 100644 --- a/crates/editor/src/actions.rs +++ b/crates/editor/src/actions.rs @@ -94,12 +94,6 @@ pub struct SelectDownByLines { pub(super) lines: u32, } -#[derive(PartialEq, Clone, Deserialize, Default)] -pub struct DuplicateLine { - #[serde(default)] - pub move_upwards: bool, -} - impl_actions!( editor, [ @@ -119,7 +113,6 @@ impl_actions!( MoveDownByLines, SelectUpByLines, SelectDownByLines, - DuplicateLine ] ); @@ -160,6 +153,8 @@ gpui::actions!( DeleteToPreviousSubwordStart, DeleteToPreviousWordStart, DisplayCursorNames, + DuplicateLineUp, + DuplicateLineDown, ExpandMacroRecursively, FindAllReferences, Fold, diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 510dcc8ee5887486eb081d3188027c3961894616..4ced3244eb7a884088783f3ae47de748513532b1 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -5123,7 +5123,7 @@ impl Editor { }); } - pub fn duplicate_line(&mut self, action: &DuplicateLine, cx: &mut ViewContext) { + pub fn duplicate_line(&mut self, upwards: bool, cx: &mut ViewContext) { let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); let buffer = &display_map.buffer_snapshot; let selections = self.selections.all::(cx); @@ -5152,7 +5152,7 @@ impl Editor { .text_for_range(start..end) .chain(Some("\n")) .collect::(); - let insert_location = if action.move_upwards { + let insert_location = if upwards { Point::new(rows.end, 0) } else { start @@ -5169,6 +5169,14 @@ impl Editor { }); } + pub fn duplicate_line_up(&mut self, _: &DuplicateLineUp, cx: &mut ViewContext) { + self.duplicate_line(true, cx); + } + + pub fn duplicate_line_down(&mut self, _: &DuplicateLineDown, cx: &mut ViewContext) { + self.duplicate_line(false, cx); + } + pub fn move_line_up(&mut self, _: &MoveLineUp, cx: &mut ViewContext) { let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); let buffer = self.buffer.read(cx).snapshot(cx); diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs index 197accd055828e10b93cdb3149b4d844efcb5668..0b01e834d2341ba61ba0f9f69ac7d2527e8c60c0 100644 --- a/crates/editor/src/editor_tests.rs +++ b/crates/editor/src/editor_tests.rs @@ -3116,7 +3116,7 @@ fn test_duplicate_line(cx: &mut TestAppContext) { DisplayPoint::new(3, 0)..DisplayPoint::new(3, 0), ]) }); - view.duplicate_line(&DuplicateLine::default(), cx); + view.duplicate_line_down(&DuplicateLineDown, cx); assert_eq!(view.display_text(cx), "abc\nabc\ndef\ndef\nghi\n\n"); assert_eq!( view.selections.display_ranges(cx), @@ -3140,7 +3140,7 @@ fn test_duplicate_line(cx: &mut TestAppContext) { DisplayPoint::new(1, 2)..DisplayPoint::new(2, 1), ]) }); - view.duplicate_line(&DuplicateLine::default(), cx); + view.duplicate_line_down(&DuplicateLineDown, cx); assert_eq!(view.display_text(cx), "abc\ndef\nghi\nabc\ndef\nghi\n"); assert_eq!( view.selections.display_ranges(cx), @@ -3166,7 +3166,7 @@ fn test_duplicate_line(cx: &mut TestAppContext) { DisplayPoint::new(3, 0)..DisplayPoint::new(3, 0), ]) }); - view.duplicate_line(&DuplicateLine { move_upwards: true }, cx); + view.duplicate_line_up(&DuplicateLineUp, cx); assert_eq!(view.display_text(cx), "abc\nabc\ndef\ndef\nghi\n\n"); assert_eq!( view.selections.display_ranges(cx), @@ -3190,7 +3190,7 @@ fn test_duplicate_line(cx: &mut TestAppContext) { DisplayPoint::new(1, 2)..DisplayPoint::new(2, 1), ]) }); - view.duplicate_line(&DuplicateLine { move_upwards: true }, cx); + view.duplicate_line_up(&DuplicateLineUp, cx); assert_eq!(view.display_text(cx), "abc\ndef\nghi\nabc\ndef\nghi\n"); assert_eq!( view.selections.display_ranges(cx), diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index 2c92d82d1e540a0d3c3604e9c6209b5106964b27..5748741af449c708e83d70be1c5544907eaea2b9 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -177,7 +177,8 @@ impl EditorElement { register_action(view, cx, Editor::delete_to_beginning_of_line); register_action(view, cx, Editor::delete_to_end_of_line); register_action(view, cx, Editor::cut_to_end_of_line); - register_action(view, cx, Editor::duplicate_line); + register_action(view, cx, Editor::duplicate_line_up); + register_action(view, cx, Editor::duplicate_line_down); register_action(view, cx, Editor::move_line_up); register_action(view, cx, Editor::move_line_down); register_action(view, cx, Editor::transpose); diff --git a/crates/zed/src/zed/app_menus.rs b/crates/zed/src/zed/app_menus.rs index c4425d983b1a96024cabc9317c0423cf8aea0d89..62a59f337f0a273d6a9b7f58a6dc71f8445cac79 100644 --- a/crates/zed/src/zed/app_menus.rs +++ b/crates/zed/src/zed/app_menus.rs @@ -98,10 +98,7 @@ pub fn app_menus() -> Vec> { MenuItem::separator(), MenuItem::action("Move Line Up", editor::actions::MoveLineUp), MenuItem::action("Move Line Down", editor::actions::MoveLineDown), - MenuItem::action( - "Duplicate Selection", - editor::actions::DuplicateLine::default(), - ), + MenuItem::action("Duplicate Selection", editor::actions::DuplicateLineDown), ], }, Menu {