Add an opt-in detailed text diff

This commit is contained in:
Jan Petykiewicz 2026-11-18 23:20:00 -08:00
commit f3275fc161
3 changed files with 266 additions and 45 deletions

View file

@ -5,7 +5,7 @@ use flate2::read::MultiGzDecoder;
use pheon::review::report::{ReportInputs, ReportOptions, failure_report};
use pheon::review::{
Comparison, Diagnostic, Document, Layer, Outcome, RevisionPair, Side, TextComparison,
dump_records,
TextReportOptions, dump_records,
names::{parse_layer, parse_layer_name, resolve_layer_name, write_layer_names},
selection::{DisplayFilter, Scope},
};
@ -22,7 +22,7 @@ use termcolor::ColorChoice;
const HELP: &str = "Usage:
pheon dump FILE [--records] [--color auto|always|never]
pheon diff OLD NEW [--top NAME] [--cell NAME] [--layer L/T|NAME]\n [--show-cell NAME] [--show-layer L/T|NAME] [--hierarchy ascii|mermaid|none|graph]\n [--max-changes N] [--depth N] [--format text|json]
pheon diff OLD NEW [--top NAME] [--cell NAME] [--layer L/T|NAME]\n [--show-cell NAME] [--show-layer L/T|NAME] [--hierarchy ascii|mermaid|none|graph]\n [--details] [--max-changes N] [--depth N] [--format text|json]
[--color auto|always|never]
pheon hierarchy FILE [--format ascii|mermaid] [--depth N] [--top NAME]\n pheon git-diff [OPTIONS] -- PATH OLD OLD_OID OLD_MODE NEW NEW_OID NEW_MODE
pheon name-layers INPUT OUTPUT --cell NAME --carrier L/T --name L:T:NAME ...
@ -346,6 +346,7 @@ fn run(args: &[OsString]) -> Result<u8, String> {
"ascii".to_string()
};
let mut max_changes = 100;
let mut details = false;
let mut explicit_max = None;
let mut output_format = "text".to_string();
let mut depth = 8;
@ -383,6 +384,7 @@ fn run(args: &[OsString]) -> Result<u8, String> {
(selectors.filter.layer, selectors.show_layer_name) =
layer_argument(value(args, &mut i)?);
}
Some("--details") if compare => details = true,
Some("--hierarchy") if compare => format = value(args, &mut i)?,
Some("--format") if command == "hierarchy" => format = value(args, &mut i)?,
Some("--format") if command == "diff" => output_format = value(args, &mut i)?,
@ -482,20 +484,19 @@ fn run(args: &[OsString]) -> Result<u8, String> {
}
}
let show_library_presence = !old_missing && !new_missing;
if format == "ascii" {
output.push_str(&diff.render_text_with_ascii_hierarchy(
max_changes,
&selectors.filter,
show_library_presence,
depth,
));
} else {
output.push_str(&diff.render_text(max_changes, &selectors.filter, show_library_presence));
if format == "mermaid" {
output.push_str("Hierarchy\n```mermaid\n");
output.push_str(&diff.hierarchy(true, depth));
output.push_str("```\n");
}
output.push_str(&diff.render_text_with_options(
max_changes,
&selectors.filter,
show_library_presence,
TextReportOptions {
details,
ascii_depth: (format == "ascii").then_some(depth),
},
));
if format == "mermaid" {
output.push_str("Hierarchy\n```mermaid\n");
output.push_str(&diff.hierarchy(true, depth));
output.push_str("```\n");
}
color::print(&output, color::Report::Diff, color_choice).map_err(|error| error.to_string())?;
// Git's external driver expects successful rendering for either equality

View file

@ -1202,6 +1202,15 @@ pub struct Comparison {
pub diagnostics: Vec<Diagnostic>,
}
/// Additional presentation options for a text comparison.
#[derive(Clone, Copy, Debug, Default)]
pub struct TextReportOptions {
/// Print individual object evidence, including both sides of replacements.
pub details: bool,
/// Append the ASCII hierarchy at this depth when provided.
pub ascii_depth: Option<usize>,
}
/// A comparison for text and hierarchy reports.
#[derive(Debug)]
pub struct TextComparison {
@ -1229,6 +1238,17 @@ impl TextComparison {
.render_text(max_objects, show, show_library_presence)
}
pub fn render_text_with_options(
&self,
max_objects: usize,
show: &DisplayFilter,
show_library_presence: bool,
options: TextReportOptions,
) -> String {
self.comparison
.render_text_with_options(max_objects, show, show_library_presence, options)
}
/// Render the cell changes and their ASCII hierarchy together.
pub fn render_text_with_ascii_hierarchy(
&self,
@ -1457,7 +1477,12 @@ impl Comparison {
show: &DisplayFilter,
show_library_presence: bool,
) -> String {
self.render_text_report(max_objects, show, show_library_presence, None)
self.render_text_with_options(
max_objects,
show,
show_library_presence,
TextReportOptions::default(),
)
}
/// Render the cell changes and omit simple hierarchy edges already shown there.
@ -1468,16 +1493,29 @@ impl Comparison {
show_library_presence: bool,
max_depth: usize,
) -> String {
self.render_text_report(max_objects, show, show_library_presence, Some(max_depth))
self.render_text_with_options(
max_objects,
show,
show_library_presence,
TextReportOptions {
ascii_depth: Some(max_depth),
..TextReportOptions::default()
},
)
}
fn render_text_report(
/// Render the selected changes with optional object details and ASCII hierarchy.
pub fn render_text_with_options(
&self,
max_objects: usize,
show: &DisplayFilter,
show_library_presence: bool,
ascii_depth: Option<usize>,
options: TextReportOptions,
) -> String {
let TextReportOptions {
details,
ascii_depth,
} = options;
let outcome = self.outcome();
let mut out = match outcome {
Outcome::Equal => "Equal",
@ -1594,27 +1632,36 @@ impl Comparison {
}
paired_old.insert(replacement.removed);
paired_new.insert(replacement.added);
if max_objects - shown >= 2 {
if max_objects - shown < 2 {
continue;
}
if !details && a.layer == b.layer && is_polygon(a.kind) {
polygons.replacement(a, b);
} else {
out.push_str(" ~ ");
let row_start = out.len();
if a.layer != b.layer {
out.push_str(" ~ ");
changes::write_layer_transition(&mut out, a, b, Some(&mut layers));
out.push('\n');
} else if is_polygon(a.kind) {
polygons.replacement(a, b);
} else {
out.push_str(" ~ ");
let row_start = out.len();
layers.record(a.layer, row_start);
writeln!(out, "{}", changes::replacement_summary(a, b)).unwrap();
if let Some(target) = &a.target {
shown_refs
.entry(target)
.or_default()
.replacement(a, b, row_start);
if details {
out.push_str(&changes::replacement_heading(a, b));
} else {
out.push_str(&changes::replacement_summary(a, b));
}
}
shown += 2;
out.push('\n');
if details {
changes::write_replacement_objects(&mut out, a, b);
}
if let Some(target) = &a.target {
shown_refs
.entry(target)
.or_default()
.replacement(a, b, row_start);
}
}
shown += 2;
}
let mut removed_refs = BTreeMap::new();
let mut added_refs = BTreeMap::new();
@ -1630,9 +1677,9 @@ impl Comparison {
}
selected += 1;
if !paired.contains(&index) && shown < max_objects {
if is_polygon(object.kind) {
if !details && is_polygon(object.kind) {
polygons.unmatched(object, removed);
} else if let Some(target) = object.target.as_deref() {
} else if !details && let Some(target) = object.target.as_deref() {
let (_, counts) = references
.entry(target)
.or_insert_with(|| (object, changes::ReferenceCounts::default()));
@ -1655,8 +1702,19 @@ impl Comparison {
if count > 1 {
write!(out, "{count}× ").unwrap();
}
layers.record(object.layer, out.len());
changes::write_unmatched(&mut out, object);
let row_start = out.len();
layers.record(object.layer, row_start);
if details {
writeln!(out, "{object}").unwrap();
if let Some(target) = object.target.as_deref() {
shown_refs
.entry(target)
.or_default()
.individual(object, removed, count, row_start);
}
} else {
changes::write_unmatched(&mut out, object);
}
}
}
let no_refs = changes::ReferenceCounts::default();

View file

@ -426,6 +426,23 @@ impl ShownReferences {
self.row_start = row_start;
}
pub fn individual(
&mut self,
object: &ObjectChange,
removed: bool,
count: usize,
row_start: usize,
) {
let placements = if removed {
&mut self.removed
} else {
&mut self.added
};
*placements += reference_placements(object) * count as u128;
self.shown += count;
self.row_start = row_start;
}
pub fn count_change(&mut self) {
self.total += 1;
}
@ -555,6 +572,28 @@ fn polygon_replacement(old: &ObjectChange, new: &ObjectChange) -> String {
out
}
pub(super) fn replacement_heading(old: &ObjectChange, new: &ObjectChange) -> String {
let rectangular = is_polygon(old.kind)
&& rectangle_size(object_xy(old)).is_some()
&& rectangle_size(object_xy(new)).is_some();
let mut heading = object_heading(old, object_name(old.kind, rectangular));
if let Some(target) = &old.target {
write!(heading, " {target:?}").unwrap();
}
heading
}
pub(super) fn write_replacement_objects(out: &mut String, old: &ObjectChange, new: &ObjectChange) {
for (sign, object) in [('-', old), ('+', new)] {
let description = object.to_string();
let mut lines = description.split('\n');
writeln!(out, " {sign} {}", lines.next().unwrap()).unwrap();
for line in lines {
writeln!(out, " {line}").unwrap();
}
}
}
pub(super) fn replacement_summary(old: &ObjectChange, new: &ObjectChange) -> String {
if old.layer != new.layer && old.layer.is_some() && new.layer.is_some() {
let mut out = String::new();
@ -565,11 +604,7 @@ pub(super) fn replacement_summary(old: &ObjectChange, new: &ObjectChange) -> Str
let mut out = if polygon {
polygon_replacement(old, new)
} else {
let mut heading = object_heading(old, object_name(old.kind, false));
if let Some(target) = &old.target {
write!(heading, " {target:?}").unwrap();
}
heading
replacement_heading(old, new)
};
let mut separator = if polygon { "\n " } else { " " };