vim: Fix ctrl-u/ctrl-d with high vertical_scroll_margin (#16031)

This makes sure that the `vertical_scroll_margin` doesn't leave the
cursor out of screen or somewhere it shouldn't go when it's higher than
the visible lines on screen.

So we cap it to `visible_line_count / 2`, similar to nvim:


5aa1a9532c/src/nvim/window.c (L6560)

Fixes #15101

Release Notes:

- Fixed `ctrl-u`/`ctrl-d` in Vim mode not working correctly when
`vertical_scroll_margin` is set to a really high value.

Co-authored-by: Bennet <bennet@zed.dev>
This commit is contained in:
Thorsten Ball 2024-08-09 16:00:05 +02:00 committed by GitHub
parent 09c9ed4765
commit c1872e9cb0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -92,6 +92,9 @@ fn scroll_editor(
let mut head = selection.head(); let mut head = selection.head();
let top = top_anchor.to_display_point(map); let top = top_anchor.to_display_point(map);
let vertical_scroll_margin =
(vertical_scroll_margin as u32).min(visible_line_count as u32 / 2);
if preserve_cursor_position { if preserve_cursor_position {
let old_top = old_top_anchor.to_display_point(map); let old_top = old_top_anchor.to_display_point(map);
let new_row = if old_top.row() == top.row() { let new_row = if old_top.row() == top.row() {
@ -108,12 +111,12 @@ fn scroll_editor(
let min_row = if top.row().0 == 0 { let min_row = if top.row().0 == 0 {
DisplayRow(0) DisplayRow(0)
} else { } else {
DisplayRow(top.row().0 + vertical_scroll_margin as u32) DisplayRow(top.row().0 + vertical_scroll_margin)
}; };
let max_row = DisplayRow( let max_row = DisplayRow(
top.row().0 top.row().0
+ (visible_line_count as u32) + (visible_line_count as u32)
.saturating_sub(vertical_scroll_margin as u32) .saturating_sub(vertical_scroll_margin)
.saturating_sub(1), .saturating_sub(1),
); );