Skip to content
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

Unaligned display when one line source given #57

Open
tisonkun opened this issue Jun 23, 2023 · 3 comments · May be fixed by #142
Open

Unaligned display when one line source given #57

tisonkun opened this issue Jun 23, 2023 · 3 comments · May be fixed by #142
Labels
C-bug Category: bug

Comments

@tisonkun
Copy link

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
Copy link
Author

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

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

@tisonkun
Copy link
Author

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

@epage epage added the C-bug Category: bug label Mar 13, 2024
chengr4 added a commit to chengr4/annotate-snippets-rs that referenced this issue Aug 20, 2024
@chengr4 chengr4 linked a pull request Aug 20, 2024 that will close this issue
@chengr4
Copy link

chengr4 commented Aug 21, 2024

I wrote down what I found, even if my solution isn't accepted.

What I Found

I changed line_start(13) to 0 and 1 in examples/footer.rs to reproduce the issue.

line_start(1):

Screenshot 2024-08-21 at 2 20 47 PM

line_start(0):

Screenshot 2024-08-21 at 2 20 20 PM

I do not know the term we use to describe "space", so I use "col".

Upon closer comparison, you can see that the message for line_start(0) starts at col 1 and the message for line_start(1) starts at col 2.

According to my research, the number of spaces to insert at the beginning of each line is based on the line number width.

My Idea

No matter what the line number width is, we always start message from col 2 "at a minimum", which is what is #142 for.

cc @epage

chengr4 added a commit to chengr4/annotate-snippets-rs that referenced this issue Aug 21, 2024
chengr4 added a commit to chengr4/annotate-snippets-rs that referenced this issue Aug 27, 2024
chengr4 added a commit to chengr4/annotate-snippets-rs that referenced this issue Aug 30, 2024
	- Line number width is 1 if max line number is 0
	- Line number witdh is 0 if max line number is None
	- Update test_line_number_0 in tests/formatter.rs to reflect new behavior
	- Fix issue rust-lang#57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants