diff --git a/assets/keymaps/default.json b/assets/keymaps/default.json index 7ca49a414d3aba99d6c908a86abd613ff8be967b..7c89eacde844b109a0b8996c0697d67d85d0432f 100644 --- a/assets/keymaps/default.json +++ b/assets/keymaps/default.json @@ -75,7 +75,7 @@ "ctrl-n": "editor::MoveDown", "ctrl-b": "editor::MoveLeft", "ctrl-f": "editor::MoveRight", - "ctrl-l": "editor::CenterScreen", + "ctrl-l": "editor::NextScreen", "alt-left": "editor::MoveToPreviousWordStart", "alt-b": "editor::MoveToPreviousWordStart", "alt-right": "editor::MoveToNextWordEnd", diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index a049239f70cda660422d00bac8aab8f720c69e93..478b1e9133ff490136d12fab3e326f18dd78ea77 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -187,7 +187,7 @@ actions!( Paste, Undo, Redo, - CenterScreen, + NextScreen, MoveUp, PageUp, MoveDown, @@ -307,7 +307,8 @@ pub fn init(cx: &mut MutableAppContext) { cx.add_action(Editor::move_down); cx.add_action(Editor::move_page_down); cx.add_action(Editor::page_down); - cx.add_action(Editor::center_screen); + cx.add_action(Editor::next_screen); + cx.add_action(Editor::move_left); cx.add_action(Editor::move_right); cx.add_action(Editor::move_to_previous_word_start); @@ -407,11 +408,25 @@ pub enum SelectMode { All, } -#[derive(PartialEq, Eq)] +#[derive(PartialEq, Eq, Default)] pub enum Autoscroll { Fit, - Center, Newest, + #[default] + Center, + Top, + Bottom, + Next, +} + +impl Autoscroll { + fn next(&self) -> Self { + match self { + Autoscroll::Center => Autoscroll::Top, + Autoscroll::Top => Autoscroll::Bottom, + _ => Autoscroll::Center, + } + } } #[derive(Copy, Clone, PartialEq, Eq)] @@ -553,6 +568,7 @@ pub struct Editor { hover_state: HoverState, link_go_to_definition_state: LinkGoToDefinitionState, visible_line_count: Option, + last_autoscroll: Option<(Vector2F, f32, f32, Autoscroll)>, _subscriptions: Vec, } @@ -1205,6 +1221,7 @@ impl Editor { hover_state: Default::default(), link_go_to_definition_state: Default::default(), visible_line_count: None, + last_autoscroll: None, _subscriptions: vec![ cx.observe(&buffer, Self::on_buffer_changed), cx.subscribe(&buffer, Self::on_buffer_event), @@ -1424,7 +1441,7 @@ impl Editor { self.set_scroll_position(scroll_position, cx); } - let (autoscroll, local) = if let Some(autoscroll) = self.autoscroll_request.take() { + let (mut autoscroll, local) = if let Some(autoscroll) = self.autoscroll_request.take() { autoscroll } else { return false; @@ -1465,6 +1482,22 @@ impl Editor { return false; } + if matches!(autoscroll, Autoscroll::Next) { + let last_autoscroll = &self.last_autoscroll; + autoscroll = if let Some(last_autoscroll) = last_autoscroll { + if self.scroll_position == last_autoscroll.0 + && first_cursor_top == last_autoscroll.1 + && last_cursor_bottom == last_autoscroll.2 + { + last_autoscroll.3.next() + } else { + Autoscroll::default() + } + } else { + Autoscroll::default() + } + } + match autoscroll { Autoscroll::Fit | Autoscroll::Newest => { let margin = margin.min(self.vertical_scroll_margin); @@ -1485,8 +1518,26 @@ impl Editor { scroll_position.set_y((first_cursor_top - margin).max(0.0)); self.set_scroll_position_internal(scroll_position, local, cx); } + Autoscroll::Next => { + unreachable!("This should be handled above") + } + Autoscroll::Top => { + scroll_position.set_y((first_cursor_top).max(0.0)); + self.set_scroll_position_internal(scroll_position, local, cx); + } + Autoscroll::Bottom => { + scroll_position.set_y((last_cursor_bottom - visible_lines).max(0.0)); + self.set_scroll_position_internal(scroll_position, local, cx); + } } + self.last_autoscroll = Some(( + self.scroll_position, + first_cursor_top, + last_cursor_bottom, + autoscroll, + )); + true } @@ -4003,7 +4054,7 @@ impl Editor { }) } - pub fn center_screen(&mut self, _: &CenterScreen, cx: &mut ViewContext) { + pub fn next_screen(&mut self, _: &NextScreen, cx: &mut ViewContext) { if self.take_rename(true, cx).is_some() { return; } @@ -4017,7 +4068,7 @@ impl Editor { return; } - self.request_autoscroll(Autoscroll::Center, cx); + self.request_autoscroll(Autoscroll::Next, cx); } pub fn move_up(&mut self, _: &MoveUp, cx: &mut ViewContext) {