diff: Fix sticky file headers

e368e37139773d37c3026388c97564a322b56eac
Alexis Sellier committed ago 1 parent d9444493
crates/diff/src/viewer.rs +32 -8
302 302
    end: usize,
303 303
    /// Row offset inside the file card.
304 304
    local: usize,
305 305
}
306 306
307 +
/// Sticky file-header rows visible during a render pass.
308 +
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
309 +
struct StickyHeaderRows {
310 +
    /// Global source row where the full file header starts.
311 +
    start: usize,
312 +
    /// Number of header rows still pinned in the viewport.
313 +
    rows: usize,
314 +
}
315 +
307 316
/// Metadata for one source row.
308 317
#[derive(Clone, Debug, Eq, PartialEq)]
309 318
struct DiffLineMeta {
310 319
    /// File containing the row.
311 320
    file: FileId,
1238 1247
    }
1239 1248
1240 1249
    /// Returns sticky file-header rows for the current offset.
1241 1250
    fn sticky_rows(&self, scroll: usize, visible_rows: usize, cols: usize) -> Vec<Line> {
1242 1251
        // Build the fixed sticky header strip directly from current scroll context.
1243 -
        self.sticky_start(scroll, visible_rows)
1244 -
            .map(|start| {
1245 -
                (0..STICKY_HEADER_ROWS)
1252 +
        self.sticky_header_rows(scroll, visible_rows)
1253 +
            .map(|sticky| {
1254 +
                (STICKY_HEADER_ROWS - sticky.rows..STICKY_HEADER_ROWS)
1246 1255
                    .map(|index| {
1247 1256
                        if index == STICKY_HEADER_ROWS - 1 {
1248 1257
                            FILE_BORDER.divider(cols.saturating_sub(2), STICKY_DIVIDER)
1249 1258
                        } else {
1250 -
                            self.render_row(start + index, cols)
1259 +
                            self.render_row(sticky.start + index, cols)
1251 1260
                                .map(|row| row.line)
1252 1261
                                .unwrap_or_default()
1253 1262
                        }
1254 1263
                    })
1255 1264
                    .collect()
1257 1266
            .unwrap_or_default()
1258 1267
    }
1259 1268
1260 1269
    /// Returns the number of sticky rows for the current offset.
1261 1270
    fn sticky_row_count(&self, scroll: usize, visible_rows: usize) -> usize {
1262 -
        self.sticky_start(scroll, visible_rows)
1263 -
            .map_or(0, |_| STICKY_HEADER_ROWS.min(visible_rows))
1271 +
        self.sticky_header_rows(scroll, visible_rows)
1272 +
            .map_or(0, |sticky| sticky.rows)
1264 1273
    }
1265 1274
1266 1275
    /// Returns the source row for the sticky header at the current offset.
1276 +
    #[cfg(test)]
1267 1277
    fn sticky_start(&self, scroll: usize, visible_rows: usize) -> Option<usize> {
1278 +
        self.sticky_header_rows(scroll, visible_rows)
1279 +
            .map(|sticky| sticky.start)
1280 +
    }
1281 +
1282 +
    /// Returns the visible sticky header rows at the current offset.
1283 +
    fn sticky_header_rows(&self, scroll: usize, visible_rows: usize) -> Option<StickyHeaderRows> {
1268 1284
        let scroll = scroll.min(self.index.rows.saturating_sub(visible_rows));
1269 1285
        if scroll == 0 {
1270 1286
            return None;
1271 1287
        }
1272 1288
        let location = self.file_row_location(scroll)?;
1273 1289
        if scroll <= location.start {
1274 1290
            return None;
1275 1291
        }
1276 -
        if location.end <= scroll.saturating_add(STICKY_HEADER_ROWS.min(visible_rows)) {
1292 +
        let file_bottom = location.end.saturating_sub(location.start + 1);
1293 +
        let rows = file_bottom
1294 +
            .saturating_sub(location.local)
1295 +
            .min(STICKY_HEADER_ROWS)
1296 +
            .min(visible_rows);
1297 +
        if rows == 0 {
1277 1298
            return None;
1278 1299
        }
1279 -
        Some(location.start)
1300 +
        Some(StickyHeaderRows {
1301 +
            start: location.start,
1302 +
            rows,
1303 +
        })
1280 1304
    }
1281 1305
1282 1306
    /// Focuses a target relative to the current focus.
1283 1307
    fn focus(&mut self, visible_rows: usize, delta: isize, filter: FocusFilter) {
1284 1308
        let targets = self.focus_targets(filter);
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
src/a.rs +0 -0
src/b.rs +0 -0
README.md +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.rs +28 -0
113 113
        64,
114 114
        5,
115 115
    );
116 116
}
117 117
118 +
#[test]
119 +
fn sticky_header_full_corner_boundary_matches_golden_frame() {
120 +
    let mut viewer = two_file_viewer();
121 +
    viewer.apply(DiffAction::Scroll(Scroll::Lines(5)), Rect::new(0, 0, 64, 5));
122 +
123 +
    assert_golden(
124 +
        "sticky_header_full_corner_boundary_64x5.txt",
125 +
        include_str!("golden/sticky_header_full_corner_boundary_64x5.txt"),
126 +
        viewer,
127 +
        64,
128 +
        5,
129 +
    );
130 +
}
131 +
132 +
#[test]
133 +
fn sticky_header_file_end_boundary_matches_golden_frame() {
134 +
    let mut viewer = two_file_viewer();
135 +
    viewer.apply(DiffAction::Scroll(Scroll::Lines(6)), Rect::new(0, 0, 64, 5));
136 +
137 +
    assert_golden(
138 +
        "sticky_header_file_end_boundary_64x5.txt",
139 +
        include_str!("golden/sticky_header_file_end_boundary_64x5.txt"),
140 +
        viewer,
141 +
        64,
142 +
        5,
143 +
    );
144 +
}
145 +
118 146
#[test]
119 147
fn bottom_of_file_matches_golden_frame() {
120 148
    let mut viewer = sample_viewer();
121 149
    viewer.apply(DiffAction::Scroll(Scroll::Bottom), Rect::new(0, 0, 64, 5));
122 150
crates/diff/tests/golden/sticky_header_file_end_boundary_64x5.txt added +5 -0
1 +
โ”‚ โŠ™ a.rs                                                +2 -1 โ”‚๐Ÿฎ˜
2 +
โ”œโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ”ค๐Ÿฎ˜
3 +
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏโ–“
4 +
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ๐Ÿฎ˜
5 +
โ”‚ โŠ™ b.rs                                                +1 -1 โ”‚๐Ÿฎ˜
crates/diff/tests/golden/sticky_header_full_corner_boundary_64x5.txt added +5 -0
1 +
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ๐Ÿฎ˜
2 +
โ”‚ โŠ™ a.rs                                                +2 -1 โ”‚โ–“
3 +
โ”œโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ•ดโ”ค๐Ÿฎ˜
4 +
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ๐Ÿฎ˜
5 +
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ๐Ÿฎ˜