From f20e3f35a1855e35af5f7fe6f765fba764623b7c Mon Sep 17 00:00:00 2001 From: Keith Simmons Date: Wed, 13 Apr 2022 12:30:33 -0700 Subject: [PATCH 1/3] Record scroll position in nav history --- crates/editor/src/editor.rs | 25 ++++++++++++++++++++++++- crates/editor/src/items.rs | 9 +++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index bc398754a7..73674362c6 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -850,6 +850,9 @@ struct ClipboardSelection { pub struct NavigationData { anchor: Anchor, offset: usize, + scroll_position: Vector2F, + scroll_top_anchor: Anchor, + scroll_top_offset: usize, } pub struct EditorCreated(pub ViewHandle); @@ -3896,6 +3899,7 @@ impl Editor { let buffer = self.buffer.read(cx).read(cx); let offset = position.to_offset(&buffer); let point = position.to_point(&buffer); + let scroll_top_offset = self.scroll_top_anchor.to_offset(&buffer); drop(buffer); if let Some(new_position) = new_position { @@ -3908,6 +3912,9 @@ impl Editor { nav_history.push(Some(NavigationData { anchor: position, offset, + scroll_position: self.scroll_position, + scroll_top_anchor: self.scroll_top_anchor.clone(), + scroll_top_offset, })); } } @@ -6705,7 +6712,7 @@ mod tests { cx.set_global(Settings::test(cx)); use workspace::Item; let nav_history = Rc::new(RefCell::new(workspace::NavHistory::default())); - let buffer = MultiBuffer::build_simple(&sample_text(30, 5, 'a'), cx); + let buffer = MultiBuffer::build_simple(&sample_text(300, 5, 'a'), cx); cx.add_window(Default::default(), |cx| { let mut editor = build_editor(buffer.clone(), cx); @@ -6756,6 +6763,22 @@ mod tests { ); assert!(nav_history.borrow_mut().pop_backward().is_none()); + // Set scroll position to check later + editor.set_scroll_position(Vector2F::new(5.5, 5.5), cx); + let original_scroll_position = editor.scroll_position; + let original_scroll_top_anchor = editor.scroll_top_anchor.clone(); + + // Jump to the end of the document and adjust scroll + editor.move_to_end(&MoveToEnd, cx); + editor.set_scroll_position(Vector2F::new(-2.5, -0.5), cx); + assert_ne!(editor.scroll_position, original_scroll_position); + assert_ne!(editor.scroll_top_anchor, original_scroll_top_anchor); + + let nav_entry = nav_history.borrow_mut().pop_backward().unwrap(); + editor.navigate(nav_entry.data.unwrap(), cx); + assert_eq!(editor.scroll_position, original_scroll_position); + assert_eq!(editor.scroll_top_anchor, original_scroll_top_anchor); + editor }); } diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rs index 9a102dd5ce..7e767b10a2 100644 --- a/crates/editor/src/items.rs +++ b/crates/editor/src/items.rs @@ -253,12 +253,21 @@ impl Item for Editor { buffer.clip_offset(data.offset, Bias::Left) }; let newest_selection = self.newest_selection_with_snapshot::(&buffer); + + let scroll_top_anchor = if buffer.can_resolve(&data.scroll_top_anchor) { + data.scroll_top_anchor.clone() + } else { + buffer.anchor_at(data.scroll_top_offset, Bias::Left) + }; + drop(buffer); if newest_selection.head() == offset { false } else { let nav_history = self.nav_history.take(); + self.scroll_position = data.scroll_position; + self.scroll_top_anchor = scroll_top_anchor; self.select_ranges([offset..offset], Some(Autoscroll::Fit), cx); self.nav_history = nav_history; true From 9cec6d8d651bbbf5825ccd0bb0099d3a6b0c0160 Mon Sep 17 00:00:00 2001 From: Keith Simmons Date: Wed, 13 Apr 2022 13:30:03 -0700 Subject: [PATCH 2/3] add comment explaining offsets --- crates/editor/src/editor.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 73674362c6..c67a9c233f 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -848,6 +848,8 @@ struct ClipboardSelection { } pub struct NavigationData { + // Matching offsets for anchor and scroll_top_anchor allows us to recreate the anchor if the buffer + // has since been closed anchor: Anchor, offset: usize, scroll_position: Vector2F, From b893cb6d82390b6f92a9b63dc7fa9bbdf58c25ac Mon Sep 17 00:00:00 2001 From: Keith Simmons Date: Wed, 13 Apr 2022 14:53:47 -0700 Subject: [PATCH 3/3] rename NavigationData anchor and offset to cursor_anchor and cursor_offset --- crates/editor/src/editor.rs | 8 ++++---- crates/editor/src/items.rs | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index c67a9c233f..357e0237c7 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -850,8 +850,8 @@ struct ClipboardSelection { pub struct NavigationData { // Matching offsets for anchor and scroll_top_anchor allows us to recreate the anchor if the buffer // has since been closed - anchor: Anchor, - offset: usize, + cursor_anchor: Anchor, + cursor_offset: usize, scroll_position: Vector2F, scroll_top_anchor: Anchor, scroll_top_offset: usize, @@ -3912,8 +3912,8 @@ impl Editor { } nav_history.push(Some(NavigationData { - anchor: position, - offset, + cursor_anchor: position, + cursor_offset: offset, scroll_position: self.scroll_position, scroll_top_anchor: self.scroll_top_anchor.clone(), scroll_top_offset, diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rs index 7e767b10a2..9fe6e21b71 100644 --- a/crates/editor/src/items.rs +++ b/crates/editor/src/items.rs @@ -247,10 +247,10 @@ impl Item for Editor { fn navigate(&mut self, data: Box, cx: &mut ViewContext) -> bool { if let Some(data) = data.downcast_ref::() { let buffer = self.buffer.read(cx).read(cx); - let offset = if buffer.can_resolve(&data.anchor) { - data.anchor.to_offset(&buffer) + let offset = if buffer.can_resolve(&data.cursor_anchor) { + data.cursor_anchor.to_offset(&buffer) } else { - buffer.clip_offset(data.offset, Bias::Left) + buffer.clip_offset(data.cursor_offset, Bias::Left) }; let newest_selection = self.newest_selection_with_snapshot::(&buffer);