//! Diff decorations derived from review and comment state. //! //! The diff viewer is review-agnostic; this decorator feeds it per-hunk //! markers. A [`Decorations`] value is an immutable snapshot of the review //! state, rebuilt and reinstalled by the session whenever acceptance or //! comments change, so the viewer never observes state mutating underneath //! it. Accepted hunks are dimmed and marked by a green gutter; commented //! hunks show a comment marker and count; hunks that need review after a //! rebase show a rebase marker. use std::collections::{BTreeMap, BTreeSet}; use rustik_diff::{ DiffDecorator, DiffPalette, FileDecoration, FileId, FileStats, HunkDecoration, HunkId, ReviewProgress, }; use rustik_tui as tui; use tui::{Label, Line, Paint, Style, colors}; use crate::comments::{COMMENT_MARK, Comments}; use crate::review::Review; /// Check mark drawn beside accepted hunks and fully accepted files: ✔. const ACCEPTED_MARK: char = '✔'; /// Vertical rule drawn in the gutter beside accepted hunks: ▏. const ACCEPTED_GUTTER: char = '▏'; /// Immutable decoration snapshot of one commit's review state. #[derive(Clone, Debug)] pub struct Decorations { /// Hunks accepted at snapshot time. accepted: BTreeSet, /// Per-hunk comment counts at snapshot time. comments: BTreeMap, /// Original hunk ids for each file in the commit. file_hunks: BTreeMap>, /// Original addition and deletion counts for each hunk. hunk_stats: BTreeMap, /// Diff palette the markers are drawn with. palette: DiffPalette, } impl Decorations { /// Captures the current review and comment state for `file_hunks`. pub fn snapshot( review: &Review, comments: &Comments, file_hunks: &BTreeMap>, hunk_stats: &BTreeMap, palette: DiffPalette, ) -> Self { Self { accepted: review.accepted().clone(), comments: comments.counts(), file_hunks: file_hunks.clone(), hunk_stats: hunk_stats.clone(), palette, } } /// Returns the combined diffstat of hunks that still need review. fn remaining_stats(&self, hunks: &[HunkId]) -> FileStats { hunks .iter() .filter(|hunk| !self.accepted.contains(hunk)) .filter_map(|hunk| self.hunk_stats.get(hunk)) .fold(FileStats::default(), |mut total, stats| { total.additions += stats.additions; total.deletions += stats.deletions; total }) } } impl DiffDecorator for Decorations { fn file(&self, file: FileId) -> FileDecoration { let hunks = self.file_hunks.get(&file).map_or(&[][..], Vec::as_slice); let progress = (!hunks.is_empty()).then(|| ReviewProgress { accepted: hunks .iter() .filter(|hunk| self.accepted.contains(hunk)) .filter_map(|hunk| self.hunk_stats.get(hunk)) .map(|stats| (stats.additions + stats.deletions).max(1)) .sum(), total: hunks .iter() .filter_map(|hunk| self.hunk_stats.get(hunk)) .map(|stats| (stats.additions + stats.deletions).max(1)) .sum(), }); let comment_count = hunks .iter() .filter_map(|hunk| self.comments.get(hunk)) .sum::(); let mut decoration = FileDecoration { progress, stats: Some(self.remaining_stats(hunks)), ..FileDecoration::default() }; if progress.is_some_and(ReviewProgress::is_complete) { decoration.status = Some(Paint::new(ACCEPTED_MARK).fg(self.palette.positive_fg)); } else if comment_count > 0 { decoration.annotations = vec![Line::new( Label::new(&format!("{comment_count} {COMMENT_MARK}")).fg(colors::rgb::DIM), )]; } decoration } fn hunk(&self, _file: FileId, hunk: HunkId) -> HunkDecoration { let accepted = self.accepted.contains(&hunk); let comments = self.comments.get(&hunk).copied().unwrap_or_default(); if !accepted && comments == 0 { return HunkDecoration::default(); } let mut corner = Line::blank(); if comments > 0 { corner = corner.item(Label::new(&format!("{comments} {COMMENT_MARK}")).fg(colors::rgb::DIM)); } HunkDecoration { gutter: accepted.then(|| Paint::new(ACCEPTED_GUTTER).fg(self.palette.positive_fg)), corner, style: accepted.then(|| Style::default().dim()), } } } #[cfg(test)] mod tests { use super::*; #[test] fn file_diffstat_excludes_accepted_hunks() { let first = HunkId::new(0); let second = HunkId::new(1); let decorations = Decorations { accepted: BTreeSet::from([first]), comments: BTreeMap::new(), file_hunks: BTreeMap::from([(FileId::new(0), vec![first, second])]), hunk_stats: BTreeMap::from([ ( first, FileStats { additions: 2, deletions: 4, }, ), ( second, FileStats { additions: 3, deletions: 1, }, ), ]), palette: DiffPalette::default(), }; assert_eq!( decorations.remaining_stats(&[first, second]), FileStats { additions: 3, deletions: 1, } ); } #[test] fn zero_line_hunk_has_one_review_unit() { let hunk = HunkId::new(0); let file = FileId::new(0); let decorations = Decorations { accepted: BTreeSet::new(), comments: BTreeMap::new(), file_hunks: BTreeMap::from([(file, vec![hunk])]), hunk_stats: BTreeMap::from([(hunk, FileStats::default())]), palette: DiffPalette::default(), }; assert_eq!( decorations.file(file).progress, Some(ReviewProgress { accepted: 0, total: 1, }) ); let accepted = Decorations { accepted: BTreeSet::from([hunk]), ..decorations }; assert!( accepted .file(file) .progress .is_some_and(ReviewProgress::is_complete) ); } }