From 3d4f275c52773cd65382ca7f2c9c2cee6143fe11 Mon Sep 17 00:00:00 2001 From: Thorsten Ball Date: Wed, 19 Jun 2024 17:37:17 +0200 Subject: [PATCH] editor: Add SelectPageUp/SelectPageDown actions (#13272) This adds two new actions to `editor`: - `editor::SelectPageUp` - `editor::SelectPageDown` On Linux they're bound by default to `shift-pageup` and `shift-pagedown`, which matches VS Code and JetBrains. Release Notes: - N/A --- assets/keymaps/default-linux.json | 4 ++-- crates/editor/src/actions.rs | 2 ++ crates/editor/src/editor.rs | 36 +++++++++++++++++++++++++------ crates/editor/src/element.rs | 2 ++ crates/editor/src/scroll.rs | 5 +++++ 5 files changed, 41 insertions(+), 8 deletions(-) diff --git a/assets/keymaps/default-linux.json b/assets/keymaps/default-linux.json index 9529042c6b8f66e5e0a04e8b3ee71ec64ccbc0ba..ee3ced09b302e689a8e61e8d95c1fcb0e32347af 100644 --- a/assets/keymaps/default-linux.json +++ b/assets/keymaps/default-linux.json @@ -62,11 +62,11 @@ "ctrl-up": "editor::LineUp", "ctrl-down": "editor::LineDown", "pageup": "editor::PageUp", - // "shift-pageup": "editor::MovePageUp", todo(linux) should be 'select page up' + "shift-pageup": "editor::SelectPageUp", "home": "editor::MoveToBeginningOfLine", "down": "editor::MoveDown", "pagedown": "editor::PageDown", - // "shift-pagedown": "editor::MovePageDown", todo(linux) should be 'select page down' + "shift-pagedown": "editor::SelectPageDown", "end": "editor::MoveToEndOfLine", "left": "editor::MoveLeft", "right": "editor::MoveRight", diff --git a/crates/editor/src/actions.rs b/crates/editor/src/actions.rs index 88d6df6cad0dce459d0c3367c222c9947e9e325a..22b2f02d5caee5f7750e5db0d51bd3c16c1d1c66 100644 --- a/crates/editor/src/actions.rs +++ b/crates/editor/src/actions.rs @@ -281,6 +281,8 @@ gpui::actions!( SelectToPreviousWordStart, SelectToStartOfParagraph, SelectUp, + SelectPageDown, + SelectPageUp, ShowCharacterPalette, ShowInlineCompletion, ShuffleLines, diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 5e1c6ee8cca1292897821a9dd98cb267a5ca28c7..e07e0df8f728b9e90de4f1b9f870e0f409a7cae7 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -6722,6 +6722,20 @@ impl Editor { }) } + pub fn select_page_up(&mut self, _: &SelectPageUp, cx: &mut ViewContext) { + let Some(row_count) = self.visible_row_count() else { + return; + }; + + let text_layout_details = &self.text_layout_details(cx); + + self.change_selections(Some(Autoscroll::fit()), cx, |s| { + s.move_heads_with(|map, head, goal| { + movement::up_by_rows(map, head, row_count, goal, false, &text_layout_details) + }) + }) + } + pub fn move_page_up(&mut self, action: &MovePageUp, cx: &mut ViewContext) { if self.take_rename(true, cx).is_some() { return; @@ -6732,9 +6746,7 @@ impl Editor { return; } - let row_count = if let Some(row_count) = self.visible_line_count() { - row_count as u32 - 1 - } else { + let Some(row_count) = self.visible_row_count() else { return; }; @@ -6809,6 +6821,20 @@ impl Editor { } } + pub fn select_page_down(&mut self, _: &SelectPageDown, cx: &mut ViewContext) { + let Some(row_count) = self.visible_row_count() else { + return; + }; + + let text_layout_details = &self.text_layout_details(cx); + + self.change_selections(Some(Autoscroll::fit()), cx, |s| { + s.move_heads_with(|map, head, goal| { + movement::down_by_rows(map, head, row_count, goal, false, &text_layout_details) + }) + }) + } + pub fn move_page_down(&mut self, action: &MovePageDown, cx: &mut ViewContext) { if self.take_rename(true, cx).is_some() { return; @@ -6829,9 +6855,7 @@ impl Editor { return; } - let row_count = if let Some(row_count) = self.visible_line_count() { - row_count as u32 - 1 - } else { + let Some(row_count) = self.visible_row_count() else { return; }; diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index 09ed7a870e2243b3fcdbf9b5794d0f8ca7bddf60..7716325f31e532ac3edaaf133700f08df03f96c3 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -167,6 +167,8 @@ impl EditorElement { register_action(view, cx, Editor::move_up); register_action(view, cx, Editor::move_up_by_lines); register_action(view, cx, Editor::select_up_by_lines); + register_action(view, cx, Editor::select_page_down); + register_action(view, cx, Editor::select_page_up); register_action(view, cx, Editor::cancel); register_action(view, cx, Editor::newline); register_action(view, cx, Editor::newline_above); diff --git a/crates/editor/src/scroll.rs b/crates/editor/src/scroll.rs index 095c9e98451727003e2a90c31fb360fc06ad812e..6205b53d8945e7c6fced7ee30394a860193fc91d 100644 --- a/crates/editor/src/scroll.rs +++ b/crates/editor/src/scroll.rs @@ -330,6 +330,11 @@ impl Editor { self.scroll_manager.visible_line_count } + pub fn visible_row_count(&self) -> Option { + self.visible_line_count() + .map(|line_count| line_count as u32 - 1) + } + pub(crate) fn set_visible_line_count(&mut self, lines: f32, cx: &mut ViewContext) { let opened_first_time = self.scroll_manager.visible_line_count.is_none(); self.scroll_manager.visible_line_count = Some(lines);