Skip to content

fix!: Make the first snippet or origin primary #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 3 additions & 25 deletions src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
48 changes: 48 additions & 0 deletions tests/color/first_snippet_is_primary.rs
Original file line number Diff line number Diff line change
@@ -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);
}
54 changes: 54 additions & 0 deletions tests/color/first_snippet_is_primary.term.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/color/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading