Skip to content

Commit 5218ce0

Browse files
authored
Merge pull request #237 from epage/group-title
fix: Force group levels to be explicitly given
2 parents 74c2517 + 49322a7 commit 5218ce0

32 files changed

+1038
-1137
lines changed

benches/bench.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ fn simple() -> String {
2424
_ => continue,
2525
}
2626
}"#;
27-
let message = &[Group::new()
28-
.element(Level::ERROR.title("mismatched types").id("E0308"))
29-
.element(
27+
let message = &[
28+
Group::with_title(Level::ERROR.title("mismatched types").id("E0308")).element(
3029
Snippet::source(source)
3130
.line_start(51)
3231
.path("src/format.rs")
@@ -40,7 +39,8 @@ fn simple() -> String {
4039
.span(26..724)
4140
.label("expected enum `std::option::Option`"),
4241
),
43-
)];
42+
),
43+
];
4444

4545
let renderer = Renderer::plain();
4646
let rendered = renderer.render(message);
@@ -69,9 +69,8 @@ fn fold(bencher: divan::Bencher<'_, '_>, context: usize) {
6969
(input, span)
7070
})
7171
.bench_values(|(input, span)| {
72-
let message = &[Group::new()
73-
.element(Level::ERROR.title("mismatched types").id("E0308"))
74-
.element(
72+
let message = &[
73+
Group::with_title(Level::ERROR.title("mismatched types").id("E0308")).element(
7574
Snippet::source(&input)
7675
.fold(true)
7776
.path("src/format.rs")
@@ -80,7 +79,8 @@ fn fold(bencher: divan::Bencher<'_, '_>, context: usize) {
8079
.span(span)
8180
.label("expected `Option<String>` because of return type"),
8281
),
83-
)];
82+
),
83+
];
8484

8585
let renderer = Renderer::plain();
8686
let rendered = renderer.render(message);

examples/custom_error.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,22 @@ fn main() {
1515
pub static C: u32 = 0 - 1;
1616
//~^ ERROR could not evaluate static initializer
1717
"#;
18-
let message = &[Group::new()
19-
.element(
20-
Level::ERROR
21-
.text(Some("error: internal compiler error"))
22-
.title("could not evaluate static initializer")
23-
.id("E0080"),
24-
)
25-
.element(
26-
Snippet::source(source)
27-
.path("$DIR/err.rs")
28-
.fold(true)
29-
.annotation(
30-
AnnotationKind::Primary
31-
.span(386..391)
32-
.label("attempt to compute `0_u32 - 1_u32`, which would overflow"),
33-
),
34-
)];
18+
let message = &[Group::with_title(
19+
Level::ERROR
20+
.text(Some("error: internal compiler error"))
21+
.title("could not evaluate static initializer")
22+
.id("E0080"),
23+
)
24+
.element(
25+
Snippet::source(source)
26+
.path("$DIR/err.rs")
27+
.fold(true)
28+
.annotation(
29+
AnnotationKind::Primary
30+
.span(386..391)
31+
.label("attempt to compute `0_u32 - 1_u32`, which would overflow"),
32+
),
33+
)];
3534

3635
let renderer = Renderer::styled().theme(OutputTheme::Unicode);
3736
anstream::println!("{}", renderer.render(message));

examples/custom_level.rs

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,41 +30,39 @@ fn main() {
3030
}
3131
"#;
3232
let message = &[
33-
Group::new()
34-
.element(
35-
Level::ERROR
36-
.title("`break` with value from a `while` loop")
37-
.id("E0571"),
38-
)
39-
.element(
40-
Snippet::source(source)
41-
.line_start(1)
42-
.path("$DIR/issue-114529-illegal-break-with-value.rs")
43-
.fold(true)
44-
.annotation(
45-
AnnotationKind::Primary
46-
.span(483..581)
47-
.label("can only break with a value inside `loop` or breakable block"),
48-
)
49-
.annotation(
50-
AnnotationKind::Context
51-
.span(462..472)
52-
.label("you can't `break` with a value in a `while` loop"),
53-
),
54-
),
55-
Group::new()
56-
.element(
57-
Level::HELP
58-
.text(Some("suggestion"))
59-
.title("use `break` on its own without a value inside this `while` loop"),
60-
)
61-
.element(
62-
Snippet::source(source)
63-
.line_start(1)
64-
.path("$DIR/issue-114529-illegal-break-with-value.rs")
65-
.fold(true)
66-
.patch(Patch::new(483..581, "break")),
67-
),
33+
Group::with_title(
34+
Level::ERROR
35+
.title("`break` with value from a `while` loop")
36+
.id("E0571"),
37+
)
38+
.element(
39+
Snippet::source(source)
40+
.line_start(1)
41+
.path("$DIR/issue-114529-illegal-break-with-value.rs")
42+
.fold(true)
43+
.annotation(
44+
AnnotationKind::Primary
45+
.span(483..581)
46+
.label("can only break with a value inside `loop` or breakable block"),
47+
)
48+
.annotation(
49+
AnnotationKind::Context
50+
.span(462..472)
51+
.label("you can't `break` with a value in a `while` loop"),
52+
),
53+
),
54+
Group::with_title(
55+
Level::HELP
56+
.text(Some("suggestion"))
57+
.title("use `break` on its own without a value inside this `while` loop"),
58+
)
59+
.element(
60+
Snippet::source(source)
61+
.line_start(1)
62+
.path("$DIR/issue-114529-illegal-break-with-value.rs")
63+
.fold(true)
64+
.patch(Patch::new(483..581, "break")),
65+
),
6866
];
6967

7068
let renderer = Renderer::styled().theme(OutputTheme::Unicode);

examples/elide_header.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ def foobar(door, bar={}):
88
"""
99
"#;
1010

11-
let message = &[Group::new()
12-
.primary_level(Level::NOTE)
11+
let message = &[Group::with_level(Level::NOTE)
1312
.element(
1413
Snippet::source(source)
1514
.fold(false)

examples/expected_type.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ fn main() {
66
,
77
range: <22, 25>,"#;
88
let message =
9-
&[Group::new()
10-
.element(Level::ERROR.title("expected type, found `22`"))
11-
.element(
9+
&[
10+
Group::with_title(Level::ERROR.title("expected type, found `22`")).element(
1211
Snippet::source(source)
1312
.line_start(26)
1413
.path("examples/footer.rs")
@@ -21,7 +20,8 @@ fn main() {
2120
.span(34..50)
2221
.label("while parsing this struct"),
2322
),
24-
)];
23+
),
24+
];
2525

2626
let renderer = Renderer::styled();
2727
anstream::println!("{}", renderer.render(message));

examples/footer.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
use annotate_snippets::{AnnotationKind, Group, Level, Renderer, Snippet};
22

33
fn main() {
4-
let message = &[
5-
Group::new()
6-
.element(Level::ERROR.title("mismatched types").id("E0308"))
7-
.element(
4+
let message =
5+
&[
6+
Group::with_title(Level::ERROR.title("mismatched types").id("E0308")).element(
87
Snippet::source(" slices: vec![\"A\",")
98
.line_start(13)
109
.path("src/multislice.rs")
1110
.annotation(AnnotationKind::Primary.span(21..24).label(
1211
"expected struct `annotate_snippets::snippet::Slice`, found reference",
1312
)),
1413
),
15-
Group::new().element(Level::NOTE.title(
16-
"expected type: `snippet::Annotation`\n found type: `__&__snippet::Annotation`",
17-
)),
18-
];
14+
Group::with_title(Level::NOTE.title(
15+
"expected type: `snippet::Annotation`\n found type: `__&__snippet::Annotation`",
16+
)),
17+
];
1918

2019
let renderer = Renderer::styled();
2120
anstream::println!("{}", renderer.render(message));

examples/format.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ fn main() {
2323
_ => continue,
2424
}
2525
}"#;
26-
let message = &[Group::new()
27-
.element(Level::ERROR.title("mismatched types").id("E0308"))
28-
.element(
26+
let message = &[
27+
Group::with_title(Level::ERROR.title("mismatched types").id("E0308")).element(
2928
Snippet::source(source)
3029
.line_start(51)
3130
.path("src/format.rs")
@@ -39,7 +38,8 @@ fn main() {
3938
.span(26..724)
4039
.label("expected enum `std::option::Option`"),
4140
),
42-
)];
41+
),
42+
];
4343

4444
let renderer = Renderer::styled();
4545
anstream::println!("{}", renderer.render(message));

examples/highlight_source.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const CON: Vec<i32> = vec![1, 2, 3]; //~ ERROR E0010
99
//~| ERROR cannot call non-const method
1010
fn main() {}
1111
"#;
12-
let message = &[Group::new().element(Level::ERROR.title("allocations are not allowed in constants")
12+
let message = &[Group::with_title(Level::ERROR.title("allocations are not allowed in constants")
1313
.id("E0010"))
1414
.element(
1515
Snippet::source(source)

examples/highlight_title.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ fn main() {
4242
);
4343

4444
let message = &[
45-
Group::new()
46-
.element(Level::ERROR.title("mismatched types").id("E0308"))
45+
Group::with_title(Level::ERROR.title("mismatched types").id("E0308"))
4746
.element(
4847
Snippet::source(source)
4948
.fold(true)
@@ -60,15 +59,13 @@ fn main() {
6059
),
6160
)
6261
.element(Level::NOTE.pre_styled_title(&title)),
63-
Group::new()
64-
.element(Level::NOTE.title("function defined here"))
65-
.element(
66-
Snippet::source(source)
67-
.fold(true)
68-
.path("$DIR/highlighting.rs")
69-
.annotation(AnnotationKind::Context.span(200..333).label(""))
70-
.annotation(AnnotationKind::Primary.span(194..199)),
71-
),
62+
Group::with_title(Level::NOTE.title("function defined here")).element(
63+
Snippet::source(source)
64+
.fold(true)
65+
.path("$DIR/highlighting.rs")
66+
.annotation(AnnotationKind::Context.span(200..333).label(""))
67+
.annotation(AnnotationKind::Primary.span(194..199)),
68+
),
7269
];
7370

7471
let renderer = Renderer::styled().anonymized_line_numbers(true);

examples/id_hyperlink.rs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,28 @@ fn main() {
77
let () = 4; //~ ERROR
88
}
99
"#;
10-
let message = &[Group::new()
11-
.element(
12-
Level::ERROR
13-
.title("mismatched types")
14-
.id("E0308")
15-
.id_url("https://doc.rust-lang.org/error_codes/E0308.html"),
16-
)
17-
.element(
18-
Snippet::source(source)
19-
.line_start(1)
20-
.path("$DIR/terminal_urls.rs")
21-
.fold(true)
22-
.annotation(
23-
AnnotationKind::Primary
24-
.span(59..61)
25-
.label("expected integer, found `()`"),
26-
)
27-
.annotation(
28-
AnnotationKind::Context
29-
.span(64..65)
30-
.label("this expression has type `{integer}`"),
31-
),
32-
)];
10+
let message = &[Group::with_title(
11+
Level::ERROR
12+
.title("mismatched types")
13+
.id("E0308")
14+
.id_url("https://doc.rust-lang.org/error_codes/E0308.html"),
15+
)
16+
.element(
17+
Snippet::source(source)
18+
.line_start(1)
19+
.path("$DIR/terminal_urls.rs")
20+
.fold(true)
21+
.annotation(
22+
AnnotationKind::Primary
23+
.span(59..61)
24+
.label("expected integer, found `()`"),
25+
)
26+
.annotation(
27+
AnnotationKind::Context
28+
.span(64..65)
29+
.label("this expression has type `{integer}`"),
30+
),
31+
)];
3332

3433
let renderer = Renderer::styled().theme(OutputTheme::Unicode);
3534
anstream::println!("{}", renderer.render(message));

examples/multislice.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use annotate_snippets::{Annotation, Group, Level, Renderer, Snippet};
22

33
fn main() {
4-
let message = &[Group::new()
5-
.element(Level::ERROR.title("mismatched types"))
4+
let message = &[Group::with_title(Level::ERROR.title("mismatched types"))
65
.element(
76
Snippet::<Annotation<'_>>::source("Foo")
87
.line_start(51)

0 commit comments

Comments
 (0)