Skip to content

Origin line number is not correct when using a slice with fold: true #52

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
lucacasonato opened this issue Nov 14, 2021 · 3 comments · Fixed by #55
Closed

Origin line number is not correct when using a slice with fold: true #52

lucacasonato opened this issue Nov 14, 2021 · 3 comments · Fixed by #55

Comments

@lucacasonato
Copy link

Here is a test case demonstrating the issue:

#[test]
fn test_origin_line_numbers_with_fold() {
    let snippets = Snippet {
        title: Some(snippet::Annotation {
            id: None,
            label: Some("oops"),
            annotation_type: snippet::AnnotationType::Error,
        }),
        footer: vec![],
        slices: vec![snippet::Slice {
            source: "First line\r\n\r\n\r\nSecond oops line",
            line_start: 1,
            origin: Some("<current file>"),
            annotations: vec![
                snippet::SourceAnnotation {
                    range: (23, 27),
                    label: "oops",
                    annotation_type: snippet::AnnotationType::Error,
                },
                snippet::SourceAnnotation {
                    range: (0, 5),
                    label: "First",
                    annotation_type: snippet::AnnotationType::Info,
                },
            ],
            fold: true,
        }],
        opt: Default::default(),
    };
    let expected = r#"error: oops
 --> <current file>:4:8
  |
1 | First line
  | ----- info: First
...
4 | Second oops line
  |        ^^^^ oops
  |"#;

    assert_eq!(DisplayList::from(snippets).to_string(), expected);
}

--> <current file>:2:8 is printed instead of --> <current file>:4:8.

Inky-developer added a commit to Inky-developer/annotate-snippets-rs that referenced this issue Jan 3, 2022
This fixes wrong line numbers shown when the `fold` is set to `true`
@cellog
Copy link

cellog commented May 31, 2023

just ran into this bug. There is a working pull request that is a year and a half old. is annotate-snippets unmaintained?

zbraniecki added a commit that referenced this issue Jun 2, 2023
Show correct line numbers when fold ist set to true, fixes #52
@zbraniecki
Copy link
Contributor

It's not unmaintained, but I haven't had time to work on it lately

@ironcev
Copy link

ironcev commented Jul 31, 2023

Hi @zbraniecki, thanks for keep maintaining the annotate-snippets! The Sway programming language uses annotate-snippets for printing its compiler output. We are just switching from short single-slice Snippets where we didn't fold the lines, to larger multi-slice Snippets where we fold the lines and where we see this bug.

The bug is fixed, but not provided in any release. Can you please publish a new patch release with this fix? It would help us a lot.

Thanks in advance!

ironcev added a commit to FuelLabs/sway that referenced this issue Aug 2, 2023
## Description

A new `Diagnostic` type is introduced for detail description of compile
errors and warnings. The change is backward compatible. The existing
`CompileWarning`s and `CompileError`s will continue render as they had
before.

The `Diagnostic` is formed out of a:
- _Level_: Error or Warning.
- _Code_: Unique error or warning code. Placeholder for future. Not used
at the moment.
- _Reason_: Short description of the diagnostic, not related to a
specific error/warning. Answers the question "Why is this an
error/warning?" E.g., Because - "Constants cannot be shadowed".
- _Issue_: Short description of the concrete case that the compiler has
found. E.g., "Variable "X" shadows imported constant with the same name"
- _Hints_: Detailed description of the diagnostic placed in the source
code.
- _Help_: Additional friendly information that helps understanding and
solving the issue, but which is not related to a place in code.

![07C Constant shadowing - Alias - After 02 -
Terminology](https://github.com/FuelLabs/sway/assets/4142833/50c639a1-43f5-4bc2-afa2-5717652f0172)

The `Diagnostic`s are defined imperatively in code right now, pretty
much the sam way we do `CompilWarning`s at the moment. Development of a
proc-macro that should make de definitions declarative is out of scope
of #21.

## Known Limitations

The `annotate-snippets` library has a bug and a missing functionality
for which I opened issues:

- Wrong display of line numbers when using folding of lines of code.
This issue is fixed but there is no patch release provided:
rust-lang/annotate-snippets-rs#52 (comment)
- No possibility to remove the "note:" prefix as shown on the image
above: rust-lang/annotate-snippets-rs#59

These two issues are not blocking. Proposal is to wait for the official
support in the library, or contribute or in the worst case make a
workaround in our code.

## Demo (Before and After)

### Errors
![07A Constant shadowing - Alias -
Before](https://github.com/FuelLabs/sway/assets/4142833/5d67c5e5-d456-4762-b7c7-6fcf7340d6b3)

![07C Constant shadowing - Alias - After
02](https://github.com/FuelLabs/sway/assets/4142833/02691b39-3d08-4776-ba93-5ffeca4546ed)

### Warnings
![06A NonScreamingSnakeCaseConstName -
Before](https://github.com/FuelLabs/sway/assets/4142833/c27279d4-0e6c-4d05-9b00-da7121bb60ad)

![06B NonScreamingSnakeCaseConstName -
After](https://github.com/FuelLabs/sway/assets/4142833/483044f3-f190-4506-9e9e-2d03d70cb3f2)

Closes #21

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
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

Successfully merging a pull request may close this issue.

4 participants