Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up render snippets #324

Merged
merged 7 commits into from
Jan 11, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 64 additions & 61 deletions src/handlers/graphical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use unicode_width::UnicodeWidthChar;
use crate::diagnostic_chain::{DiagnosticChain, ErrorKind};
use crate::handlers::theme::*;
use crate::protocol::{Diagnostic, Severity};
use crate::{LabeledSpan, MietteError, ReportHandler, SourceCode, SourceSpan, SpanContents};
use crate::{LabeledSpan, ReportHandler, SourceCode, SourceSpan, SpanContents};

/**
A [`ReportHandler`] that displays a given [`Report`](crate::Report) in a
Expand Down Expand Up @@ -377,66 +377,63 @@ impl GraphicalReportHandler {
diagnostic: &(dyn Diagnostic),
opt_source: Option<&dyn SourceCode>,
) -> fmt::Result {
if let Some(source) = opt_source {
if let Some(labels) = diagnostic.labels() {
let mut labels = labels.collect::<Vec<_>>();
labels.sort_unstable_by_key(|l| l.inner().offset());
if !labels.is_empty() {
let contents = labels
.iter()
.map(|label| {
source.read_span(label.inner(), self.context_lines, self.context_lines)
})
.collect::<Result<Vec<Box<dyn SpanContents<'_>>>, MietteError>>()
.map_err(|_| fmt::Error)?;
let mut contexts = Vec::with_capacity(contents.len());
for (right, right_conts) in labels.iter().cloned().zip(contents.iter()) {
if contexts.is_empty() {
contexts.push((right, right_conts));
} else {
let (left, left_conts) = contexts.last().unwrap().clone();
let left_end = left.offset() + left.len();
let right_end = right.offset() + right.len();
if left_conts.line() + left_conts.line_count() >= right_conts.line() {
// The snippets will overlap, so we create one Big Chunky Boi
let new_span = LabeledSpan::new(
left.label().map(String::from),
left.offset(),
if right_end >= left_end {
// Right end goes past left end
right_end - left.offset()
} else {
// right is contained inside left
left.len()
},
);
if source
.read_span(
new_span.inner(),
self.context_lines,
self.context_lines,
)
.is_ok()
{
contexts.pop();
contexts.push((
// We'll throw this away later
new_span, left_conts,
));
} else {
contexts.push((right, right_conts));
}
} else {
contexts.push((right, right_conts));
}
}
}
for (ctx, _) in contexts {
self.render_context(f, source, &ctx, &labels[..])?;
}
let Some(source) = opt_source else {
LHolten marked this conversation as resolved.
Show resolved Hide resolved
return Ok(());
};
let Some(labels) = diagnostic.labels() else {
return Ok(());
};

let mut labels = labels.collect::<Vec<_>>();
labels.sort_unstable_by_key(|l| l.inner().offset());

let mut contexts = Vec::with_capacity(labels.len());
for right in labels.iter().cloned() {
let right_conts = source
.read_span(right.inner(), self.context_lines, self.context_lines)
.map_err(|_| fmt::Error)?;

if contexts.is_empty() {
contexts.push((right, (right_conts.line(), right_conts.line_count())));
continue;
}

let (left, (left_line, left_line_count)) = contexts.last().unwrap().clone();
if left_line + left_line_count >= right_conts.line() {
// The snippets will overlap, so we create one Big Chunky Boi
let left_end = left.offset() + left.len();
let right_end = right.offset() + right.len();
let new_span = LabeledSpan::new(
left.label().map(String::from),
left.offset(),
if right_end >= left_end {
// Right end goes past left end
right_end - left.offset()
} else {
// right is contained inside left
left.len()
},
);
// Check that the two contexts can be combined
if let Ok(new_conts) =
source.read_span(new_span.inner(), self.context_lines, self.context_lines)
{
contexts.pop();
contexts.push((
// We'll throw this away later
new_span,
(new_conts.line(), new_conts.line_count()),
LHolten marked this conversation as resolved.
Show resolved Hide resolved
));
continue;
}
}

contexts.push((right, (right_conts.line(), right_conts.line_count())));
}
for (ctx, _) in contexts {
self.render_context(f, source, &ctx, &labels[..])?;
}

Ok(())
}

Expand All @@ -449,10 +446,16 @@ impl GraphicalReportHandler {
) -> fmt::Result {
let (contents, lines) = self.get_lines(source, context.inner())?;

let primary_label = labels
.iter()
// only consider labels from the context as primary label
let ctx_labels = labels.iter().filter(|l| {
context.inner().offset() <= l.inner().offset()
&& l.inner().offset() + l.inner().len()
<= context.inner().offset() + context.inner().len()
});
let primary_label = ctx_labels
.clone()
.find(|label| label.primary())
.or_else(|| labels.first());
.or_else(|| ctx_labels.clone().next());

// sorting is your friend
let labels = labels
Expand Down
Loading