From bc5398f5f54e70a50deb0e7c2bb67cbff29a5ed8 Mon Sep 17 00:00:00 2001 From: Scott Schafer Date: Thu, 4 Jan 2024 14:17:11 -0700 Subject: [PATCH] fix: Allow highlighting one past end --- src/renderer/display_list.rs | 7 ++++--- tests/fixtures/no-color/one_past.toml | 12 ++++++++++++ tests/fixtures/no-color/one_past.txt | 6 ++++++ 3 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/no-color/one_past.toml create mode 100644 tests/fixtures/no-color/one_past.txt diff --git a/src/renderer/display_list.rs b/src/renderer/display_list.rs index e7784db..f283e52 100644 --- a/src/renderer/display_list.rs +++ b/src/renderer/display_list.rs @@ -934,14 +934,15 @@ fn format_body( ) -> Vec> { 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 ) } @@ -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, }], diff --git a/tests/fixtures/no-color/one_past.toml b/tests/fixtures/no-color/one_past.toml new file mode 100644 index 0000000..62d1d42 --- /dev/null +++ b/tests/fixtures/no-color/one_past.toml @@ -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] diff --git a/tests/fixtures/no-color/one_past.txt b/tests/fixtures/no-color/one_past.txt new file mode 100644 index 0000000..7f255b8 --- /dev/null +++ b/tests/fixtures/no-color/one_past.txt @@ -0,0 +1,6 @@ +error: expected `.`, `=` + --> Cargo.toml:1:5 + | +1 | asdf + | ^ + |