annotate-snippets-rs icon indicating copy to clipboard operation
annotate-snippets-rs copied to clipboard

Unaligned display when one line source given

Open tisonkun opened this issue 2 years ago • 3 comments

Result:

 |
0 | SELECT bar
 |        ^^^ unexpected token
 | ^^^^^^^^^^ while parsing statement
 |

As you can see the | is not aligned.

Code:

pub(crate) fn display_annotated_error(source: &str, labels: Vec<(Range, String)>) -> String {
    let annotations = labels
        .iter()
        .map(|label| SourceAnnotation {
            range: (label.0.start, label.0.end),
            label: label.1.as_str(),
            annotation_type: AnnotationType::Error,
        })
        .collect::<Vec<_>>();

    let snippet = Snippet {
        title: None,
        footer: vec![],
        slices: vec![Slice {
            source,
            line_start: 0,
            origin: None,
            annotations,
            fold: false,
        }],
        opt: FormatOptions {
            color: true,
            ..Default::default()
        },
    };

    format!("{}", DisplayList::from(snippet))
}

display_annotated_error("SELECT bar", labels)
// where labels = [((7, 10), unexpected token), ((0, 10), while parsing statement)],

tisonkun avatar Jun 23 '23 03:06 tisonkun

... while change source to SELECT \nbar gives a proper formatted:

  |
0 | / SELECT 
1 |   bar
  | | ^^^ unexpected token
  | |___^ while parsing statement
  |

tisonkun avatar Jun 23 '23 03:06 tisonkun

Change line_start to 1 also fix the issue. Perhaps it's a bug for line_start == 0.

tisonkun avatar Jun 23 '23 04:06 tisonkun