diff: Avoid rendering rows for highlight requests

a7e91867ba2af4d12c76e568277ece21dca4ff1a
Computes visible highlight requests from row metadata without constructing
rendered rows first.
Alexis Sellier committed ago 1 parent 517a81c7
crates/diff/src/renderer.rs +49 -36
295 295
        let view = View::new(self.state.scroll, self.row_count(), area.rows);
296 296
        let start = view
297 297
            .offset
298 298
            .saturating_add(self.sticky_row_count(view.offset, area.rows));
299 299
        let visible = (start..start.saturating_add(take))
300 -
            .filter_map(|row| self.render_row(row, ScrollView::body_cols(area))?.diff)
300 +
            .filter_map(|row| self.diff_meta_at(row))
301 301
            .collect::<Vec<_>>();
302 302
303 303
        let mut requests = Vec::new();
304 304
        let mut sources = BTreeMap::new();
305 305
        for meta in visible {
450 450
            line: border.bottom(inner_cols),
451 451
            diff: None,
452 452
        }
453 453
    }
454 454
455 +
    /// Returns source metadata for one rendered row without building UI lines.
456 +
    fn diff_meta_at(&self, row: usize) -> Option<DiffLineMeta> {
457 +
        let location = self.file_row_location(row)?;
458 +
        let collapsed = self.state.collapsed_files.contains(&location.file.id);
459 +
        for (hunk, header_row) in location.file.hunk_header_rows(collapsed) {
460 +
            let body_start = header_row + 1;
461 +
            let body_end = body_start + hunk.lines.len();
462 +
            if location.local < body_start {
463 +
                return None;
464 +
            }
465 +
            if location.local < body_end {
466 +
                return Some(self.hunk_meta_at(location.file, hunk, location.local - body_start));
467 +
            }
468 +
        }
469 +
        None
470 +
    }
471 +
455 472
    /// Returns the file location for a global rendered row.
456 473
    fn file_row_location(&self, row: usize) -> Option<FileRowLocation<'_>> {
457 474
        let mut start = self.document.preamble.len();
458 475
        for file in &self.document.files {
459 476
            let end = start + self.file_row_count(file);
627 644
        hunk: &crate::Hunk,
628 645
        line_index: usize,
629 646
        inner: usize,
630 647
    ) -> RenderRow {
631 648
        let source_cols = inner.saturating_sub(LINE_NUMBER_WIDTH * 2 + MARKER_WIDTH);
649 +
        let (old_line, new_line) = Self::hunk_line_numbers(hunk, line_index);
650 +
        let line = &hunk.lines[line_index];
651 +
        let meta = DiffLineMeta::new(file, hunk.id, line, old_line, new_line);
652 +
653 +
        self.hunk_row(meta, line, old_line, source_cols)
654 +
    }
655 +
656 +
    /// Returns source metadata for one rendered hunk body row.
657 +
    fn hunk_meta_at(&self, file: &FileDiff, hunk: &crate::Hunk, line_index: usize) -> DiffLineMeta {
658 +
        let (old_line, new_line) = Self::hunk_line_numbers(hunk, line_index);
659 +
        DiffLineMeta::new(file, hunk.id, &hunk.lines[line_index], old_line, new_line)
660 +
    }
661 +
662 +
    /// Returns the old and new source numbers for one hunk body row.
663 +
    fn hunk_line_numbers(hunk: &crate::Hunk, line_index: usize) -> (u32, u32) {
632 664
        let mut old_line = hunk.header.old_line_no;
633 665
        let mut new_line = hunk.header.new_line_no;
634 666
        for line in &hunk.lines[..line_index] {
635 667
            old_line = old_line.saturating_add(line.kind.old_len());
636 668
            new_line = new_line.saturating_add(line.kind.new_len());
637 669
        }
638 -
639 -
        self.hunk_row(
640 -
            file,
641 -
            hunk.id,
642 -
            &hunk.lines[line_index],
643 -
            &mut old_line,
644 -
            &mut new_line,
645 -
            source_cols,
646 -
        )
670 +
        (old_line, new_line)
647 671
    }
648 672
649 673
    /// Returns one rendered hunk row.
650 674
    fn hunk_row(
651 675
        &self,
652 -
        file: &FileDiff,
653 -
        hunk: HunkId,
676 +
        meta: DiffLineMeta,
654 677
        line: &DiffLine,
655 -
        old_line: &mut u32,
656 -
        new_line: &mut u32,
678 +
        old_line: u32,
657 679
        source_cols: usize,
658 680
    ) -> RenderRow {
659 -
        let side = match line.kind {
660 -
            DiffLineKind::Deletion => DiffSide::Old,
661 -
            _ => DiffSide::New,
662 -
        };
663 -
        let source_line = match side {
664 -
            DiffSide::Old => *old_line,
665 -
            DiffSide::New => *new_line,
666 -
        };
667 -
        let meta = DiffLineMeta::new(file, hunk, line, side, source_line);
668 681
        let (cells, source, style) = match line.kind {
669 682
            DiffLineKind::Addition => (
670 683
                [
671 684
                    Self::blank_number(colors::rgb::POSITIVE),
672 685
                    Self::number_cell(
673 -
                        source_line,
686 +
                        meta.source_line,
674 687
                        colors::rgb::POSITIVE_LIGHT,
675 688
                        colors::rgb::POSITIVE,
676 689
                    ),
677 690
                    Self::marker_cell(" + ", colors::rgb::POSITIVE_LIGHT),
678 691
                ],
680 693
                Style::default().bg(colors::rgb::POSITIVE_DARK),
681 694
            ),
682 695
            DiffLineKind::Deletion => (
683 696
                [
684 697
                    Self::number_cell(
685 -
                        source_line,
698 +
                        meta.source_line,
686 699
                        colors::rgb::NEGATIVE_LIGHT,
687 700
                        colors::rgb::NEGATIVE,
688 701
                    ),
689 702
                    Self::blank_number(colors::rgb::NEGATIVE),
690 703
                    Self::marker_cell(" - ", colors::rgb::NEGATIVE_LIGHT),
692 705
                self.highlighted_or_plain(&meta, source_cols),
693 706
                Style::default().bg(colors::rgb::NEGATIVE_DARK).dim(),
694 707
            ),
695 708
            DiffLineKind::Context => (
696 709
                [
697 -
                    Self::number_cell(*old_line, colors::rgb::DIM, colors::rgb::FAINT),
698 -
                    Self::number_cell(source_line, colors::rgb::DIM, colors::rgb::FAINT),
710 +
                    Self::number_cell(old_line, colors::rgb::DIM, colors::rgb::FAINT),
711 +
                    Self::number_cell(meta.source_line, colors::rgb::DIM, colors::rgb::FAINT),
699 712
                    Self::marker_cell("   ", Color::Unset),
700 713
                ],
701 714
                self.highlighted_or_plain(&meta, source_cols),
702 715
                Style::default(),
703 716
            ),
710 723
                Line::new(NO_NEWLINE_MARKER).style(Style::default().fg(colors::rgb::DIM)),
711 724
                Style::default(),
712 725
            ),
713 726
        };
714 727
        let rendered = self.hunk_line(cells, source, source_cols, style);
715 -
        *old_line = old_line.saturating_add(line.kind.old_len());
716 -
        *new_line = new_line.saturating_add(line.kind.new_len());
717 728
        RenderRow {
718 729
            line: rendered,
719 730
            diff: Some(meta),
720 731
        }
721 732
    }
1065 1076
    }
1066 1077
}
1067 1078
1068 1079
impl DiffLineMeta {
1069 1080
    /// Creates metadata for a source row.
1070 -
    fn new(
1071 -
        file: &FileDiff,
1072 -
        hunk: HunkId,
1073 -
        line: &DiffLine,
1074 -
        side: DiffSide,
1075 -
        source_line: u32,
1076 -
    ) -> Self {
1081 +
    fn new(file: &FileDiff, hunk: HunkId, line: &DiffLine, old_line: u32, new_line: u32) -> Self {
1082 +
        let side = match line.kind {
1083 +
            DiffLineKind::Deletion => DiffSide::Old,
1084 +
            _ => DiffSide::New,
1085 +
        };
1086 +
        let source_line = match side {
1087 +
            DiffSide::Old => old_line,
1088 +
            DiffSide::New => new_line,
1089 +
        };
1077 1090
        Self {
1078 1091
            file: file.id,
1079 1092
            hunk,
1080 1093
            row: line.id,
1081 1094
            side,
a.rs +0 -0
a.rs +0 -0
b.rs +0 -0
a.rs +0 -0