diff --git a/src/renderer/display_list.rs b/src/renderer/display_list.rs index 53caa38b..4d2836a3 100644 --- a/src/renderer/display_list.rs +++ b/src/renderer/display_list.rs @@ -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 { @@ -1068,14 +1069,15 @@ fn format_body( .chars() .map(|c| unicode_width::UnicodeWidthChar::width(c).unwrap_or(0)) .sum::(); - // 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::() - + safe_end; + .sum::(); + 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); diff --git a/tests/fixtures/no-color/one_past.svg b/tests/fixtures/no-color/ann_eof.svg similarity index 100% rename from tests/fixtures/no-color/one_past.svg rename to tests/fixtures/no-color/ann_eof.svg diff --git a/tests/fixtures/no-color/ann_eof.toml b/tests/fixtures/no-color/ann_eof.toml new file mode 100644 index 00000000..313d2204 --- /dev/null +++ b/tests/fixtures/no-color/ann_eof.toml @@ -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] diff --git a/tests/fixtures/no-color/ann_insertion.svg b/tests/fixtures/no-color/ann_insertion.svg new file mode 100644 index 00000000..b15b81b4 --- /dev/null +++ b/tests/fixtures/no-color/ann_insertion.svg @@ -0,0 +1,33 @@ + + + + + + + error: expected `.`, `=` + + --> Cargo.toml:1:3 + + | + + 1 | asf + + | ^ 'd' belongs here + + | + + + + diff --git a/tests/fixtures/no-color/ann_insertion.toml b/tests/fixtures/no-color/ann_insertion.toml new file mode 100644 index 00000000..ffd2140b --- /dev/null +++ b/tests/fixtures/no-color/ann_insertion.toml @@ -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] diff --git a/tests/fixtures/no-color/ann_removed_nl.svg b/tests/fixtures/no-color/ann_removed_nl.svg new file mode 100644 index 00000000..c8900d03 --- /dev/null +++ b/tests/fixtures/no-color/ann_removed_nl.svg @@ -0,0 +1,33 @@ + + + + + + + error: expected `.`, `=` + + --> Cargo.toml:1:5 + + | + + 1 | asdf + + | ^ + + | + + + + diff --git a/tests/fixtures/no-color/one_past.toml b/tests/fixtures/no-color/ann_removed_nl.toml similarity index 100% rename from tests/fixtures/no-color/one_past.toml rename to tests/fixtures/no-color/ann_removed_nl.toml