Skip to content

Commit 0713ddc

Browse files
authored
editor: Fix vertical scroll margin not accounting for file header height (zed-industries#43521)
Closes zed-industries#43178 Release Notes: - Fixed vertical scroll margin not accounting for file header height Here's the before/after: With `{ "vertical_scroll_margin": 0 }` in `~/.config/zed/settings.json` https://github.com/user-attachments/assets/418c6d7f-de0f-4da6-a038-69927b1b8b88
1 parent 6fbbc89 commit 0713ddc

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

crates/editor/src/editor_tests.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28198,3 +28198,87 @@ async fn test_multibuffer_selections_with_folding(cx: &mut TestAppContext) {
2819828198
3
2819928199
"});
2820028200
}
28201+
28202+
#[gpui::test]
28203+
async fn test_multibuffer_scroll_cursor_top_margin(cx: &mut TestAppContext) {
28204+
init_test(cx, |_| {});
28205+
28206+
let (editor, cx) = cx.add_window_view(|window, cx| {
28207+
let multi_buffer = MultiBuffer::build_multi(
28208+
[
28209+
("1\n2\n3\n", vec![Point::row_range(0..3)]),
28210+
("1\n2\n3\n4\n5\n6\n7\n8\n9\n", vec![Point::row_range(0..9)]),
28211+
],
28212+
cx,
28213+
);
28214+
Editor::new(EditorMode::full(), multi_buffer, None, window, cx)
28215+
});
28216+
28217+
let mut cx = EditorTestContext::for_editor_in(editor.clone(), cx).await;
28218+
28219+
cx.assert_excerpts_with_selections(indoc! {"
28220+
[EXCERPT]
28221+
ˇ1
28222+
2
28223+
3
28224+
[EXCERPT]
28225+
1
28226+
2
28227+
3
28228+
4
28229+
5
28230+
6
28231+
7
28232+
8
28233+
9
28234+
"});
28235+
28236+
cx.update_editor(|editor, window, cx| {
28237+
editor.change_selections(None.into(), window, cx, |s| {
28238+
s.select_ranges([MultiBufferOffset(19)..MultiBufferOffset(19)]);
28239+
});
28240+
});
28241+
28242+
cx.assert_excerpts_with_selections(indoc! {"
28243+
[EXCERPT]
28244+
1
28245+
2
28246+
3
28247+
[EXCERPT]
28248+
1
28249+
2
28250+
3
28251+
4
28252+
5
28253+
6
28254+
ˇ7
28255+
8
28256+
9
28257+
"});
28258+
28259+
cx.update_editor(|editor, _window, cx| {
28260+
editor.set_vertical_scroll_margin(0, cx);
28261+
});
28262+
28263+
cx.update_editor(|editor, window, cx| {
28264+
assert_eq!(editor.vertical_scroll_margin(), 0);
28265+
editor.scroll_cursor_top(&ScrollCursorTop, window, cx);
28266+
assert_eq!(
28267+
editor.snapshot(window, cx).scroll_position(),
28268+
gpui::Point::new(0., 12.0)
28269+
);
28270+
});
28271+
28272+
cx.update_editor(|editor, _window, cx| {
28273+
editor.set_vertical_scroll_margin(3, cx);
28274+
});
28275+
28276+
cx.update_editor(|editor, window, cx| {
28277+
assert_eq!(editor.vertical_scroll_margin(), 3);
28278+
editor.scroll_cursor_top(&ScrollCursorTop, window, cx);
28279+
assert_eq!(
28280+
editor.snapshot(window, cx).scroll_position(),
28281+
gpui::Point::new(0., 9.0)
28282+
);
28283+
});
28284+
}

crates/editor/src/scroll/actions.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,20 @@ impl Editor {
7171
window: &mut Window,
7272
cx: &mut Context<Editor>,
7373
) {
74+
let display_snapshot = self.display_snapshot(cx);
7475
let scroll_margin_rows = self.vertical_scroll_margin() as u32;
7576
let new_screen_top = self
7677
.selections
77-
.newest_display(&self.display_snapshot(cx))
78+
.newest_display(&display_snapshot)
7879
.head()
7980
.row()
8081
.0;
81-
let new_screen_top = new_screen_top.saturating_sub(scroll_margin_rows);
82+
let header_offset = display_snapshot
83+
.buffer_snapshot()
84+
.show_headers()
85+
.then(|| display_snapshot.buffer_header_height())
86+
.unwrap_or(0);
87+
let new_screen_top = new_screen_top.saturating_sub(scroll_margin_rows + header_offset);
8288
self.set_scroll_top_row(DisplayRow(new_screen_top), window, cx);
8389
}
8490

0 commit comments

Comments
 (0)