Skip to content

Commit

Permalink
Merge pull request #107 from epage/past
Browse files Browse the repository at this point in the history
fix: Allow empty annotation spans
  • Loading branch information
Muscraft authored Mar 21, 2024
2 parents 3c96e12 + 7310fd4 commit fb131af
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/renderer/display_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,7 @@ fn format_body(
Range { start, .. } if start > line_end_index => true,
Range { start, end }
if start >= line_start_index && end <= line_end_index
// Allow annotating eof or stripped eol
|| start == line_end_index && end - start <= 1 =>
{
if let DisplayLine::Source {
Expand All @@ -1068,14 +1069,15 @@ fn format_body(
.chars()
.map(|c| unicode_width::UnicodeWidthChar::width(c).unwrap_or(0))
.sum::<usize>();
// This allows for annotations to be placed one past the
// last character
let safe_end = (end - line_start_index).saturating_sub(line_length);
let annotation_end_col = line[0..(end - line_start_index) - safe_end]
let mut annotation_end_col = line
[0..(end - line_start_index).min(line_length)]
.chars()
.map(|c| unicode_width::UnicodeWidthChar::width(c).unwrap_or(0))
.sum::<usize>()
+ safe_end;
.sum::<usize>();
if annotation_start_col == annotation_end_col {
// At least highlight something
annotation_end_col += 1;
}

span_left_margin = min(span_left_margin, annotation_start_col);
span_right_margin = max(span_right_margin, annotation_end_col);
Expand Down
File renamed without changes
12 changes: 12 additions & 0 deletions tests/fixtures/no-color/ann_eof.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[message]
level = "Error"
title = "expected `.`, `=`"

[[message.snippets]]
source = "asdf"
line_start = 1
origin = "Cargo.toml"
[[message.snippets.annotations]]
label = ""
level = "Error"
range = [4, 4]
33 changes: 33 additions & 0 deletions tests/fixtures/no-color/ann_insertion.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions tests/fixtures/no-color/ann_insertion.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[message]
level = "Error"
title = "expected `.`, `=`"

[[message.snippets]]
source = "asf"
line_start = 1
origin = "Cargo.toml"
[[message.snippets.annotations]]
label = "'d' belongs here"
level = "Error"
range = [2, 2]
33 changes: 33 additions & 0 deletions tests/fixtures/no-color/ann_removed_nl.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.

0 comments on commit fb131af

Please sign in to comment.