Fold unbranched paths in ASCII diffs

This commit is contained in:
Jan Petykiewicz 2026-11-18 23:04:24 -08:00
commit 4ae689cd1c

View file

@ -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");