diff: Keep hunk navigation below file
db2768e91fa6e4ef3c527b0353de0b0a7887e326
Anchor forward hunk navigation to the focused file when restored hunks become visible, preventing the cursor from jumping to commit metadata. Assisted-by: Codex:gpt-5.6-sol
1 parent
6a726cb2
crates/diff/src/viewer.rs
+11 -1
| 2084 | 2084 | targets |
|
| 2085 | 2085 | .iter() |
|
| 2086 | 2086 | .position(|target| target.focus == DiffFocus::File(file)) |
|
| 2087 | 2087 | }) |
|
| 2088 | 2088 | }); |
|
| 2089 | + | let focused_file_start = self.focused_file().and_then(|file| { |
|
| 2090 | + | self.index |
|
| 2091 | + | .files |
|
| 2092 | + | .iter() |
|
| 2093 | + | .find(|indexed| self.document.files[indexed.file_index].id == file) |
|
| 2094 | + | .map(|indexed| indexed.start) |
|
| 2095 | + | }); |
|
| 2089 | 2096 | let next = match (current, filter, delta.is_negative()) { |
|
| 2090 | 2097 | (Some(index), _, true) => index.checked_sub(1), |
|
| 2091 | 2098 | (Some(index), _, false) => index.checked_add(1).filter(|index| *index < targets.len()), |
|
| 2092 | 2099 | (None, FocusFilter::HunkNavigation, true) => targets |
|
| 2093 | 2100 | .partition_point(|target| target.row <= self.state.scroll.offset) |
|
| 2094 | 2101 | .checked_sub(1), |
|
| 2095 | 2102 | (None, FocusFilter::HunkNavigation, false) => { |
|
| 2096 | - | let index = targets.partition_point(|target| target.row < self.state.scroll.offset); |
|
| 2103 | + | let offset = focused_file_start.map_or(self.state.scroll.offset, |start| { |
|
| 2104 | + | start.max(self.state.scroll.offset) |
|
| 2105 | + | }); |
|
| 2106 | + | let index = targets.partition_point(|target| target.row < offset); |
|
| 2097 | 2107 | (index < targets.len()).then_some(index) |
|
| 2098 | 2108 | } |
|
| 2099 | 2109 | (None, FocusFilter::File, true) => targets.len().checked_sub(1), |
|
| 2100 | 2110 | (None, FocusFilter::File, false) => Some(0), |
|
| 2101 | 2111 | }; |
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