Skip to content

Commit 7dabb82

Browse files
Oiyoomvdan
authored andcommitted
force multiline function signatures to separate the body
closes #73
1 parent 0ab4b7a commit 7dabb82

File tree

3 files changed

+82
-13
lines changed

3 files changed

+82
-13
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ No empty lines at the beginning or end of a function
4040

4141
```
4242
func foo() {
43+
4344
println("bar")
4445
4546
}
@@ -53,6 +54,28 @@ func foo() {
5354

5455
</details>
5556

57+
Functions using an empty line for readability should use a `) {` line instead
58+
59+
<details><summary><i>example</i></summary>
60+
61+
```
62+
func foo(s string,
63+
i int) {
64+
65+
println("bar")
66+
}
67+
```
68+
69+
```
70+
func foo(s string,
71+
i int,
72+
) {
73+
println("bar")
74+
}
75+
```
76+
77+
</details>
78+
5679
No empty lines around a lone statement (or comment) in a block
5780

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

format/format.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -542,12 +542,22 @@ func (f *fumpter) applyPre(c *astutil.Cursor) {
542542
f.Position(sign.Results.Closing).Column == 1 &&
543543
f.Line(sign.Results.Closing) == endLine
544544

545-
if f.Line(sign.Pos()) != endLine &&
546-
// param/result closing is not the 1st char of the left bracket line
547-
!(paramClosingIsFirstCharOnEndLine || resultClosingIsFirstCharOnEndLine) {
545+
endLineIsIndented := !(paramClosingIsFirstCharOnEndLine || resultClosingIsFirstCharOnEndLine)
546+
547+
if f.Line(sign.Pos()) != endLine && endLineIsIndented {
548+
// is there an empty line?
549+
isThereAnEmptyLine := endLine+1 != f.Line(bodyPos)
550+
548551
// The body is preceded by a multi-line function
549-
// signature, and the empty line helps readability.
550-
return
552+
// signature, we move the `) {` to avoid the empty line.
553+
switch {
554+
case isThereAnEmptyLine && sign.Results != nil && !resultClosingIsFirstCharOnEndLine:
555+
sign.Results.Closing += 1
556+
f.addNewline(sign.Results.Closing)
557+
case isThereAnEmptyLine && sign.Params != nil && !paramClosingIsFirstCharOnEndLine:
558+
sign.Params.Closing += 1
559+
f.addNewline(sign.Params.Closing)
560+
}
551561
}
552562
}
553563

testdata/scripts/func-newlines.txt

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,25 @@ func f2(
230230

231231
return "", nil
232232
}
233+
234+
func multilineResultsMultipleEmptyLines() (p1 string,
235+
p2 string) {
236+
237+
238+
println("body")
239+
240+
}
241+
242+
func multilineParamsWithoutEmptyLine(p1 string,
243+
p2 string) {
244+
println("body")
245+
}
246+
247+
func multilineParamsWithoutEmptyLineWithComment(p1 string,
248+
p2 string) {
249+
// comment
250+
println("body")
251+
}
233252
-- foo.go.golden --
234253
package p
235254

@@ -253,8 +272,8 @@ func _() {
253272
}
254273

255274
func multilineParams(p1 string,
256-
p2 string) {
257-
275+
p2 string,
276+
) {
258277
println("body")
259278
}
260279

@@ -380,8 +399,8 @@ func multilineParamsOneParamReturningMultiLineValues(
380399
}
381400

382401
func multilineResults() (p1 string,
383-
p2 string) {
384-
402+
p2 string,
403+
) {
385404
println("body")
386405
}
387406

@@ -398,8 +417,8 @@ func multilineNoFields() {
398417
func f(
399418
foo int,
400419
bar string,
401-
/* baz */) {
402-
420+
/* baz */
421+
) {
403422
body()
404423
}
405424

@@ -409,7 +428,24 @@ func f2(
409428
) (
410429
string,
411430
error,
412-
/* baz */) {
413-
431+
/* baz */
432+
) {
414433
return "", nil
415434
}
435+
436+
func multilineResultsMultipleEmptyLines() (p1 string,
437+
p2 string,
438+
) {
439+
println("body")
440+
}
441+
442+
func multilineParamsWithoutEmptyLine(p1 string,
443+
p2 string) {
444+
println("body")
445+
}
446+
447+
func multilineParamsWithoutEmptyLineWithComment(p1 string,
448+
p2 string) {
449+
// comment
450+
println("body")
451+
}

0 commit comments

Comments
 (0)