Skip to content

Commit

Permalink
Merge pull request #74 from Muscraft/one-past
Browse files Browse the repository at this point in the history
fix: Allow highlighting one past end
  • Loading branch information
Muscraft authored Jan 4, 2024
2 parents 6a1ed2a + bc5398f commit e0bf89f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/renderer/display_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -934,14 +934,15 @@ fn format_body(
) -> Vec<DisplayLine<'_>> {
let source_len = slice.source.chars().count();
if let Some(bigger) = slice.annotations.iter().find_map(|x| {
if source_len < x.range.1 {
// Allow highlighting one past the last character in the source.
if source_len + 1 < x.range.1 {
Some(x.range)
} else {
None
}
}) {
panic!(
"SourceAnnotation range `{:?}` is bigger than source length `{}`",
"SourceAnnotation range `{:?}` is beyond the end of buffer `{}`",
bigger, source_len
)
}
Expand Down Expand Up @@ -1479,7 +1480,7 @@ mod tests {
footer: vec![],
slices: vec![snippet::Slice {
annotations: vec![snippet::SourceAnnotation {
range: (0, source.len() + 1),
range: (0, source.len() + 2),
label,
annotation_type: snippet::AnnotationType::Error,
}],
Expand Down
12 changes: 12 additions & 0 deletions tests/fixtures/no-color/one_past.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[snippet.title]
label = "expected `.`, `=`"
annotation_type = "Error"

[[snippet.slices]]
source = "asdf"
line_start = 1
origin = "Cargo.toml"
[[snippet.slices.annotations]]
label = ""
annotation_type = "Error"
range = [4, 5]
6 changes: 6 additions & 0 deletions tests/fixtures/no-color/one_past.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
error: expected `.`, `=`
--> Cargo.toml:1:5
|
1 | asdf
| ^
|

0 comments on commit e0bf89f

Please sign in to comment.