gofumpt icon indicating copy to clipboard operation
gofumpt copied to clipboard

consider breaking lines before ) in multi-line calls

Open kovetskiy opened this issue 4 years ago • 4 comments

gofumpt doesn't enforce any styles related to closing ) in multi-line calls:

	x := call(
		a,
		b,
		c,
	)

	x := call(
		a,
		b,
		c)

It would be great if gofumpt would format these calls to the first option (or at least one of them, but I personally prefer the first one because it makes it clear that the next line should go on the same indentation level)

kovetskiy avatar Jun 12 '20 11:06 kovetskiy

Sounds good. Let's make this rule a bit conservative for now, though; if we make all multi-line argument lists use a separate closing parentheses, we would rewrite

x := call(a, b,
    c, d)

into one of

x := call(
    a, b,
    c, d,
)

x := call(
    a,
    b,
    c,
    d,
)

You could argue we want to do something like that in the long run, but it seems like less of a clear case. For example, we could only apply the rule to place ) at the start of a line when ( is at the end of a line as well.

mvdan avatar Jun 12 '20 14:06 mvdan

I'd hope for at least the opening/closing newline behaviour of "Composite literals should use newlines consistently":

// A newline before or after an element requires newlines for the opening and
// closing braces.
var ints = []int{1, 2,
	3, 4}
// ->
var ints = []int{
	1, 2,
	3, 4,
}

icio avatar Mar 03 '21 11:03 icio

@icio the plan is to only change the code if the opening parentheses is at the end of a line, or if the closing parenthesis is at the start of a line. Those cases are pretty clearly inconsistent.

What you suggest, which is what I mentioned in my last comment, would also trigger the rule if there are any newlines between arguments. I think that makes sense for composite literals, but I'm not convinced it always makes sense for calls. I'm still leaving that idea as a "maybe" for the future, but for now I want to add the more conservative rule that's a clear win.

mvdan avatar Mar 03 '21 12:03 mvdan

There is also https://github.com/segmentio/golines which does exactly one thing — splits long lines. It perfectly fits with gofumpt at the same time.

kovetskiy avatar Mar 04 '21 04:03 kovetskiy