Fold unbranched paths in ASCII diffs
This commit is contained in:
parent
4789638e22
commit
4ae689cd1c
4 changed files with 208 additions and 33 deletions
|
|
@ -137,7 +137,10 @@ reference changes whose placement count stays the same. When the cell list
|
|||
shows all edits to an unchanged target on a single branch, ASCII can omit
|
||||
that terminal edge. The hierarchy retains ancestor paths, shared branches,
|
||||
and edges with edits hidden by display filters or limits. ASCII appears when
|
||||
there are relationships to show. Use `--depth` with text output.
|
||||
there are relationships to show. ASCII diffs fold chains of three or more cells
|
||||
along unchanged single-placement edges, for example `CHIP → block → * leaf`.
|
||||
Branch points and changed edges keep their own rows. `--depth` counts the
|
||||
original edges.
|
||||
For the complete graph within the comparison scope, use
|
||||
`--format json --hierarchy graph` without `--depth`. Display filters leave the
|
||||
graph intact.
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@ pub(super) fn print(text: &str, report: Report, choice: ColorChoice) -> io::Resu
|
|||
}
|
||||
|
||||
let mut in_cells = false;
|
||||
let mut in_hierarchy = false;
|
||||
let mut in_mermaid = false;
|
||||
for line in text.split_inclusive('\n') {
|
||||
let body = line.strip_suffix('\n').unwrap_or(line);
|
||||
|
|
@ -48,10 +49,14 @@ pub(super) fn print(text: &str, report: Report, choice: ColorChoice) -> io::Resu
|
|||
_ => {
|
||||
match body {
|
||||
"Cells:" => in_cells = true,
|
||||
"Hierarchy" | "Diagnostics" => in_cells = false,
|
||||
"Hierarchy" => {
|
||||
in_cells = false;
|
||||
in_hierarchy = true;
|
||||
}
|
||||
"Diagnostics" => in_cells = false,
|
||||
_ => (),
|
||||
}
|
||||
diff_line(&mut out, body, in_cells)?;
|
||||
diff_line(&mut out, body, in_cells, in_hierarchy)?;
|
||||
}
|
||||
},
|
||||
Report::Dump => dump_line(&mut out, body)?,
|
||||
|
|
@ -75,7 +80,40 @@ fn paint(
|
|||
result.and(out.reset())
|
||||
}
|
||||
|
||||
fn diff_line(out: &mut impl WriteColor, line: &str, in_cells: bool) -> io::Result<()> {
|
||||
fn change_color(marker: &str) -> Option<Color> {
|
||||
match marker {
|
||||
"+" => Some(Color::Green),
|
||||
"-" => Some(Color::Red),
|
||||
"*" | "~" => Some(Color::Yellow),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn folded_endpoint(line: &str) -> Option<(&str, &str)> {
|
||||
let mut chars = line.char_indices();
|
||||
let mut quoted = false;
|
||||
let mut endpoint = None;
|
||||
while let Some((index, ch)) = chars.next() {
|
||||
match ch {
|
||||
'"' => quoted = !quoted,
|
||||
'\\' if quoted => {
|
||||
chars.next();
|
||||
}
|
||||
'→' if !quoted && line[index..].starts_with("→ ") => {
|
||||
endpoint = Some(line.split_at(index + "→ ".len()));
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
endpoint
|
||||
}
|
||||
|
||||
fn diff_line(
|
||||
out: &mut impl WriteColor,
|
||||
line: &str,
|
||||
in_cells: bool,
|
||||
in_hierarchy: bool,
|
||||
) -> io::Result<()> {
|
||||
let content = line.trim_start_matches(' ');
|
||||
let indent = &line[..line.len() - content.len()];
|
||||
let first = content.split_once(' ').map_or(content, |(word, _)| word);
|
||||
|
|
@ -93,16 +131,19 @@ fn diff_line(out: &mut impl WriteColor, line: &str, in_cells: bool) -> io::Resul
|
|||
return paint(out, line, color, true);
|
||||
}
|
||||
}
|
||||
let color = match first {
|
||||
"+" => Some(Color::Green),
|
||||
"-" => Some(Color::Red),
|
||||
"*" | "~" => Some(Color::Yellow),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(color) = color {
|
||||
if let Some(color) = change_color(first) {
|
||||
out.write_all(indent.as_bytes())?;
|
||||
return paint(out, content, color, false);
|
||||
}
|
||||
if in_hierarchy
|
||||
&& line.contains('→')
|
||||
&& let Some((path, cell)) = folded_endpoint(line)
|
||||
&& let Some((marker, _)) = cell.split_once(' ')
|
||||
&& let Some(color) = change_color(marker)
|
||||
{
|
||||
out.write_all(path.as_bytes())?;
|
||||
return paint(out, cell, color, false);
|
||||
}
|
||||
if in_cells && indent == " " {
|
||||
if (layer_token(content).is_some() || content.starts_with("unknown layer: "))
|
||||
&& let Some((layer, counts)) = content.rsplit_once(": ")
|
||||
|
|
|
|||
118
src/review.rs
118
src/review.rs
|
|
@ -1890,27 +1890,28 @@ fn render_hierarchy(
|
|||
};
|
||||
let mut children = BTreeMap::<String, Vec<String>>::new();
|
||||
let mut targets = BTreeSet::new();
|
||||
let fold_paths = !mermaid && changes.is_some();
|
||||
let mut shared = BTreeSet::new();
|
||||
for (parent, target) in &edges {
|
||||
if visible.contains(parent) && visible.contains(target) {
|
||||
children
|
||||
.entry(parent.clone())
|
||||
.or_default()
|
||||
.push(target.clone());
|
||||
targets.insert(target.clone());
|
||||
if !targets.insert(target.clone()) && fold_paths {
|
||||
shared.insert(target.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(covered) = covered_edges {
|
||||
if !covered.is_empty() {
|
||||
let mut incoming = BTreeMap::new();
|
||||
for target in children.values().flatten() {
|
||||
*incoming.entry(target).or_insert(0_usize) += 1;
|
||||
}
|
||||
// Keep changed or shared descendants, branches, and target status notes.
|
||||
let redundant: BTreeSet<_> = covered
|
||||
.iter()
|
||||
.map(|(_, target)| target)
|
||||
.filter(|&target| {
|
||||
incoming.get(target) == Some(&1)
|
||||
targets.contains(target)
|
||||
&& !shared.contains(target)
|
||||
&& !children.contains_key(target)
|
||||
&& !affected.contains(target)
|
||||
&& old.cells.contains(target)
|
||||
|
|
@ -1938,6 +1939,12 @@ fn render_hierarchy(
|
|||
}
|
||||
}
|
||||
let mut included = BTreeSet::new();
|
||||
struct Row {
|
||||
name: String,
|
||||
depth: usize,
|
||||
parent: Option<String>,
|
||||
note: &'static str,
|
||||
}
|
||||
let mut rows = Vec::new();
|
||||
let mut stack: Vec<_> = roots
|
||||
.iter()
|
||||
|
|
@ -1956,7 +1963,12 @@ fn render_hierarchy(
|
|||
} else {
|
||||
""
|
||||
};
|
||||
rows.push((name.clone(), depth, parent, note));
|
||||
rows.push(Row {
|
||||
name: name.clone(),
|
||||
depth,
|
||||
parent,
|
||||
note,
|
||||
});
|
||||
if !seen && depth < max_depth {
|
||||
for child in descendants.iter().rev() {
|
||||
stack.push((child.clone(), depth + 1, Some(name.clone())));
|
||||
|
|
@ -1964,7 +1976,12 @@ fn render_hierarchy(
|
|||
}
|
||||
}
|
||||
for name in visible.difference(&reachable) {
|
||||
rows.push((name.clone(), 0, None, " [children omitted]"));
|
||||
rows.push(Row {
|
||||
name: name.clone(),
|
||||
depth: 0,
|
||||
parent: None,
|
||||
note: " [children omitted]",
|
||||
});
|
||||
}
|
||||
let status = |name: &String| {
|
||||
if !old.cells.contains(name) && !new.cells.contains(name) {
|
||||
|
|
@ -2004,7 +2021,7 @@ fn render_hierarchy(
|
|||
if mermaid {
|
||||
let ids: BTreeMap<_, _> = rows
|
||||
.iter()
|
||||
.map(|r| &r.0)
|
||||
.map(|row| &row.name)
|
||||
.collect::<BTreeSet<_>>()
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
|
|
@ -2034,25 +2051,84 @@ fn render_hierarchy(
|
|||
}
|
||||
out
|
||||
} else {
|
||||
let single_placement = |parent: &String, target: &String| {
|
||||
let key = (parent.clone(), target.clone());
|
||||
old_edges.get(&key) == Some(&1)
|
||||
&& new_edges.get(&key) == Some(&1)
|
||||
&& !changed_refs.contains(&key)
|
||||
};
|
||||
let unbranched = |name: &String| {
|
||||
!shared.contains(name)
|
||||
&& children
|
||||
.get(name)
|
||||
.is_none_or(|descendants| descendants.len() == 1)
|
||||
};
|
||||
let can_join = |parent: &Row, child: &Row| {
|
||||
child.parent.as_ref() == Some(&parent.name)
|
||||
&& parent.note.is_empty()
|
||||
&& !direct.contains(&parent.name)
|
||||
&& affected.contains(&parent.name)
|
||||
&& unbranched(&child.name)
|
||||
&& (old.cells.contains(&child.name) || new.cells.contains(&child.name))
|
||||
&& single_placement(&parent.name, &child.name)
|
||||
};
|
||||
let mut out = "Hierarchy\n".to_string();
|
||||
if changes.is_none() {
|
||||
new.scope.write_restrictions(&mut out);
|
||||
}
|
||||
for (name, depth, parent, note) in rows {
|
||||
out.push_str(&" ".repeat(depth));
|
||||
if direct.contains(&name) {
|
||||
let marker = cell_marker(old.cells.contains(&name), new.cells.contains(&name));
|
||||
write!(out, "{marker} ").unwrap();
|
||||
let mut index = 0;
|
||||
let mut ancestor_depths = Vec::new();
|
||||
while let Some(first) = rows.get(index) {
|
||||
let mut end = index;
|
||||
if fold_paths
|
||||
&& unbranched(&first.name)
|
||||
&& first
|
||||
.parent
|
||||
.as_ref()
|
||||
.is_none_or(|parent| single_placement(parent, &first.name))
|
||||
{
|
||||
while rows
|
||||
.get(end + 1)
|
||||
.is_some_and(|next| can_join(&rows[end], next))
|
||||
{
|
||||
end += 1;
|
||||
}
|
||||
// Two rows are already easy to scan.
|
||||
if end - index < 2 {
|
||||
end = index;
|
||||
}
|
||||
}
|
||||
if changes.is_some() {
|
||||
out.push_str(&cell_label(&name));
|
||||
} else {
|
||||
write!(out, "{name:?}").unwrap();
|
||||
while ancestor_depths
|
||||
.last()
|
||||
.is_some_and(|&depth| depth >= first.depth)
|
||||
{
|
||||
ancestor_depths.pop();
|
||||
}
|
||||
if let Some(parent) = parent {
|
||||
write!(out, " {}", edge_label(&parent, &name)).unwrap();
|
||||
out.push_str(&" ".repeat(ancestor_depths.len()));
|
||||
for (i, row) in rows[index..=end].iter().enumerate() {
|
||||
let name = &row.name;
|
||||
if i != 0 {
|
||||
out.push_str(" → ");
|
||||
}
|
||||
if direct.contains(name) {
|
||||
let marker = cell_marker(old.cells.contains(name), new.cells.contains(name));
|
||||
write!(out, "{marker} ").unwrap();
|
||||
}
|
||||
if changes.is_some() {
|
||||
out.push_str(&cell_label(name));
|
||||
} else {
|
||||
write!(out, "{name:?}").unwrap();
|
||||
}
|
||||
if i == 0
|
||||
&& let Some(parent) = &row.parent
|
||||
{
|
||||
write!(out, " {}", edge_label(parent, name)).unwrap();
|
||||
}
|
||||
}
|
||||
writeln!(out, "{}{note}", status(&name)).unwrap();
|
||||
let last = &rows[end];
|
||||
writeln!(out, "{}{}", status(&last.name), last.note).unwrap();
|
||||
ancestor_depths.push(last.depth);
|
||||
index = end + 1;
|
||||
}
|
||||
if visible.is_empty() {
|
||||
out.push_str("No changed hierarchy\n");
|
||||
|
|
|
|||
|
|
@ -533,6 +533,48 @@ fn shared_hierarchy_reports_ancestors_and_each_leaf_placement_count() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ascii_diff_folds_single_placement_paths_up_to_branches_and_changed_edges() {
|
||||
let with_branch = |count, shape| {
|
||||
layout(&[
|
||||
("CHIP", vec![reference("block", 0, 1)]),
|
||||
("block", vec![reference("routing", 0, 1)]),
|
||||
("routing", vec![reference("fanout", 0, 1)]),
|
||||
(
|
||||
"fanout",
|
||||
vec![reference("left", 0, 1), reference("right", 0, 1)],
|
||||
),
|
||||
("left", vec![reference("target", 0, count)]),
|
||||
("target", vec![]),
|
||||
("right", vec![reference("unit", 0, 1)]),
|
||||
("unit", vec![reference("leaf", 0, 1)]),
|
||||
("leaf", vec![shape]),
|
||||
])
|
||||
};
|
||||
let old = with_branch(2, square());
|
||||
let new = with_branch(3, triangle());
|
||||
let diff = Comparison::between(
|
||||
&Document::parse(&old).unwrap(),
|
||||
&Document::parse(&new).unwrap(),
|
||||
);
|
||||
assert_eq!(
|
||||
diff.hierarchy(false, 8),
|
||||
concat!(
|
||||
"Hierarchy\n",
|
||||
"CHIP → block → routing [affected]\n",
|
||||
" fanout x1 [affected]\n",
|
||||
" * left x1\n",
|
||||
" target x2 -> x3\n",
|
||||
" right x1 → unit → * leaf\n",
|
||||
)
|
||||
);
|
||||
assert_eq!(
|
||||
diff.hierarchy(false, 2),
|
||||
"Hierarchy\nCHIP → block → routing [affected] [children omitted]\n"
|
||||
);
|
||||
assert!(!diff.hierarchy(true, 8).contains('→'));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ascii_hierarchy_marks_repeated_subtrees_and_depth_limits() {
|
||||
let bytes = layout(&[
|
||||
|
|
@ -880,7 +922,13 @@ fn cli_color_preserves_plain_output_and_machine_formats() {
|
|||
.map(|x| reference("leaf", x, 1))
|
||||
.chain([shape])
|
||||
.collect();
|
||||
layout(&[("CHIP", objects), ("leaf", vec![]), (name, vec![])])
|
||||
layout(&[
|
||||
("top", vec![reference("route", 0, 1)]),
|
||||
("route", vec![reference("CHIP", 0, 1)]),
|
||||
("CHIP", objects),
|
||||
("leaf", vec![]),
|
||||
(name, vec![]),
|
||||
])
|
||||
};
|
||||
fs::write(&old, with_changes("obsolete", square(), 0..2)).unwrap();
|
||||
fs::write(&new, with_changes("new", triangle(), 10..13)).unwrap();
|
||||
|
|
@ -928,6 +976,13 @@ fn cli_color_preserves_plain_output_and_machine_formats() {
|
|||
diff(&["--color", "always", "--format", "json"]),
|
||||
diff(&["--format", "json"])
|
||||
);
|
||||
let ascii = diff(&["--hierarchy", "ascii", "--color=always"]);
|
||||
let folded = ascii
|
||||
.lines()
|
||||
.find(|line| line.starts_with("top → route → "))
|
||||
.unwrap();
|
||||
assert!(folded.contains("\x1b[33m* CHIP"));
|
||||
assert_eq!(strip_ansi(&ascii), diff(&["--hierarchy", "ascii"]));
|
||||
|
||||
for (options, token) in [
|
||||
(&[][..], "\x1b[36mcell \"CHIP\""),
|
||||
|
|
|
|||
Loading…
Reference in a new issue