diff: Keep navigated hunks near viewport center
909ec447664f97db0936f7db4a5e61d69547c5e0
Navigating between hunks could leave the focused change against a viewport edge, making its surrounding context harder to follow. Preserve the scroll position only while the complete hunk remains within the central 80% of the viewport, and scroll once it crosses that comfort region. Assisted-by: Codex:gpt-5.6
1 parent
db2768e9
crates/diff/src/viewer.rs
+5 -2
| 89 | 89 | const STICKY_HEADER_ROWS: usize = 3; |
|
| 90 | 90 | /// Number of body rows moved by keyboard line-scroll inputs. |
|
| 91 | 91 | const LINE_SCROLL_ROWS: isize = 1; |
|
| 92 | 92 | /// Number of body rows moved by mouse-wheel inputs. |
|
| 93 | 93 | const WHEEL_SCROLL_ROWS: isize = 3; |
|
| 94 | + | /// Central viewport percentage where hunk navigation preserves the scroll offset. |
|
| 95 | + | const HUNK_SCROLL_COMFORT_PERCENT: usize = 80; |
|
| 94 | 96 | /// Local row of a file card's top border. |
|
| 95 | 97 | const FILE_TOP_ROW: usize = 0; |
|
| 96 | 98 | /// Local row of a file card's header. |
|
| 97 | 99 | const FILE_HEADER_ROW: usize = 1; |
|
| 98 | 100 | /// Local row of the divider between a file header and its hunk body. |
| 2560 | 2562 | let screen_row = leading + available.saturating_sub(height) / 2; |
|
| 2561 | 2563 | ||
| 2562 | 2564 | self.row.saturating_sub(screen_row) |
|
| 2563 | 2565 | } |
|
| 2564 | 2566 | HunkScroll::Preserve => { |
|
| 2565 | - | let top = self.sticky_rows(current).saturating_add(1); |
|
| 2566 | - | let bottom = visible_rows.saturating_sub(1); |
|
| 2567 | + | let inset = visible_rows.saturating_mul(100 - HUNK_SCROLL_COMFORT_PERCENT) / 200; |
|
| 2568 | + | let top = self.sticky_rows(current).saturating_add(inset); |
|
| 2569 | + | let bottom = visible_rows.saturating_sub(inset); |
|
| 2567 | 2570 | let screen_top = self.row.saturating_sub(current); |
|
| 2568 | 2571 | let screen_bottom = screen_top.saturating_add(height); |
|
| 2569 | 2572 | ||
| 2570 | 2573 | if screen_top < top { |
|
| 2571 | 2574 | self.row.saturating_sub(top) |
a.rs
+0 -0
image.png
+0 -0
a.rs
+0 -0
b.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
crates/diff/tests/golden/sticky_header_before_next_file_64x5.txt
+0 -0
src/a.rs
+0 -0
src/b.rs
+0 -0
README.md
+0 -0
src/app/a.rs
+0 -0
src/lib/b.rs
+0 -0
README.md
+0 -0
a.rs
+0 -0
b.rs
+0 -0
first.rs
+0 -0
second.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
a.rs\n",
+0 -0
new.rs\n",
+0 -0
crates/diff/tests/golden/expanded_context_focus_64x8.txt
+4 -4
| 1 | 1 | ╭─────────────────────────────────────────────────────────────╮🮘 |
|
| 2 | 2 | │ ⊙ a.rs 4 LOC +2 -2 │🮘 |
|
| 3 | 3 | ├╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴┤🮘 |
|
| 4 | - | │ 9 9 nine │🮘 |
|
| 5 | - | │ 10 10 keep │▗ |
|
| 4 | + | │ 8 8 eight │🮘 |
|
| 5 | + | │ 9 9 nine │▐ |
|
| 6 | + | │ 10 10 keep │▐ |
|
| 6 | 7 | │ 11 - before │▐ |
|
| 7 | - | │ 11 + after │▐ |
|
| 8 | - | ╰─────────────────────────────────────────────────────────────╯▐ |
|
| 8 | + | │ 11 + after │▝ |
crates/diff/tests/golden/focused_second_file_64x6.txt
+4 -4
| 1 | 1 | ╭─────────────────────────────────────────────────────────────╮🮘 |
|
| 2 | 2 | │ ⊙ b.rs 2 LOC +1 -1 │🮘 |
|
| 3 | - | ├╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴┤🮘 |
|
| 4 | - | │ 2 - before │🮘 |
|
| 5 | - | │ 2 + after │▐ |
|
| 6 | - | ╰─────────────────────────────────────────────────────────────╯▐ |
|
| 3 | + | ├─────────────────────────────────────────────────────────────┤🮘 |
|
| 4 | + | │ 1 1 keep │▗ |
|
| 5 | + | │ 2 - before │▐ |
|
| 6 | + | │ 2 + after │▝ |