Skip to content

Commit

Permalink
Update annotate_snippets and spanned
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed May 2, 2024
1 parent 4dc2e2f commit 266e25d
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 90 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ comma = "1.0.0"
anyhow = "1.0.6"
indicatif = "0.17.6"
prettydiff = { version = "0.6.4", default_features = false }
annotate-snippets = { version = "0.10.0" }
annotate-snippets = { version = "0.11.2" }
levenshtein = "1.0.5"
spanned = "0.1.6"
spanned = "0.2"

[dependencies.regex]
version = "1.5.5"
Expand Down
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ impl CommentParser<Comments> {
line_start: l,
line_end: l,
col_start: NonZeroUsize::new(1).unwrap(),
col_end: NonZeroUsize::new(line.chars().count() + 1).unwrap(),
col_end: NonZeroUsize::new(line.len() + 1).unwrap(),
};
match self.parse_checked_line(fallthrough_to, Spanned::new(line, span)) {
Ok(ParsePatternResult::Other) => {
Expand Down
95 changes: 37 additions & 58 deletions src/status_emitter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Variaous schemes for reporting messages during testing or after testing is done.
use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet, SourceAnnotation};
use annotate_snippets::{Renderer, Snippet};
use bstr::ByteSlice;
use colored::Colorize;
use crossbeam_channel::{Sender, TryRecvError};
Expand Down Expand Up @@ -681,63 +681,42 @@ fn create_error(
let source = std::fs::read_to_string(file).unwrap();
let source: Vec<_> = source.split_inclusive('\n').collect();
let file = file.display().to_string();
let msg = Snippet {
title: Some(Annotation {
id: None,
annotation_type: AnnotationType::Error,
label: Some(s.as_ref()),
}),
slices: lines
.iter()
.filter_map(|(label, line)| {
let source = source.get(line.get() - 1)?;
let len = source.chars().count();
Some(Slice {
source,
line_start: line.get(),
origin: Some(&file),
annotations: label
.iter()
.map(|(label, lc)| SourceAnnotation {
range: lc.as_ref().map_or((0, len - 1), |lc| {
assert_eq!(lc.line_start, *line);
if lc.line_end > lc.line_start {
(lc.col_start.get() - 1, len - 1)
} else if lc.col_start == lc.col_end {
if lc.col_start.get() - 1 == len {
// rustc sometimes produces spans pointing *after* the `\n` at the end of the line,
// but we want to render an annotation at the end.
(lc.col_start.get() - 2, lc.col_start.get() - 1)
} else {
(lc.col_start.get() - 1, lc.col_start.get())
}
} else {
(lc.col_start.get() - 1, lc.col_end.get() - 1)
}
}),
label,
annotation_type: AnnotationType::Error,
})
.collect(),
fold: false,
})
})
.collect(),
footer: lines
.iter()
.filter_map(|(label, line)| {
if source.get(line.get() - 1).is_some() {
return None;
}
Some(label.iter().map(|(label, _)| Annotation {
id: None,
annotation_type: AnnotationType::Note,
label: Some(label),
}))
})
.flatten()
.collect(),
};
let mut msg = annotate_snippets::Level::Error.title(s.as_ref());
for &(label, line) in lines {
let Some(source) = source.get(line.get() - 1) else {
for (label, _) in label {
let footer = annotate_snippets::Level::Note.title(label);
msg = msg.footer(footer);
}

continue;
};
let len = source.len();
let snippet = Snippet::source(source)
.line_start(line.get())
.origin(&file)
.annotations(label.iter().map(|(label, lc)| {
annotate_snippets::Level::Error
.span(lc.as_ref().map_or(0..len - 1, |lc| {
assert_eq!(lc.line_start, line);
if lc.line_end > lc.line_start {
lc.col_start.get() - 1..len - 1
} else if lc.col_start == lc.col_end {
if lc.col_start.get() - 1 == len {
// rustc sometimes produces spans pointing *after* the `\n` at the end of the line,
// but we want to render an annotation at the end.
lc.col_start.get() - 2..lc.col_start.get() - 1
} else {
lc.col_start.get() - 1..lc.col_start.get()
}
} else {
lc.col_start.get() - 1..lc.col_end.get() - 1
}
}))
.label(label)
}));
msg = msg.snippet(snippet);
}
let renderer = if colored::control::SHOULD_COLORIZE.should_colorize() {
Renderer::styled()
} else {
Expand Down
8 changes: 4 additions & 4 deletions tests/integrations/basic-bin/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions tests/integrations/basic-fail-mode/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions tests/integrations/basic-fail/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/integrations/basic-fail/Cargo.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Execute `DO NOT BLESS. These are meant to fail` to update `tests/actual_tests/ex
error: there were 1 unmatched diagnostics
--> tests/actual_tests/executable_compile_err.rs:4:1
|
4 |
4 | ...
| ^ Error: this file contains an unclosed delimiter
|

Expand Down
8 changes: 4 additions & 4 deletions tests/integrations/basic/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions tests/integrations/cargo-run/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions tests/integrations/ui_test_dep_bug/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 266e25d

Please sign in to comment.