Skip to content

Commit

Permalink
front: dim identical rule pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Jan 1, 2025
1 parent 878b9e4 commit 176a32f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 46 deletions.
40 changes: 14 additions & 26 deletions src/ast/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,43 @@ pub trait Style {
fn green(&self) -> String;
fn red(&self) -> String;
fn comment(&self) -> String;
fn dimmed(&self) -> String;
fn tooltip(&self, text: &str) -> String;
fn inherited_ref(&self) -> String;
fn code(&self) -> String;
}

impl Style for &str {
impl<T: Display + AsRef<str>> Style for T {
fn green(&self) -> String {
if cfg!(target_arch = "wasm32") {
format!("<span style=\"color: green\">{self}</span>")
} else {
use colored::Colorize;
<Self as Colorize>::green(self).to_string()
<_ as Colorize>::green(self.as_ref()).to_string()
}
}
fn red(&self) -> String {
if cfg!(target_arch = "wasm32") {
format!("<span style=\"color: red\">{self}</span>")
} else {
use colored::Colorize;
<Self as Colorize>::red(self).to_string()
<_ as Colorize>::red(self.as_ref()).to_string()
}
}
fn dimmed(&self) -> String {
if cfg!(target_arch = "wasm32") {
format!("<span style=\"opacity: 0.5\">{self}</span>")
} else {
use colored::Colorize;
<_ as Colorize>::dimmed(self.as_ref()).to_string()
}
}
fn comment(&self) -> String {
if cfg!(target_arch = "wasm32") {
format!("<span style=\"color: dimgray\">{self}</span>")
} else {
use colored::Colorize;
<Self as Colorize>::dimmed(self).to_string()
<_ as Colorize>::dimmed(self.as_ref()).to_string()
}
}
fn tooltip(&self, text: &str) -> String {
Expand All @@ -52,7 +61,7 @@ impl Style for &str {
format!("<span class=\"inherited-ref\">{self}</span>")
} else {
use colored::Colorize;
<Self as Colorize>::dimmed(self).to_string()
<_ as Colorize>::dimmed(self.as_ref()).to_string()
}
.tooltip("inherited reference")
}
Expand All @@ -65,27 +74,6 @@ impl Style for &str {
}
}

impl Style for String {
fn green(&self) -> String {
self.as_str().green()
}
fn red(&self) -> String {
self.as_str().red()
}
fn inherited_ref(&self) -> String {
self.as_str().inherited_ref()
}
fn comment(&self) -> String {
self.as_str().comment()
}
fn tooltip(&self, text: &str) -> String {
self.as_str().tooltip(text)
}
fn code(&self) -> String {
self.as_str().code()
}
}

impl BindingMode {
pub fn name(self) -> &'static str {
match self {
Expand Down
40 changes: 21 additions & 19 deletions src/ast/printer/display_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,40 +166,43 @@ impl<'a> DisplayTree<'a> {

/// Display `self` and `other`, highlighting differences.
pub fn diff_display(&self, other: &Self) -> (String, String) {
let (left, right, _) = self.diff_display_has_diff(other);
(left, right)
}

/// Display `self` and `other`, highlighting differences. Returns whether there was any diff.
pub fn diff_display_has_diff(&self, other: &Self) -> (String, String, bool) {
let mut left = String::new();
let mut right = String::new();
let _ = self.diff_display_inner(other, &mut left, &mut right);
(left, right)
let has_diff = self
.diff_display_inner(other, &mut left, &mut right)
.unwrap();
(left, right, has_diff)
}

/// Returns whether there was any diff.
fn diff_display_inner(
&self,
other: &Self,
left: &mut String,
right: &mut String,
) -> std::fmt::Result {
) -> Result<bool, std::fmt::Error> {
use std::fmt::Write;
// The trivial cases: the trees are either fully identical or fully different.
let all_same = |left: &mut String, right: &mut String| {
write!(left, "{self}")?;
write!(right, "{other}")?;
Ok(())
Ok(false)
};
let all_different = |left: &mut String, right: &mut String| {
write!(left, "{}", self.to_string().red())?;
write!(right, "{}", other.to_string().green())?;
Ok(())
Ok(true)
};
match (self.kind, other.kind) {
_ if self.tag != other.tag => {
all_different(left, right)?;
}
_ if self.ignore_for_diff && other.ignore_for_diff => {
all_same(left, right)?;
}
(Leaf(l), Leaf(r)) if strip_markup(l) == strip_markup(r) => {
all_same(left, right)?;
}
_ if self.tag != other.tag => all_different(left, right),
_ if self.ignore_for_diff && other.ignore_for_diff => all_same(left, right),
(Leaf(l), Leaf(r)) if strip_markup(l) == strip_markup(r) => all_same(left, right),
// The non-trivial case: the trees differ partially.
(
Separated { sep, children: c1 },
Expand All @@ -209,20 +212,19 @@ impl<'a> DisplayTree<'a> {
},
) if strip_markup(sep) == strip_markup(sep2) && c1.len() == c2.len() => {
let mut is_first = true;
let mut any_diff = false;
for (c1, c2) in c1.iter().zip(c2) {
if !is_first {
write!(left, "{sep}")?;
write!(right, "{sep}")?;
}
c1.diff_display_inner(c2, left, right)?;
any_diff |= c1.diff_display_inner(c2, left, right)?;
is_first = false;
}
Ok(any_diff)
}
_ => {
all_different(left, right)?;
}
_ => all_different(left, right),
}
Ok(())
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,11 @@ pub fn display_joint_rules_js(
.map(|r| r.make_renderable(a, style).unwrap())
.map(|r| r.display_to_tree(a, style))
.unwrap_or_default();
let (left, right) = left.diff_display(&right);
let (mut left, mut right, has_diff) = left.diff_display_has_diff(&right);
if !has_diff {
left = left.dimmed();
right = right.dimmed();
}
JointDisplayOutput { left, right }
})
.map(|out| JsValue::from_serde(&out).unwrap())
Expand Down

0 comments on commit 176a32f

Please sign in to comment.