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

Colored Spans #97

Closed
wants to merge 2 commits into from
Closed
Changes from all 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
33 changes: 28 additions & 5 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,11 +439,29 @@ impl<S: Span> Report<'_, S> {
})
.min_by_key(|ll| (ll.col, !ll.label.span.start()));

// Generate a list of color spans for this line
let color_spans = self.labels.iter().filter_map(|label| {
if label.msg.is_some() {
return None;
}
if label.span.source() != src_id {
return None;
}
if span.start() >= line.span().start && span.end() <= line.span().end {
Some((span.start() - line.offset(), span.end() - line.offset(), label.color))
} else {
None
}
}).collect::<Vec<_>>();

// Generate a list of labels for this line, along with their label columns
let mut line_labels = multi_labels_with_message
.iter()
.enumerate()
.filter_map(|(_i, label)| {
if label.msg.is_none() {
return None;
}
let is_start = line.span().contains(&label.span.start());
let is_end = line.span().contains(&label.last_offset());
if is_start
Expand Down Expand Up @@ -545,6 +563,14 @@ impl<S: Span> Report<'_, S> {
};

let get_highlight = |col| {
if let Some(color_span) = color_spans
.iter()
.filter(|(start, end, _)| *start <= col && *end > col)
.map(|(start, end, c)| (c, end - start))
.min_by_key(|(_, len)| *len)
.map(|(c, _len)| *c) {
return color_span
}
margin_label
.iter()
.map(|ll| ll.label)
Expand All @@ -553,6 +579,7 @@ impl<S: Span> Report<'_, S> {
.filter(|l| l.span.contains(line.offset() + col))
// Prioritise displaying smaller spans
.min_by_key(|l| (-l.priority, l.span.len()))
.and_then(|l| l.color)
};

let get_underline = |col| {
Expand Down Expand Up @@ -583,11 +610,7 @@ impl<S: Span> Report<'_, S> {
// Line
if !is_ellipsis {
for (col, c) in src.get_line_text(line).unwrap().chars().enumerate() {
let color = if let Some(highlight) = get_highlight(col) {
highlight.color
} else {
self.config.unimportant_color()
};
let color = get_highlight(col).map_or(self.config.unimportant_color(), Some);
let (c, width) = self.config.char_width(c, col);
if c.is_whitespace() {
for _ in 0..width {
Expand Down