Skip to content

annotation sometimes points at the line above the correct line #177

Closed as duplicate of#176
@BurntSushi

Description

@BurntSushi

Here is a repro program (note the assert to make it clear where the span is pointing):

use annotate_snippets::{Level, Renderer, Snippet};

fn main() {
    let source = "    \"\"\"Summary.\n\nDescription.\n\n    \"\"\"\n";
    assert_eq!(&source[17..], "Description.\n\n    \"\"\"\n");
    let src_annotation = Level::Error.span(17..17).label("D207");
    let snippet =
        Snippet::source(source).line_start(230).annotation(src_annotation);
    let message = Level::Error.title("").snippet(snippet);

    println!("{}", Renderer::plain().render(message));
}

The actual output:

error
    |
230 |     """Summary.
231 |
    | ^ D207
232 | Description.
233 |
234 |     """
    |

And the expected output:

error
    |
230 |     """Summary.
231 |
232 | Description.
    | ^ D207
233 |
234 |     """
    |

In annotate-snippets 0.9.2, the rendering targeted the right line, but the ^ is missing. Here's a repro program for that:

use annotate_snippets::{
    display_list::{DisplayList, FormatOptions},
    snippet::{AnnotationType, Slice, Snippet, SourceAnnotation},
};

fn main() {
    let source = "    \"\"\"Summary.\n\nDescription.\n\n    \"\"\"\n";
    let snippet = Snippet {
        title: None,
        footer: vec![],
        slices: vec![Slice {
            source,
            line_start: 230,
            origin: None,
            annotations: vec![SourceAnnotation {
                range: (17, 17),
                label: "D207",
                annotation_type: AnnotationType::Error,
            }],
            fold: false,
        }],
        opt: FormatOptions {
            color: false,
            anonymized_line_numbers: false,
            margin: None,
        },
    };
    println!("{message}", message = DisplayList::from(snippet));
}

And the output:

    |
230 |     """Summary.
231 |
232 | Description.
    |  D207
233 |
234 |     """
    |

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions