editor: Preserve scroll position when jumping from multibuffer (#9921)

This is a best-effort attempt, as the target offset from the top is just
an estimate; furthermore, this does not account for things like project
search header (which adds a bit of vertical offset by itself and is
removed once we jump into a buffer), but it still should improve the
situation quite a bit.

Fixes: #5296

Release Notes:

- Improved target selection when jumping from multibuffer; final
position in the buffer should more closely match the original position
of the cursor in the multibuffer.
This commit is contained in:
Piotr Osiewicz
2024-03-28 18:33:57 +01:00
committed by GitHub
parent 7f54935324
commit fcaf4383e9
3 changed files with 44 additions and 7 deletions
+10 -5
View File
@@ -7670,7 +7670,7 @@ impl Editor {
let range = target.range.to_offset(target.buffer.read(cx));
let range = editor.range_for_match(&range);
if Some(&target.buffer) == editor.buffer.read(cx).as_singleton().as_ref() {
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
editor.change_selections(Some(Autoscroll::focused()), cx, |s| {
s.select_ranges([range]);
});
} else {
@@ -7690,7 +7690,7 @@ impl Editor {
// to avoid creating a history entry at the previous cursor location.
pane.update(cx, |pane, _| pane.disable_history());
target_editor.change_selections(
Some(Autoscroll::fit()),
Some(Autoscroll::focused()),
cx,
|s| {
s.select_ranges([range]);
@@ -9504,6 +9504,7 @@ impl Editor {
path: ProjectPath,
position: Point,
anchor: language::Anchor,
offset_from_top: u32,
cx: &mut ViewContext<Self>,
) {
let workspace = self.workspace();
@@ -9531,9 +9532,13 @@ impl Editor {
};
let nav_history = editor.nav_history.take();
editor.change_selections(Some(Autoscroll::newest()), cx, |s| {
s.select_ranges([cursor..cursor]);
});
editor.change_selections(
Some(Autoscroll::top_relative(offset_from_top as usize)),
cx,
|s| {
s.select_ranges([cursor..cursor]);
},
);
editor.nav_history = nav_history;
anyhow::Ok(())