Skip to content

Commit 19c2214

Browse files
authoredJan 11, 2024
feat(graphical): render disjoint snippets separately for cleaner output (#324)
1 parent b074446 commit 19c2214

File tree

2 files changed

+151
-61
lines changed

2 files changed

+151
-61
lines changed
 

‎src/handlers/graphical.rs

+59-61
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use unicode_width::UnicodeWidthChar;
66
use crate::diagnostic_chain::{DiagnosticChain, ErrorKind};
77
use crate::handlers::theme::*;
88
use crate::protocol::{Diagnostic, Severity};
9-
use crate::{LabeledSpan, MietteError, ReportHandler, SourceCode, SourceSpan, SpanContents};
9+
use crate::{LabeledSpan, ReportHandler, SourceCode, SourceSpan, SpanContents};
1010

1111
/**
1212
A [`ReportHandler`] that displays a given [`Report`](crate::Report) in a
@@ -386,66 +386,58 @@ impl GraphicalReportHandler {
386386
diagnostic: &(dyn Diagnostic),
387387
opt_source: Option<&dyn SourceCode>,
388388
) -> fmt::Result {
389-
if let Some(source) = opt_source {
390-
if let Some(labels) = diagnostic.labels() {
391-
let mut labels = labels.collect::<Vec<_>>();
392-
labels.sort_unstable_by_key(|l| l.inner().offset());
393-
if !labels.is_empty() {
394-
let contents = labels
395-
.iter()
396-
.map(|label| {
397-
source.read_span(label.inner(), self.context_lines, self.context_lines)
398-
})
399-
.collect::<Result<Vec<Box<dyn SpanContents<'_>>>, MietteError>>()
400-
.map_err(|_| fmt::Error)?;
401-
let mut contexts = Vec::with_capacity(contents.len());
402-
for (right, right_conts) in labels.iter().cloned().zip(contents.iter()) {
403-
if contexts.is_empty() {
404-
contexts.push((right, right_conts));
405-
} else {
406-
let (left, left_conts) = contexts.last().unwrap().clone();
407-
let left_end = left.offset() + left.len();
408-
let right_end = right.offset() + right.len();
409-
if left_conts.line() + left_conts.line_count() >= right_conts.line() {
410-
// The snippets will overlap, so we create one Big Chunky Boi
411-
let new_span = LabeledSpan::new(
412-
left.label().map(String::from),
413-
left.offset(),
414-
if right_end >= left_end {
415-
// Right end goes past left end
416-
right_end - left.offset()
417-
} else {
418-
// right is contained inside left
419-
left.len()
420-
},
421-
);
422-
if source
423-
.read_span(
424-
new_span.inner(),
425-
self.context_lines,
426-
self.context_lines,
427-
)
428-
.is_ok()
429-
{
430-
contexts.pop();
431-
contexts.push((
432-
// We'll throw this away later
433-
new_span, left_conts,
434-
));
435-
} else {
436-
contexts.push((right, right_conts));
437-
}
438-
} else {
439-
contexts.push((right, right_conts));
440-
}
441-
}
442-
}
443-
for (ctx, _) in contexts {
444-
self.render_context(f, source, &ctx, &labels[..])?;
445-
}
389+
let source = match opt_source {
390+
Some(source) => source,
391+
None => return Ok(()),
392+
};
393+
let labels = match diagnostic.labels() {
394+
Some(labels) => labels,
395+
None => return Ok(()),
396+
};
397+
398+
let mut labels = labels.collect::<Vec<_>>();
399+
labels.sort_unstable_by_key(|l| l.inner().offset());
400+
401+
let mut contexts = Vec::with_capacity(labels.len());
402+
for right in labels.iter().cloned() {
403+
let right_conts = source
404+
.read_span(right.inner(), self.context_lines, self.context_lines)
405+
.map_err(|_| fmt::Error)?;
406+
407+
if contexts.is_empty() {
408+
contexts.push((right, right_conts));
409+
continue;
410+
}
411+
412+
let (left, left_conts) = contexts.last().unwrap();
413+
if left_conts.line() + left_conts.line_count() >= right_conts.line() {
414+
// The snippets will overlap, so we create one Big Chunky Boi
415+
let left_end = left.offset() + left.len();
416+
let right_end = right.offset() + right.len();
417+
let new_end = std::cmp::max(left_end, right_end);
418+
419+
let new_span = LabeledSpan::new(
420+
left.label().map(String::from),
421+
left.offset(),
422+
new_end - left.offset(),
423+
);
424+
// Check that the two contexts can be combined
425+
if let Ok(new_conts) =
426+
source.read_span(new_span.inner(), self.context_lines, self.context_lines)
427+
{
428+
contexts.pop();
429+
// We'll throw the contents away later
430+
contexts.push((new_span, new_conts));
431+
continue;
446432
}
447433
}
434+
435+
contexts.push((right, right_conts));
436+
}
437+
for (ctx, _) in contexts {
438+
self.render_context(f, source, &ctx, &labels[..])?;
448439
}
440+
449441
Ok(())
450442
}
451443

@@ -458,10 +450,16 @@ impl GraphicalReportHandler {
458450
) -> fmt::Result {
459451
let (contents, lines) = self.get_lines(source, context.inner())?;
460452

461-
let primary_label = labels
462-
.iter()
453+
// only consider labels from the context as primary label
454+
let ctx_labels = labels.iter().filter(|l| {
455+
context.inner().offset() <= l.inner().offset()
456+
&& l.inner().offset() + l.inner().len()
457+
<= context.inner().offset() + context.inner().len()
458+
});
459+
let primary_label = ctx_labels
460+
.clone()
463461
.find(|label| label.primary())
464-
.or_else(|| labels.first());
462+
.or_else(|| ctx_labels.clone().next());
465463

466464
// sorting is your friend
467465
let labels = labels

‎tests/graphical.rs

+92
Original file line numberDiff line numberDiff line change
@@ -1757,3 +1757,95 @@ fn single_line_with_wide_char_unaligned_span_empty() -> Result<(), MietteError>
17571757
assert_eq!(expected, out);
17581758
Ok(())
17591759
}
1760+
1761+
#[test]
1762+
fn triple_adjacent_highlight() -> Result<(), MietteError> {
1763+
#[derive(Debug, Diagnostic, Error)]
1764+
#[error("oops!")]
1765+
#[diagnostic(code(oops::my::bad), help("try doing it better next time?"))]
1766+
struct MyBad {
1767+
#[source_code]
1768+
src: NamedSource,
1769+
#[label = "this bit here"]
1770+
highlight1: SourceSpan,
1771+
#[label = "also this bit"]
1772+
highlight2: SourceSpan,
1773+
#[label = "finally we got"]
1774+
highlight3: SourceSpan,
1775+
}
1776+
1777+
let src = "source\n\n\n text\n\n\n here".to_string();
1778+
let err = MyBad {
1779+
src: NamedSource::new("bad_file.rs", src),
1780+
highlight1: (0, 6).into(),
1781+
highlight2: (11, 4).into(),
1782+
highlight3: (22, 4).into(),
1783+
};
1784+
let out = fmt_report(err.into());
1785+
println!("Error: {}", out);
1786+
let expected = "oops::my::bad
1787+
1788+
× oops!
1789+
╭─[bad_file.rs:1:1]
1790+
1 │ source
1791+
· ───┬──
1792+
· ╰── this bit here
1793+
2 │
1794+
3 │
1795+
4 │ text
1796+
· ──┬─
1797+
· ╰── also this bit
1798+
5 │
1799+
6 │
1800+
7 │ here
1801+
· ──┬─
1802+
· ╰── finally we got
1803+
╰────
1804+
help: try doing it better next time?
1805+
";
1806+
assert_eq!(expected, &out);
1807+
Ok(())
1808+
}
1809+
1810+
#[test]
1811+
fn non_adjacent_highlight() -> Result<(), MietteError> {
1812+
#[derive(Debug, Diagnostic, Error)]
1813+
#[error("oops!")]
1814+
#[diagnostic(code(oops::my::bad), help("try doing it better next time?"))]
1815+
struct MyBad {
1816+
#[source_code]
1817+
src: NamedSource,
1818+
#[label = "this bit here"]
1819+
highlight1: SourceSpan,
1820+
#[label = "also this bit"]
1821+
highlight2: SourceSpan,
1822+
}
1823+
1824+
let src = "source\n\n\n\n text here".to_string();
1825+
let err = MyBad {
1826+
src: NamedSource::new("bad_file.rs", src),
1827+
highlight1: (0, 6).into(),
1828+
highlight2: (12, 4).into(),
1829+
};
1830+
let out = fmt_report(err.into());
1831+
println!("Error: {}", out);
1832+
let expected = "oops::my::bad
1833+
1834+
× oops!
1835+
╭─[bad_file.rs:1:1]
1836+
1 │ source
1837+
· ───┬──
1838+
· ╰── this bit here
1839+
2 │
1840+
╰────
1841+
╭─[bad_file.rs:5:3]
1842+
4 │
1843+
5 │ text here
1844+
· ──┬─
1845+
· ╰── also this bit
1846+
╰────
1847+
help: try doing it better next time?
1848+
";
1849+
assert_eq!(expected, &out);
1850+
Ok(())
1851+
}

0 commit comments

Comments
 (0)
Please sign in to comment.