Skip to content

Commit 176a32f

Browse files
committed
front: dim identical rule pairs
1 parent 878b9e4 commit 176a32f

File tree

3 files changed

+40
-46
lines changed

3 files changed

+40
-46
lines changed

src/ast/printer.rs

+14-26
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,43 @@ pub trait Style {
1010
fn green(&self) -> String;
1111
fn red(&self) -> String;
1212
fn comment(&self) -> String;
13+
fn dimmed(&self) -> String;
1314
fn tooltip(&self, text: &str) -> String;
1415
fn inherited_ref(&self) -> String;
1516
fn code(&self) -> String;
1617
}
1718

18-
impl Style for &str {
19+
impl<T: Display + AsRef<str>> Style for T {
1920
fn green(&self) -> String {
2021
if cfg!(target_arch = "wasm32") {
2122
format!("<span style=\"color: green\">{self}</span>")
2223
} else {
2324
use colored::Colorize;
24-
<Self as Colorize>::green(self).to_string()
25+
<_ as Colorize>::green(self.as_ref()).to_string()
2526
}
2627
}
2728
fn red(&self) -> String {
2829
if cfg!(target_arch = "wasm32") {
2930
format!("<span style=\"color: red\">{self}</span>")
3031
} else {
3132
use colored::Colorize;
32-
<Self as Colorize>::red(self).to_string()
33+
<_ as Colorize>::red(self.as_ref()).to_string()
34+
}
35+
}
36+
fn dimmed(&self) -> String {
37+
if cfg!(target_arch = "wasm32") {
38+
format!("<span style=\"opacity: 0.5\">{self}</span>")
39+
} else {
40+
use colored::Colorize;
41+
<_ as Colorize>::dimmed(self.as_ref()).to_string()
3342
}
3443
}
3544
fn comment(&self) -> String {
3645
if cfg!(target_arch = "wasm32") {
3746
format!("<span style=\"color: dimgray\">{self}</span>")
3847
} else {
3948
use colored::Colorize;
40-
<Self as Colorize>::dimmed(self).to_string()
49+
<_ as Colorize>::dimmed(self.as_ref()).to_string()
4150
}
4251
}
4352
fn tooltip(&self, text: &str) -> String {
@@ -52,7 +61,7 @@ impl Style for &str {
5261
format!("<span class=\"inherited-ref\">{self}</span>")
5362
} else {
5463
use colored::Colorize;
55-
<Self as Colorize>::dimmed(self).to_string()
64+
<_ as Colorize>::dimmed(self.as_ref()).to_string()
5665
}
5766
.tooltip("inherited reference")
5867
}
@@ -65,27 +74,6 @@ impl Style for &str {
6574
}
6675
}
6776

68-
impl Style for String {
69-
fn green(&self) -> String {
70-
self.as_str().green()
71-
}
72-
fn red(&self) -> String {
73-
self.as_str().red()
74-
}
75-
fn inherited_ref(&self) -> String {
76-
self.as_str().inherited_ref()
77-
}
78-
fn comment(&self) -> String {
79-
self.as_str().comment()
80-
}
81-
fn tooltip(&self, text: &str) -> String {
82-
self.as_str().tooltip(text)
83-
}
84-
fn code(&self) -> String {
85-
self.as_str().code()
86-
}
87-
}
88-
8977
impl BindingMode {
9078
pub fn name(self) -> &'static str {
9179
match self {

src/ast/printer/display_tree.rs

+21-19
Original file line numberDiff line numberDiff line change
@@ -166,40 +166,43 @@ impl<'a> DisplayTree<'a> {
166166

167167
/// Display `self` and `other`, highlighting differences.
168168
pub fn diff_display(&self, other: &Self) -> (String, String) {
169+
let (left, right, _) = self.diff_display_has_diff(other);
170+
(left, right)
171+
}
172+
173+
/// Display `self` and `other`, highlighting differences. Returns whether there was any diff.
174+
pub fn diff_display_has_diff(&self, other: &Self) -> (String, String, bool) {
169175
let mut left = String::new();
170176
let mut right = String::new();
171-
let _ = self.diff_display_inner(other, &mut left, &mut right);
172-
(left, right)
177+
let has_diff = self
178+
.diff_display_inner(other, &mut left, &mut right)
179+
.unwrap();
180+
(left, right, has_diff)
173181
}
174182

183+
/// Returns whether there was any diff.
175184
fn diff_display_inner(
176185
&self,
177186
other: &Self,
178187
left: &mut String,
179188
right: &mut String,
180-
) -> std::fmt::Result {
189+
) -> Result<bool, std::fmt::Error> {
181190
use std::fmt::Write;
182191
// The trivial cases: the trees are either fully identical or fully different.
183192
let all_same = |left: &mut String, right: &mut String| {
184193
write!(left, "{self}")?;
185194
write!(right, "{other}")?;
186-
Ok(())
195+
Ok(false)
187196
};
188197
let all_different = |left: &mut String, right: &mut String| {
189198
write!(left, "{}", self.to_string().red())?;
190199
write!(right, "{}", other.to_string().green())?;
191-
Ok(())
200+
Ok(true)
192201
};
193202
match (self.kind, other.kind) {
194-
_ if self.tag != other.tag => {
195-
all_different(left, right)?;
196-
}
197-
_ if self.ignore_for_diff && other.ignore_for_diff => {
198-
all_same(left, right)?;
199-
}
200-
(Leaf(l), Leaf(r)) if strip_markup(l) == strip_markup(r) => {
201-
all_same(left, right)?;
202-
}
203+
_ if self.tag != other.tag => all_different(left, right),
204+
_ if self.ignore_for_diff && other.ignore_for_diff => all_same(left, right),
205+
(Leaf(l), Leaf(r)) if strip_markup(l) == strip_markup(r) => all_same(left, right),
203206
// The non-trivial case: the trees differ partially.
204207
(
205208
Separated { sep, children: c1 },
@@ -209,20 +212,19 @@ impl<'a> DisplayTree<'a> {
209212
},
210213
) if strip_markup(sep) == strip_markup(sep2) && c1.len() == c2.len() => {
211214
let mut is_first = true;
215+
let mut any_diff = false;
212216
for (c1, c2) in c1.iter().zip(c2) {
213217
if !is_first {
214218
write!(left, "{sep}")?;
215219
write!(right, "{sep}")?;
216220
}
217-
c1.diff_display_inner(c2, left, right)?;
221+
any_diff |= c1.diff_display_inner(c2, left, right)?;
218222
is_first = false;
219223
}
224+
Ok(any_diff)
220225
}
221-
_ => {
222-
all_different(left, right)?;
223-
}
226+
_ => all_different(left, right),
224227
}
225-
Ok(())
226228
}
227229
}
228230

src/wasm.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,11 @@ pub fn display_joint_rules_js(
285285
.map(|r| r.make_renderable(a, style).unwrap())
286286
.map(|r| r.display_to_tree(a, style))
287287
.unwrap_or_default();
288-
let (left, right) = left.diff_display(&right);
288+
let (mut left, mut right, has_diff) = left.diff_display_has_diff(&right);
289+
if !has_diff {
290+
left = left.dimmed();
291+
right = right.dimmed();
292+
}
289293
JointDisplayOutput { left, right }
290294
})
291295
.map(|out| JsValue::from_serde(&out).unwrap())

0 commit comments

Comments
 (0)