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
cmd/gofmt: inconsistent formatting of struct with comments #29784
Comments
This is indeed working as intended, although gofmt's behavior is not documented in detail. See #26352 (comment), for example. Perhaps the documentation somewhere could be improved, such as https://golang.org/cmd/gofmt/. /cc @griesemer |
Thank you :) Just to give my humble opinion.. it makes the code less readable. See for example: https://play.golang.org/p/T9lxMNkITmz But I can see the reasoning behind breaking it in "logical blocks" and then formatting those. Hence why I thought it might have been by design ;-) |
Indeed, not being able to break down the formatting into blocks would be much worse than what we currently have. One could argue that only empty lines should break alignment, and not comments alone, but I wonder if that could be changed at this point. I'll leave that to Robert. |
Sometimes in my code, when I have large structs, I comment in a separate line to group fields, which creates a new "logical block" of fields. @DylanMeeus - If you just remove the empty
|
@agnivade I think what we could potentially do, which is what I tried to explain above, is keep the alignment in the scenario where the lines in between fields aren't truly empty:
Not saying it's definitely a good idea, but it doesn't look unreasonable to me. |
For what it's worth, I think that's a good idea @mvdan. How I stumbled on this bug was that I (temporarily) wanted to remove something from a struct, though all fields did logically belong together. But then it grouped the imports because of the commented-out field :-) Sometimes you'd want to add comments without declaring a new 'logical block', so it makes sense to keep them together. And other times you'd want to split them up. What @agnivade suggested is a good work-around and keeps the readability of the struct. Though it'd be nice to also have the option to keep all fields as one block, even with comments. |
You've correctly diagnosed what gofmt does, but that's the intended design. Whole-line // comments introduce new blocks. At this point it is better not to make changes to the algorithm. It is what it is. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I created a struct that has some comments in it. For example:
Next I hit format. And it formats each comment section as if they are of different blocks.
Just click on format here:
https://play.golang.org/p/wFOqUymxaBY
What did you expect to see?
That all the types would be aligned throughout the entire struct.
What did you see instead?
Alignment in blocks divided by comments.
PS: Maybe this is intentionally so. I couldn't find a related issue with a quick search.
Thanks for taking a look!
~Dylan
The text was updated successfully, but these errors were encountered: