Skip to content

Line not displayed when first span character is a line feed #130

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

Closed
Volham22 opened this issue Jun 13, 2024 · 1 comment
Closed

Line not displayed when first span character is a line feed #130

Volham22 opened this issue Jun 13, 2024 · 1 comment

Comments

@Volham22
Copy link

Volham22 commented Jun 13, 2024

Hello,

Thanks for all your work on this project!

Problem

I recently discovered an issue while using annotate_snippets version 0.11.3 with the following code:

    let message = Level::Error.title("dummy").snippet(
        Snippet::source(
            r"foo
bar
baz
            ",
        )
        .fold(true)
        .annotation(Level::Error.span(4..11)),
    );
    let renderer = Renderer::styled();
    anstream::println!("{}", renderer.render(message));

test.txt

foo
bar
baz

The problem is that when a span starts with a line feed (\n), the entire following line isn't annotated and is sort of ignored. We can clearly see in the output below that the annotation is somehow truncated. While this isn't a blocking problem for us, we wanted to share our finding as this looks like a bug.

Actual output

This code produces the following output:

$ cargo r
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/error_message_tests`
error: dummy
  |
3 | | baz
  | |___^
  |

Expected output

We can get the following output by moving the annotation's span to the previous byte (3..11).

$ cargo r
   Compiling error_message_tests v0.1.0 (/home/machu/work/error_message_tests)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.18s
     Running `target/debug/error_message_tests`
error: dummy
  |
1 |   foo
  |  ____^
2 | | bar
3 | | baz
  | |___^
  |

Thanks for your time!

@Muscraft
Copy link
Member

This was fixed in #131, but I would like to note the output is a little different than you had expected:

#[test]
fn issue_130() {
    let input = Level::Error.title("dummy").snippet(
        Snippet::source("foo\nbar\nbaz")
            .origin("file/path")
            .line_start(3)
            .fold(true)
            .annotation(Level::Error.span(4..11)), // bar\nbaz
    );

    let expected = str![[r#"
error: dummy
 --> file/path:4:1
  |
4 | / bar
5 | | baz
  | |___^
  |
"#]];
    let renderer = Renderer::plain();
    assert_data_eq!(renderer.render(input).to_string(), expected);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants