Repository: rust-lang/annotate-snippets-rs
Pull request #74 (2024-01-04 21:21:27)

fix: Allow highlighting one past end
Being able to highlight one past the end of the given source if the source provided is from `.lines()` (and got its newline stripped) or is the last line in the file and you want to highlight the next "character". `cargo` needs something to do this, so it doesn't have to remember to add a new line in either scenario.


Patch:
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index efa54a0..3a3d812 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -49,7 +49,7 @@ jobs:
     - name: No-default features
       run: cargo test --workspace --no-default-features
   msrv:
-    name: "Check MSRV: 1.73"  # MSRV
+    name: "Check MSRV: 1.70"  # MSRV
     runs-on: ubuntu-latest
     steps:
     - name: Checkout repository
@@ -57,7 +57,7 @@ jobs:
     - name: Install Rust
       uses: dtolnay/rust-toolchain@stable
       with:
-        toolchain: "1.73"  # MSRV
+        toolchain: "1.70"  # MSRV
     - uses: Swatinem/rust-cache@v2
     - name: Default features
       run: cargo check --workspace --all-targets
@@ -119,7 +119,7 @@ jobs:
     - name: Install Rust
       uses: dtolnay/rust-toolchain@stable
       with:
-        toolchain: "1.73"  # MSRV
+        toolchain: "1.70"  # MSRV
         components: clippy
     - uses: Swatinem/rust-cache@v2
     - name: Install SARIF tools
diff --git a/Cargo.toml b/Cargo.toml
index 2cdd75e..51e52a0 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -2,7 +2,7 @@
 name = "annotate-snippets"
 version = "0.10.0"
 edition = "2021"
-rust-version = "1.73"  # MSRV
+rust-version = "1.70"  # MSRV
 authors = ["Zibi Braniecki <gandalf@mozilla.com>"]
 description = "Library for building code annotations"
 license = "Apache-2.0/MIT"
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<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
         )
     }
@@ -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,
                 }],


Test patch:
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
+  |     ^
+  |