mvdan / gofumpt

A stricter gofmt
https://pkg.go.dev/mvdan.cc/gofumpt
BSD 3-Clause "New" or "Revised" License
3.15k stars 110 forks source link

make breaking long lines manually easier by reading off of trailing commas #279

Open effinsky opened 11 months ago

effinsky commented 11 months ago

Hi, I realize there's been some talk of making gofumpt take care of breaking long lines, but that's not gone anywhere, I don't think. I'd like to ask about something related, but a different approach.

Inspired by formatters for Zig and by Black (for Python), I'd like to suggest including some logic that would latch onto a presence/absence of a trailing comma in a param/arg/field list to break a def or decl automatically. I think we have nothing like this on the fmt level now except for Golines which (according to my colleagues, at least) takes away too much control from our code formatting. So here's how that would work:

If you write one of the above, say a func sig, pre-save, this way (mind the trailing comma):

func TooLong(longParamName string, longParamName2 string, longParamName3 string,) string {
}

and then hit save (or just fmt), then if gets broken down into:

func TooLong(
    longParamName string, 
    longParamName2 string, 
    longParamName3 string,
) string {
}

But if you omit the trailing comma to begin with and then save/fmt, it does not get broken down.

Analogously, if you have a def etc that's split into several lines like so (mind the trailing comma now removed, we are pre-save/fmt):

func TooLong(
    longParamName string, 
    longParamName2 string, 
    longParamName3 string
) string {
}

and now you hit save/fmt, the lines get joined into a single-line (notably, trailing comma now missing post-join as well):

func TooLong(longParamName string, longParamName2 string, longParamName3 string) string {
}

Would you consider adding this kind of a feature? I am sold on it, frankly -- it gives you control over the line length, but also lets you easily break lines down and bring them back together quickly with the assistance of the fmtr.

All the best!