From aee65a315ab3343e386dd81d82a334d32b552a39 Mon Sep 17 00:00:00 2001 From: Scott Schafer Date: Thu, 31 Jul 2025 03:55:04 -0600 Subject: [PATCH 1/2] test: Add tests for primary selection --- tests/color/first_snippet_is_primary.rs | 48 +++++++++++++++++ tests/color/first_snippet_is_primary.term.svg | 54 +++++++++++++++++++ tests/color/main.rs | 1 + 3 files changed, 103 insertions(+) create mode 100644 tests/color/first_snippet_is_primary.rs create mode 100644 tests/color/first_snippet_is_primary.term.svg diff --git a/tests/color/first_snippet_is_primary.rs b/tests/color/first_snippet_is_primary.rs new file mode 100644 index 00000000..e5bf71bd --- /dev/null +++ b/tests/color/first_snippet_is_primary.rs @@ -0,0 +1,48 @@ +use annotate_snippets::{AnnotationKind, Group, Level, Renderer, Snippet}; + +use snapbox::{assert_data_eq, file}; + +#[test] +fn case() { + let file_txt_source = r#"this is from a txt file"#; + + let rust_source = r#"fn main() { + let b: &[u8] = include_str!("file.txt"); + let s: &str = include_bytes!("file.txt"); +}"#; + + let input = &[ + Group::with_title(Level::ERROR.title("mismatched types").id("E0308")) + .element( + Snippet::source(file_txt_source) + .line_start(3) + .path("$DIR/file.txt") + .annotation( + AnnotationKind::Context + .span(0..23) + .label("the macro expands to this string"), + ), + ) + .element( + Snippet::source(rust_source) + .path("$DIR/mismatched-types.rs") + .annotation( + AnnotationKind::Context + .span(23..28) + .label("expected due to this"), + ) + .annotation( + AnnotationKind::Primary + .span(31..55) + .label("expected `&[u8]`, found `&str`"), + ), + ) + .element( + Level::NOTE + .message("expected reference `&[u8]`\n found reference `&'static str`"), + ), + ]; + let expected = file!["first_snippet_is_primary.term.svg"]; + let renderer = Renderer::styled(); + assert_data_eq!(renderer.render(input), expected); +} diff --git a/tests/color/first_snippet_is_primary.term.svg b/tests/color/first_snippet_is_primary.term.svg new file mode 100644 index 00000000..32cb3371 --- /dev/null +++ b/tests/color/first_snippet_is_primary.term.svg @@ -0,0 +1,54 @@ + + + + + + + error[E0308]: mismatched types + + | + + ::: $DIR/file.txt:3:1 + + | + + 3 | this is from a txt file + + | ----------------------- the macro expands to this string + + --> $DIR/mismatched-types.rs:2:20 + + | + + 2 | let b: &[u8] = include_str!("file.txt"); + + | ----- ^^^^^^^^^^^^^^^^^^^^^^^^ expected `&[u8]`, found `&str` + + | | + + | expected due to this + + | + + = note: expected reference `&[u8]` + + found reference `&'static str` + + + + diff --git a/tests/color/main.rs b/tests/color/main.rs index a9885a0b..0bc932f4 100644 --- a/tests/color/main.rs +++ b/tests/color/main.rs @@ -4,6 +4,7 @@ mod ann_multiline; mod ann_multiline2; mod ann_removed_nl; mod ensure_emoji_highlight_width; +mod first_snippet_is_primary; mod fold_ann_multiline; mod fold_bad_origin_line; mod fold_leading; From ffa7a88c314f9aaf0d7cd57906b1337edabb6c89 Mon Sep 17 00:00:00 2001 From: Scott Schafer Date: Thu, 31 Jul 2025 03:55:04 -0600 Subject: [PATCH 2/2] fix!: Make the first snippet or origin primary --- src/renderer/mod.rs | 28 ++----------------- tests/color/first_snippet_is_primary.term.svg | 12 ++++---- 2 files changed, 9 insertions(+), 31 deletions(-) diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 8620d79c..3c6bd486 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -243,33 +243,11 @@ impl Renderer { .elements .iter() .find_map(|s| match &s { - Element::Cause(cause) => { - if cause.markers.iter().any(|m| m.kind.is_primary()) { - Some(cause.path.as_ref()) - } else { - None - } - } - Element::Origin(origin) => { - if origin.primary { - Some(Some(&origin.path)) - } else { - None - } - } + Element::Cause(cause) => Some(cause.path.as_ref()), + Element::Origin(origin) => Some(Some(&origin.path)), _ => None, }) - .unwrap_or( - group - .elements - .iter() - .find_map(|s| match &s { - Element::Cause(cause) => Some(cause.path.as_ref()), - Element::Origin(origin) => Some(Some(&origin.path)), - _ => None, - }) - .unwrap_or_default(), - ); + .unwrap_or_default(); if og_primary_path.is_none() && primary_path.is_some() { og_primary_path = primary_path; } diff --git a/tests/color/first_snippet_is_primary.term.svg b/tests/color/first_snippet_is_primary.term.svg index 32cb3371..fc1e388e 100644 --- a/tests/color/first_snippet_is_primary.term.svg +++ b/tests/color/first_snippet_is_primary.term.svg @@ -21,17 +21,17 @@ error[E0308]: mismatched types - | + --> $DIR/file.txt:3:1 - ::: $DIR/file.txt:3:1 + | - | + 3 | this is from a txt file - 3 | this is from a txt file + | ----------------------- the macro expands to this string - | ----------------------- the macro expands to this string + | - --> $DIR/mismatched-types.rs:2:20 + ::: $DIR/mismatched-types.rs:2:12 |