Skip to content

Commit

Permalink
force multiline function signatures to separate the body
Browse files Browse the repository at this point in the history
closes #73
  • Loading branch information
Oiyoo authored and mvdan committed Oct 26, 2021
1 parent 0ab4b7a commit 7dabb82
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 13 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ No empty lines at the beginning or end of a function

```
func foo() {
println("bar")
}
Expand All @@ -53,6 +54,28 @@ func foo() {

</details>

Functions using an empty line for readability should use a `) {` line instead

<details><summary><i>example</i></summary>

```
func foo(s string,
i int) {
println("bar")
}
```

```
func foo(s string,
i int,
) {
println("bar")
}
```

</details>

No empty lines around a lone statement (or comment) in a block

<details><summary><i>example</i></summary>
Expand Down
20 changes: 15 additions & 5 deletions format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,12 +542,22 @@ func (f *fumpter) applyPre(c *astutil.Cursor) {
f.Position(sign.Results.Closing).Column == 1 &&
f.Line(sign.Results.Closing) == endLine

if f.Line(sign.Pos()) != endLine &&
// param/result closing is not the 1st char of the left bracket line
!(paramClosingIsFirstCharOnEndLine || resultClosingIsFirstCharOnEndLine) {
endLineIsIndented := !(paramClosingIsFirstCharOnEndLine || resultClosingIsFirstCharOnEndLine)

if f.Line(sign.Pos()) != endLine && endLineIsIndented {
// is there an empty line?
isThereAnEmptyLine := endLine+1 != f.Line(bodyPos)

// The body is preceded by a multi-line function
// signature, and the empty line helps readability.
return
// signature, we move the `) {` to avoid the empty line.
switch {
case isThereAnEmptyLine && sign.Results != nil && !resultClosingIsFirstCharOnEndLine:
sign.Results.Closing += 1
f.addNewline(sign.Results.Closing)
case isThereAnEmptyLine && sign.Params != nil && !paramClosingIsFirstCharOnEndLine:
sign.Params.Closing += 1
f.addNewline(sign.Params.Closing)
}
}
}

Expand Down
52 changes: 44 additions & 8 deletions testdata/scripts/func-newlines.txt
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,25 @@ func f2(

return "", nil
}

func multilineResultsMultipleEmptyLines() (p1 string,
p2 string) {


println("body")

}

func multilineParamsWithoutEmptyLine(p1 string,
p2 string) {
println("body")
}

func multilineParamsWithoutEmptyLineWithComment(p1 string,
p2 string) {
// comment
println("body")
}
-- foo.go.golden --
package p

Expand All @@ -253,8 +272,8 @@ func _() {
}

func multilineParams(p1 string,
p2 string) {

p2 string,
) {
println("body")
}

Expand Down Expand Up @@ -380,8 +399,8 @@ func multilineParamsOneParamReturningMultiLineValues(
}

func multilineResults() (p1 string,
p2 string) {

p2 string,
) {
println("body")
}

Expand All @@ -398,8 +417,8 @@ func multilineNoFields() {
func f(
foo int,
bar string,
/* baz */) {

/* baz */
) {
body()
}

Expand All @@ -409,7 +428,24 @@ func f2(
) (
string,
error,
/* baz */) {

/* baz */
) {
return "", nil
}

func multilineResultsMultipleEmptyLines() (p1 string,
p2 string,
) {
println("body")
}

func multilineParamsWithoutEmptyLine(p1 string,
p2 string) {
println("body")
}

func multilineParamsWithoutEmptyLineWithComment(p1 string,
p2 string) {
// comment
println("body")
}

0 comments on commit 7dabb82

Please sign in to comment.