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

Maintain Empty Line information in AST #566

Closed
braydonk opened this issue Dec 2, 2024 · 2 comments
Closed

Maintain Empty Line information in AST #566

braydonk opened this issue Dec 2, 2024 · 2 comments

Comments

@braydonk
Copy link
Contributor

braydonk commented Dec 2, 2024

Is your feature request related to a problem? Please describe.
Since YAML parsers are not required to maintain empty line information, no available YAML library is capable of parsing YAML and reproducing the exact same line break information.

To explain what I mean, the following YAML in:

a: 1

b: 2

Coming back out would look like this:

a: 1
b: 2

This is because the parser will toss away the empty lines, since they are not vital syntax information and are only for readability.

Describe the solution you'd like
A solution similar to the WithComments option would be really great to have this.

Describe alternatives you've considered
In my tool yamlfmt I have a hack that works 75% of the time, which is to add a magic comment line to every empty line break before sending it into the parser, and replacing that magic line with linebreaks on the way out. https://github.com/google/yamlfmt/blob/1bfe5e4a8cf49bf0cdc3e77aa2ffbfb46a1bc812/internal/hotfix/retain_line_break.go#L29

This works a decent amount of the time, but causes strange errors in weird edge cases, and YAML is essentially a language of weird edge cases. 🥲

Additional context
I have tried to implement this myself in my fork of yaml.v3 but it has proven to be infeasible. I also tried in this library but had no luck. That's why I decided to make an issue to see if anyone has ideas for whether this is feasible. If it is, I'm willing to do any implementation work.

@goccy
Copy link
Owner

goccy commented Dec 2, 2024

@braydonk This PR seems to be related. The functionality to preserve empty lines is already implemented, but did it not work as expected?

#412

@braydonk
Copy link
Contributor Author

braydonk commented Dec 4, 2024

Thanks @goccy I didn't notice that! I followed the lineage of that PR to see how they were using the library, and when I do the same thing the line breaks are maintained as expected. Thank you!

@braydonk braydonk closed this as completed Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants