From 56b0bf8601b644a9ebb0b9499dd29e90250af3b2 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 14 Jun 2023 11:43:44 +0200 Subject: [PATCH] Save cursor scroll position when the editor is auto-scrolled --- crates/ai/src/assistant.rs | 9 ++++++--- crates/editor/src/editor.rs | 1 + crates/editor/src/items.rs | 2 +- crates/editor/src/scroll.rs | 23 ++++++++++++++++------- crates/editor/src/scroll/autoscroll.rs | 10 +++++----- 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/crates/ai/src/assistant.rs b/crates/ai/src/assistant.rs index e733f4d477116e0e732ada0c5cd30567cda0e7c5..a168de6c9b88a9fa84dcee9a4e724497b7dff375 100644 --- a/crates/ai/src/assistant.rs +++ b/crates/ai/src/assistant.rs @@ -693,6 +693,7 @@ impl Assistant { error: None, }, ); + cx.emit(AssistantEvent::MessagesEdited); Some(message) } else { None @@ -874,7 +875,6 @@ impl AssistantEditor { |selections| selections.select_ranges([cursor..cursor]), ); }); - self.scroll_position = self.cursor_scroll_position(cx); } } @@ -922,8 +922,11 @@ impl AssistantEditor { cx: &mut ViewContext, ) { match event { - editor::Event::ScrollPositionChanged { .. } => { - if self.cursor_scroll_position(cx) != self.scroll_position { + editor::Event::ScrollPositionChanged { autoscroll, .. } => { + let cursor_scroll_position = self.cursor_scroll_position(cx); + if *autoscroll { + self.scroll_position = cursor_scroll_position; + } else if self.scroll_position != cursor_scroll_position { self.scroll_position = None; } } diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 6e06c901f0a10dbd9a9d6edd281d8b49c6b1de19..2e0d444e0473b4b4cfe2b79f712797c2e8e799f5 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -7449,6 +7449,7 @@ pub enum Event { }, ScrollPositionChanged { local: bool, + autoscroll: bool, }, Closed, } diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rs index 9d639f9b7bd6aeebe619b9382862c75f0347c7ad..74b8e0ddb68d81f3d00fafa518cfbff4bc4c71b6 100644 --- a/crates/editor/src/items.rs +++ b/crates/editor/src/items.rs @@ -294,7 +294,7 @@ impl FollowableItem for Editor { match event { Event::Edited => true, Event::SelectionsChanged { local } => *local, - Event::ScrollPositionChanged { local } => *local, + Event::ScrollPositionChanged { local, .. } => *local, _ => false, } } diff --git a/crates/editor/src/scroll.rs b/crates/editor/src/scroll.rs index 17e8d18a625434b2fd81f8ea6938c72729aed423..a13619a82a0abfc82bdc3172b42a61bb5030d252 100644 --- a/crates/editor/src/scroll.rs +++ b/crates/editor/src/scroll.rs @@ -173,6 +173,7 @@ impl ScrollManager { scroll_position: Vector2F, map: &DisplaySnapshot, local: bool, + autoscroll: bool, workspace_id: Option, cx: &mut ViewContext, ) { @@ -203,7 +204,7 @@ impl ScrollManager { ) }; - self.set_anchor(new_anchor, top_row, local, workspace_id, cx); + self.set_anchor(new_anchor, top_row, local, autoscroll, workspace_id, cx); } fn set_anchor( @@ -211,11 +212,12 @@ impl ScrollManager { anchor: ScrollAnchor, top_row: u32, local: bool, + autoscroll: bool, workspace_id: Option, cx: &mut ViewContext, ) { self.anchor = anchor; - cx.emit(Event::ScrollPositionChanged { local }); + cx.emit(Event::ScrollPositionChanged { local, autoscroll }); self.show_scrollbar(cx); self.autoscroll_request.take(); if let Some(workspace_id) = workspace_id { @@ -296,21 +298,28 @@ impl Editor { } pub fn set_scroll_position(&mut self, scroll_position: Vector2F, cx: &mut ViewContext) { - self.set_scroll_position_internal(scroll_position, true, cx); + self.set_scroll_position_internal(scroll_position, true, false, cx); } pub(crate) fn set_scroll_position_internal( &mut self, scroll_position: Vector2F, local: bool, + autoscroll: bool, cx: &mut ViewContext, ) { let map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); hide_hover(self, cx); let workspace_id = self.workspace.as_ref().map(|workspace| workspace.1); - self.scroll_manager - .set_scroll_position(scroll_position, &map, local, workspace_id, cx); + self.scroll_manager.set_scroll_position( + scroll_position, + &map, + local, + autoscroll, + workspace_id, + cx, + ); } pub fn scroll_position(&self, cx: &mut ViewContext) -> Vector2F { @@ -326,7 +335,7 @@ impl Editor { .to_point(&self.buffer().read(cx).snapshot(cx)) .row; self.scroll_manager - .set_anchor(scroll_anchor, top_row, true, workspace_id, cx); + .set_anchor(scroll_anchor, top_row, true, false, workspace_id, cx); } pub(crate) fn set_scroll_anchor_remote( @@ -341,7 +350,7 @@ impl Editor { .to_point(&self.buffer().read(cx).snapshot(cx)) .row; self.scroll_manager - .set_anchor(scroll_anchor, top_row, false, workspace_id, cx); + .set_anchor(scroll_anchor, top_row, false, false, workspace_id, cx); } pub fn scroll_screen(&mut self, amount: &ScrollAmount, cx: &mut ViewContext) { diff --git a/crates/editor/src/scroll/autoscroll.rs b/crates/editor/src/scroll/autoscroll.rs index ed098293f6dd97dd150865fa4aa8cc3be3a3b3f6..e83e2286b1f4809d777c72257eda0e7471508ccf 100644 --- a/crates/editor/src/scroll/autoscroll.rs +++ b/crates/editor/src/scroll/autoscroll.rs @@ -136,23 +136,23 @@ impl Editor { if target_top < start_row { scroll_position.set_y(target_top); - self.set_scroll_position_internal(scroll_position, local, cx); + self.set_scroll_position_internal(scroll_position, local, true, cx); } else if target_bottom >= end_row { scroll_position.set_y(target_bottom - visible_lines); - self.set_scroll_position_internal(scroll_position, local, cx); + self.set_scroll_position_internal(scroll_position, local, true, cx); } } AutoscrollStrategy::Center => { scroll_position.set_y((first_cursor_top - margin).max(0.0)); - self.set_scroll_position_internal(scroll_position, local, cx); + self.set_scroll_position_internal(scroll_position, local, true, cx); } AutoscrollStrategy::Top => { scroll_position.set_y((first_cursor_top).max(0.0)); - self.set_scroll_position_internal(scroll_position, local, cx); + self.set_scroll_position_internal(scroll_position, local, true, cx); } AutoscrollStrategy::Bottom => { scroll_position.set_y((last_cursor_bottom - visible_lines).max(0.0)); - self.set_scroll_position_internal(scroll_position, local, cx); + self.set_scroll_position_internal(scroll_position, local, true, cx); } }