-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: remove leading/trailing blank lines from function bodies #6996
Comments
I sometimes write block comments for functions like http://play.golang.org/p/Nsovaj5c7j. I find the leading blank line helps separate it from the code, much like I would have a leading blank line before a block comment when not at the head of a function. It also makes it easier to use my editors wrap paragraph functionality. I would be slightly to somewhat annoyed if gofmt removed the line. |
At the moment, gofmt doesn't distinguish between a function body and a block inside a function (for the purpose of these empty lines) - though it could. At least for inner blocks, sometimes it is desired to have extra empty lines as in: if ... { a lot of code here a lot of code here a lot of code here a lot of code here } else { a lot of code here a lot of code here a lot of code here a lot of code here } to highlight the structure of the if. Labels changed: added release-none, repo-main. Owner changed to @griesemer. Status changed to Thinking. |
Thoughts on this one now that some time has passed @griesemer? I am still surprised when gofmt leaves leading/trailing newlines in function bodies. It's not a huge deal but removing them seems consistent with other gofmt newline behavior. |
@calmh this reply is almost four years late, but have you considered doing:
I have seen a similar technique used in the Go tree, to have function godocs that leave an empty trailing line, yet are still attached to the function:
Although it does seem safer to start by just removing trailing empty lines. A separate issue could be opened about leading empty lines. |
Looking at my code I don't appear to write comments like that any more. This could be an effect of my editor becoming smarter at paragraph-wrapping just the comment and not the adjacent code, or my taste just evolved. Regardless, I no longer care. :) |
It would be great to make sure there is one new line between things like imports and const and top level var and functions definitions too. |
@mmwtsn Sorry, this has not been a priority. I agree it would be nice to fix this. |
Marked for 1.13 so this gets some attention. |
Cross-reference to @mvdan's https://github.com/mvdan/gofumpt that does that (among other nice things). |
Shameless plug here for my tool I have written in the past: |
Any chance of this being implemented soon? |
@mvdan for input. The Go team is very much busy with the 1.18 generics release and unlikely to have time for this in this round. |
If anyone wants this feature today, I'd suggest to look at third party tools that build on top of gofmt, like gofumpt. It's very unlikely that we'll add features to gofmt this cycle, like Robert says. As for adding this feature to gofmt in the future - I'd certainly be in favor. The heuristic we've got in gofumpt has been tweaked over the past couple of years, and we're still finding bugs in it today. I'd be happy to contribute an adapted version of that code to upstream Go if we agree that its logic is sane. It's essentially:
That is, if the function declaration ends on the same indentation level as the body, preserve one existing empty line as separation. In my opinion it's better to instead write it with an unindented line, but I think forcing the user to do that is unnecessary:
|
So is it still no solution to control auto deleting blank lines? |
The text was updated successfully, but these errors were encountered: