diff: Align oversized hunks at top

3dee01edceda3ef023cf65f1fef782f6321836eb
Center a selected hunk only when its full body fits below the file
header. Otherwise, place its first line at the top of the available diff
body.

Assisted-by: Codex:gpt-5.6
Alexis Sellier committed ago 1 parent 426315a4
crates/diff/src/viewer.rs +8 -15
2342 2342
    fn scroll_offset(self, current: usize, visible_rows: usize, placement: HunkScroll) -> usize {
2343 2343
        if visible_rows == 0 {
2344 2344
            return current;
2345 2345
        }
2346 2346
        let height = self.height.max(1);
2347 +
        let top = self.row.saturating_sub(self.sticky_rows(self.row));
2348 +
        let leading = self.row.saturating_sub(top);
2349 +
        let available = visible_rows.saturating_sub(leading);
2350 +
        if height > available {
2351 +
            return top;
2352 +
        }
2347 2353
2348 2354
        match placement {
2349 2355
            HunkScroll::Center => {
2350 -
                let screen_row = visible_rows.saturating_sub(height) / 2;
2351 -
                let centered = self.row.saturating_sub(screen_row);
2356 +
                let screen_row = leading + available.saturating_sub(height) / 2;
2352 2357
2353 -
                centered.saturating_sub(self.sticky_rows(centered))
2354 -
            }
2355 -
            HunkScroll::Preserve if height >= visible_rows => {
2356 -
                let top = self.row.saturating_sub(self.sticky_rows(current));
2357 -
                let end = self.row.saturating_add(height);
2358 -
2359 -
                if current < top {
2360 -
                    top
2361 -
                } else if current >= end {
2362 -
                    end.saturating_sub(visible_rows)
2363 -
                } else {
2364 -
                    current
2365 -
                }
2358 +
                self.row.saturating_sub(screen_row)
2366 2359
            }
2367 2360
            HunkScroll::Preserve => {
2368 2361
                let top = self.sticky_rows(current).saturating_add(1);
2369 2362
                let bottom = visible_rows.saturating_sub(1);
2370 2363
                let screen_top = self.row.saturating_sub(current);
a.rs +0 -0
a.rs +0 -0
b.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
assets/css/style.css +0 -0
assets/css/style.css +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
a\n\ +0 -0