diff --git a/README.md b/README.md index 3c44c66..509d4e7 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ No empty lines at the beginning or end of a function ``` func foo() { + println("bar") } @@ -53,6 +54,28 @@ func foo() { +Functions using an empty line for readability should use a `) {` line instead + +
example + +``` +func foo(s string, + i int) { + + println("bar") +} +``` + +``` +func foo(s string, + i int, +) { + println("bar") +} +``` + +
+ No empty lines around a lone statement (or comment) in a block
example diff --git a/format/format.go b/format/format.go index e1817d9..e225286 100644 --- a/format/format.go +++ b/format/format.go @@ -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) + } } } diff --git a/testdata/scripts/func-newlines.txt b/testdata/scripts/func-newlines.txt index ea168e9..1b15615 100644 --- a/testdata/scripts/func-newlines.txt +++ b/testdata/scripts/func-newlines.txt @@ -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 @@ -253,8 +272,8 @@ func _() { } func multilineParams(p1 string, - p2 string) { - + p2 string, +) { println("body") } @@ -380,8 +399,8 @@ func multilineParamsOneParamReturningMultiLineValues( } func multilineResults() (p1 string, - p2 string) { - + p2 string, +) { println("body") } @@ -398,8 +417,8 @@ func multilineNoFields() { func f( foo int, bar string, - /* baz */) { - + /* baz */ +) { body() } @@ -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") +}