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

emitter: add pad_line_comment #15

Closed
wants to merge 2 commits into from
Closed

emitter: add pad_line_comment #15

wants to merge 2 commits into from

Conversation

braydonk
Copy link
Owner

@braydonk braydonk commented Apr 1, 2023

Added an option to pad line comment by the additional single space in formatted output.

Part of the work for google/yamlfmt#104

Added an option to pad line comment by the additional single space in
formatted output.
@braydonk
Copy link
Owner Author

braydonk commented Apr 1, 2023

Ah this is a bit too simple, because that spot where the comment is written doesn't distinguish what kind of comment it is.

Comment on lines -1983 to +1993
func yaml_emitter_write_comment(emitter *yaml_emitter_t, comment []byte) bool {
func yaml_emitter_write_comment(emitter *yaml_emitter_t, comment []byte, padding int) bool {
breaks := false
pound := false

// [Go] Start by adding any additional padding to the line comment.
for i := 0; i < padding; i++ {
if !put(emitter, ' ') {
return false
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to inject the extra spaces here instead of directly in yaml_emitter_process_line_comment ?
That would avoid having to provide a 0 any time yaml_emitter_write_comment is called

yaml/emitterc.go

Lines 1149 to 1153 in 875dd3d

if !emitter.whitespace {
if !put(emitter, ' ') {
return false
}
}

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main reason is that then I would have to change the logic at line 2007 which checks for the beginning of the line comment to be a #, and if it's not then it inserts # . It's sorta weird logic imo, but in general I tend to prefer stuff like this that simply adds an override to existing logic over messing with it directly. Just want to keep my involvement with the original library to a minimum.

Copy link

@badouralix badouralix Apr 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it ! My understanding of this logic is that it applies to non-line comments, since line comments always have comment[0] == "#" and never have breaks, whereas non-line comments can span over multiple lines and handling them requires this logic
Overall I wouldn't have to worry much about this logic for anything related to inserting spaces before writing the comment itself

I have been experimenting with this alternative implementation badouralix@f2c7845 and couldn't find any edge case breaking the behavior of line comments ( haven't opened the pull request though since you opened yours first )
One interesting thing is that the config is explicitly 2 to insert 2 spaces before a line comment, whereas the padding config wants 1 to insert 2 spaces
Wouldn't it be easier to keep an alignment between the config and the number of spaces rather than having a discrepancy ?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I had kept with the idea of "padding" and making it a padding of 1 space over the 1 space that automatically gets inserted. But I can see how that would be harder to explain that in documentation vs the default of 1.

That being said, I like your implementation better. I have a couple of comments, but I think overall yours is better and I'd rather accept that in favour of my version. Could you open a PR with it and add me as a reviewer?

Copy link

@badouralix badouralix Apr 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just opened #16 but it seems I cannot add you as a reviewer 🙈

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Not sure why it doesn't let you add me as a reviewer, but I just requested myself instead.

@braydonk
Copy link
Owner Author

braydonk commented Apr 2, 2023

Closing in favour of #16

@braydonk braydonk closed this Apr 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants