Fold unbranched paths in ASCII diffs
This commit is contained in:
parent
4789638e22
commit
4ae689cd1c
2 changed files with 208 additions and 33 deletions
|
|
@ -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");
|
||||
|
|
|
|||
Loading…
Reference in a new issue