Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Fix horizontal autoscroll not following cursor on long selections (#62691)

vinod akshatdfb696697e0a
diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs
index af3e1dd88cf..379c5fcef54 100644
--- a/crates/editor/src/editor_tests.rs
+++ b/crates/editor/src/editor_tests.rs
@@ -3633,6 +3633,156 @@ async fn test_autoscroll_relative(cx: &mut TestAppContext) {
});
}
+#[gpui::test]
+async fn test_autoscroll_horizontally_long_selection_tracks_cursor(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+ let mut cx = EditorTestContext::new(cx).await;
+
+ let window = cx.window;
+ // Narrow viewport so a long line can't possibly fit on screen at once.
+ cx.simulate_window_resize(window, size(px(200.), px(300.)));
+
+ let long_line = "x".repeat(250);
+ cx.set_state(&format!("ˇ{long_line}"));
+
+ cx.update_editor(|editor, window, cx| {
+ assert_eq!(
+ editor.snapshot(window, cx).scroll_position(),
+ gpui::Point::new(0., 0.0)
+ );
+ });
+
+ // Simulate shift+end: extend the selection from column 0 to the end
+ // of the long line. Before the fix, this selection's span was wider
+ // than the viewport, which caused autoscroll_horizontally to bail
+ // out entirely and leave scroll_position.x at 0.
+ cx.update_editor(|editor, window, cx| {
+ editor.change_selections(Default::default(), window, cx, |selections| {
+ selections.select_ranges([Point::new(0, 0)..Point::new(0, 250)]);
+ });
+ });
+ cx.run_until_parked();
+
+ cx.update_editor(|editor, window, cx| {
+ let scroll_x = editor.snapshot(window, cx).scroll_position().x;
+ let visible_columns = editor.visible_column_count().unwrap();
+ let right_edge = scroll_x + visible_columns;
+ assert!(
+ scroll_x <= 250.,
+ "head must not be scrolled past, scroll_x = {scroll_x}"
+ );
+ assert!(
+ right_edge > 249.,
+ "head column must be at the right edge, visible columns end at {right_edge}"
+ );
+ });
+}
+
+#[gpui::test]
+async fn test_autoscroll_horizontally_padded_span_boundary_still_scrolls(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+ let mut cx = EditorTestContext::new(cx).await;
+ let window = cx.window;
+
+ // Long line so scroll_width stays large and never clamps target_right,
+ // isolating the case below. Viewport is tuned so a 5-char selection's
+ // raw glyph span fits, but its padded span (+ gutter margin + em_advance,
+ // the same padding the final scroll guard uses) does not.
+ cx.simulate_window_resize(window, size(px(146.), px(300.)));
+
+ let line = "x".repeat(300);
+ cx.set_state(&format!("ˇ{line}"));
+
+ cx.update_editor(|editor, window, cx| {
+ assert_eq!(
+ editor.snapshot(window, cx).scroll_position(),
+ gpui::Point::new(0., 0.0)
+ );
+ });
+
+ // Select a short span in the middle of the line (e.g. simulating a
+ // search match). Its raw glyph span fits the viewport, but the padded
+ // span does not, so the fix must still scroll to reveal it.
+ cx.update_editor(|editor, window, cx| {
+ editor.change_selections(Default::default(), window, cx, |selections| {
+ selections.select_ranges([Point::new(0, 100)..Point::new(0, 105)]);
+ });
+ });
+ cx.run_until_parked();
+
+ cx.update_editor(|editor, window, cx| {
+ let scroll_x = editor.snapshot(window, cx).scroll_position().x;
+ let visible_columns = editor.visible_column_count().unwrap();
+ let right_edge = scroll_x + visible_columns;
+ assert!(
+ scroll_x <= 105.,
+ "selection head must be visible, scroll_x = {scroll_x}"
+ );
+ assert!(
+ right_edge > 105.,
+ "selection head must be visible, visible columns end at {right_edge}"
+ );
+ });
+}
+
+#[gpui::test]
+async fn test_autoscroll_horizontally_fitting_selection_reveals_full_span(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+ let mut cx = EditorTestContext::new(cx).await;
+ let window = cx.window;
+
+ // Viewport wide enough to fit a short selection's full span at once.
+ cx.simulate_window_resize(window, size(px(200.), px(300.)));
+
+ let line = "x".repeat(300);
+ cx.set_state(&format!("ˇ{line}"));
+
+ // Scroll right first so the whole line is out of view.
+ cx.update_editor(|editor, window, cx| {
+ editor.change_selections(Default::default(), window, cx, |selections| {
+ selections.select_ranges([Point::new(0, 300)..Point::new(0, 300)]);
+ });
+ });
+ cx.run_until_parked();
+
+ let scrolled_right_x = cx.update_editor(|editor, window, cx| {
+ let scroll_x = editor.snapshot(window, cx).scroll_position().x;
+ assert!(
+ scroll_x > 200.,
+ "should be scrolled far right, scroll_x = {scroll_x}"
+ );
+ scroll_x
+ });
+
+ // Select a short span in the middle of the line (e.g. simulating a
+ // search match). Since it fits within the viewport, both edges of
+ // the selection should end up visible, not just the head.
+ cx.update_editor(|editor, window, cx| {
+ editor.change_selections(Default::default(), window, cx, |selections| {
+ selections.select_ranges([Point::new(0, 100)..Point::new(0, 105)]);
+ });
+ });
+ cx.run_until_parked();
+
+ cx.update_editor(|editor, window, cx| {
+ let scroll_x = editor.snapshot(window, cx).scroll_position().x;
+ let visible_columns = editor.visible_column_count().unwrap();
+ let right_edge = scroll_x + visible_columns;
+ assert!(
+ scroll_x < scrolled_right_x,
+ "must scroll left towards the selection, scroll_x = {scroll_x}"
+ );
+ assert!(
+ scroll_x <= 100.5,
+ "selection start must be visible, scroll_x = {scroll_x}"
+ );
+ assert!(
+ right_edge > 105.,
+ "selection end must be visible, visible columns end at {right_edge}"
+ );
+ });
+}
+
#[gpui::test]
async fn test_exclude_overscroll_margin_clamps_scroll_position(cx: &mut TestAppContext) {
init_test(cx, |_| {});
diff --git a/crates/editor/src/scroll/autoscroll.rs b/crates/editor/src/scroll/autoscroll.rs
index 5b6cff71c7b..74cea8461e2 100644
--- a/crates/editor/src/scroll/autoscroll.rs
+++ b/crates/editor/src/scroll/autoscroll.rs
@@ -405,17 +405,31 @@ impl Editor {
},
);
- target_left = target_left.min(ScrollOffset::from(
- layouts[head.row().minus(start_row) as usize]
- .x_for_index(start_column as usize)
- + self.gutter_dimensions.margin,
- ));
- target_right = target_right.max(
- ScrollOffset::from(
- layouts[head.row().minus(start_row) as usize]
- .x_for_index(end_column as usize),
- ) + em_advance,
+ let layout = &layouts[head.row().minus(start_row) as usize];
+
+ let mut candidate_left = ScrollOffset::from(
+ layout.x_for_index(start_column as usize) + self.gutter_dimensions.margin,
);
+ let mut candidate_right =
+ ScrollOffset::from(layout.x_for_index(end_column as usize)) + em_advance;
+
+ // If the full selection span (with the same padding used below) doesn't
+ // fit in the viewport, fall back to just tracking the cursor (head)
+ // instead of the whole selection, otherwise autoscroll gives up entirely.
+ if candidate_right - candidate_left > viewport_width {
+ let head_column = head.column();
+ let head_column_clamped = cmp::min(row_line_len, head_column);
+ candidate_left = ScrollOffset::from(
+ layout.x_for_index(head_column as usize)
+ + self.gutter_dimensions.margin,
+ );
+ candidate_right =
+ ScrollOffset::from(layout.x_for_index(head_column_clamped as usize))
+ + em_advance;
+ }
+
+ target_left = target_left.min(candidate_left);
+ target_right = target_right.max(candidate_right);
}
}
} else {