From 9288922f0705f778e1f7844ef65a3efdcb8126d5 Mon Sep 17 00:00:00 2001 From: Scott Schafer Date: Thu, 27 Feb 2025 14:48:46 -0700 Subject: [PATCH 1/2] test: Add a test for keeping lines with no annotations --- examples/struct_name_as_context.rs | 31 ++++++++++++++ examples/struct_name_as_context.svg | 40 ++++++++++++++++++ tests/examples.rs | 7 ++++ tests/formatter.rs | 64 +++++++++++++++++++++++++++++ 4 files changed, 142 insertions(+) create mode 100644 examples/struct_name_as_context.rs create mode 100644 examples/struct_name_as_context.svg diff --git a/examples/struct_name_as_context.rs b/examples/struct_name_as_context.rs new file mode 100644 index 0000000..e8b69e1 --- /dev/null +++ b/examples/struct_name_as_context.rs @@ -0,0 +1,31 @@ +use annotate_snippets::{AnnotationKind, Group, Level, Renderer, Snippet}; +fn main() { + let source = r#"struct S { + field1: usize, + field2: usize, + field3: usize, + field4: usize, + fn foo() {}, + field6: usize, +} +"#; + let message = + &[ + Group::with_title( + Level::ERROR.title("functions are not allowed in struct definitions"), + ) + .element( + Snippet::source(source) + .path("$DIR/struct_name_as_context.rs") + .annotation(AnnotationKind::Primary.span(91..102)), + ) + .element( + Level::HELP.message( + "unlike in C++, Java, and C#, functions are declared in `impl` blocks", + ), + ), + ]; + + let renderer = Renderer::styled(); + anstream::println!("{}", renderer.render(message)); +} diff --git a/examples/struct_name_as_context.svg b/examples/struct_name_as_context.svg new file mode 100644 index 0000000..9002d40 --- /dev/null +++ b/examples/struct_name_as_context.svg @@ -0,0 +1,40 @@ + + + + + + + error: functions are not allowed in struct definitions + + --> $DIR/struct_name_as_context.rs:6:5 + + | + + 6 | fn foo() {}, + + | ^^^^^^^^^^^ + + | + + = help: unlike in C++, Java, and C#, functions are declared in `impl` blocks + + + + + + diff --git a/tests/examples.rs b/tests/examples.rs index 226c31f..6345559 100644 --- a/tests/examples.rs +++ b/tests/examples.rs @@ -70,6 +70,13 @@ fn multislice() { assert_example(target, expected); } +#[test] +fn struct_name_as_context() { + let target = "struct_name_as_context"; + let expected = snapbox::file!["../examples/struct_name_as_context.svg": TermSvg]; + assert_example(target, expected); +} + #[track_caller] fn assert_example(target: &str, expected: snapbox::Data) { let bin_path = snapbox::cmd::compile_example(target, ["--features=testing-colors"]).unwrap(); diff --git a/tests/formatter.rs b/tests/formatter.rs index a34517a..acf33b2 100644 --- a/tests/formatter.rs +++ b/tests/formatter.rs @@ -3005,3 +3005,67 @@ LL | .sum::<_>() //~ ERROR type annotations needed let renderer = Renderer::plain().anonymized_line_numbers(true); assert_data_eq!(renderer.render(input), expected); } + +#[test] +fn keep_lines1() { + let source = r#" +cargo +fuzzy +pizza +jumps +crazy +quack +zappy +"#; + + let input_new = &[Group::with_title( + Level::ERROR + .title("the size for values of type `T` cannot be known at compilation time") + .id("E0277"), + ) + .element( + Snippet::source(source) + .line_start(11) + .annotation(AnnotationKind::Primary.span(1..6)), + )]; + let expected = str![[r#" +error[E0277]: the size for values of type `T` cannot be known at compilation time + | +12 | cargo + | ^^^^^ +"#]]; + let renderer = Renderer::plain(); + assert_data_eq!(renderer.render(input_new), expected); +} + +#[test] +fn keep_lines2() { + let source = r#" +cargo +fuzzy +pizza +jumps +crazy +quack +zappy +"#; + + let input_new = &[Group::with_title( + Level::ERROR + .title("the size for values of type `T` cannot be known at compilation time") + .id("E0277"), + ) + .element( + Snippet::source(source) + .line_start(11) + .annotation(AnnotationKind::Primary.span(1..6)), + )]; + let expected = str![[r#" +error[E0277]: the size for values of type `T` cannot be known at compilation time + | +12 | cargo + | ^^^^^ +"#]]; + let renderer = Renderer::plain(); + assert_data_eq!(renderer.render(input_new), expected); +} From 0cd0b1f3053693cdc12a5c65149df95a3ce11a2c Mon Sep 17 00:00:00 2001 From: Scott Schafer Date: Thu, 27 Feb 2025 14:48:46 -0700 Subject: [PATCH 2/2] feat: Add a way to keep lines with no annotations --- examples/struct_name_as_context.rs | 3 ++- examples/struct_name_as_context.svg | 16 +++++++++----- src/renderer/source_map.rs | 34 +++++++++++++++++++++++++++-- src/snippet.rs | 18 +++++++++++++++ tests/formatter.rs | 10 +++++++-- 5 files changed, 70 insertions(+), 11 deletions(-) diff --git a/examples/struct_name_as_context.rs b/examples/struct_name_as_context.rs index e8b69e1..94a3d52 100644 --- a/examples/struct_name_as_context.rs +++ b/examples/struct_name_as_context.rs @@ -17,7 +17,8 @@ fn main() { .element( Snippet::source(source) .path("$DIR/struct_name_as_context.rs") - .annotation(AnnotationKind::Primary.span(91..102)), + .annotation(AnnotationKind::Primary.span(91..102)) + .annotation(AnnotationKind::Visible.span(0..8)), ) .element( Level::HELP.message( diff --git a/examples/struct_name_as_context.svg b/examples/struct_name_as_context.svg index 9002d40..177fbba 100644 --- a/examples/struct_name_as_context.svg +++ b/examples/struct_name_as_context.svg @@ -1,4 +1,4 @@ - +