Skip to content

Commit

Permalink
mixed function's signatures interpreted incorrectly
Browse files Browse the repository at this point in the history
fixes #143
  • Loading branch information
Oiyoo authored and mvdan committed Oct 17, 2021
1 parent f3595ed commit 0ab4b7a
Show file tree
Hide file tree
Showing 2 changed files with 308 additions and 7 deletions.
19 changes: 12 additions & 7 deletions format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,14 +532,19 @@ func (f *fumpter) applyPre(c *astutil.Cursor) {
return
}
if sign != nil {
var lastParam *ast.Field
if l := sign.Results; l != nil && len(l.List) > 0 {
lastParam = l.List[len(l.List)-1]
} else if l := sign.Params; l != nil && len(l.List) > 0 {
lastParam = l.List[len(l.List)-1]
}
endLine := f.Line(sign.End())
if lastParam != nil && f.Line(sign.Pos()) != endLine && f.Line(lastParam.Pos()) == endLine {

paramClosingIsFirstCharOnEndLine := sign.Params != nil &&
f.Position(sign.Params.Closing).Column == 1 &&
f.Line(sign.Params.Closing) == endLine

resultClosingIsFirstCharOnEndLine := sign.Results != nil &&
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) {
// The body is preceded by a multi-line function
// signature, and the empty line helps readability.
return
Expand Down
296 changes: 296 additions & 0 deletions testdata/scripts/func-newlines.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,149 @@ func multilineParamsUnambiguous(p1 string,

}

func multilineParamsListNoReturn(
p1 string,
p2 string,
) {

println("body")

}

func multilineParamsListReturningNamedSingleValue(
p1 string,
p2 string,
) (err error) {

println("body")
return err

}

func multilineParamsListReturningSingleValue(
p1 string,
p2 string,
) error {

println("body")
return nil

}

func multilineParamsListReturningNamedMultiValues(
p1 string,
p2 string,
) (s string, err error) {

println("body")
return s, err

}

func multilineParamsListReturningMultiValues(
p1 string,
p2 string,
) (string, error) {

println("body")
return "", nil

}

func multilineParamsListReturningNamedMultiLineValuesList(
p1 string,
p2 string,
) (
s string,
err error,
) {

println("body")
return s, err

}

func multilineParamsListReturningMultiLineValues(
p1 string,
p2 string,
) (
string,
error,
) {

println("body")
return "", nil

}

func multilineParamsOneParamNoReturn(
p1 string,
) {

println("body")

}

func multilineParamsOneParamReturningNamedSingleValue(
p1 string,
) (err error) {

println("body")
return err

}

func multilineParamsOneParamReturningSingleValue(
p1 string,
) error {

println("body")
return nil

}

func multilineParamsOneParamReturningNamedMultiValues(
p1 string,
) (s string, err error) {

println("body")
return s, err

}

func multilineParamsOneParamReturningMultiValues(
p1 string,
) (string, error) {

println("body")
return "", nil

}

func multilineParamsOneParamReturningNamedMultiLineValuesList(
p1 string,
) (
s string,
err error,
) {

println("body")
return s, err

}

func multilineParamsOneParamReturningMultiLineValues(
p1 string,
) (
string,
error,
) {

println("body")
return "", nil

}

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

Expand All @@ -68,6 +211,25 @@ func multilineNoFields(
println("body")

}

func f(
foo int,
bar string,
/* baz */) {

body()
}

func f2(
foo int,
bar string,
) (
string,
error,
/* baz */) {

return "", nil
}
-- foo.go.golden --
package p

Expand Down Expand Up @@ -102,6 +264,121 @@ func multilineParamsUnambiguous(p1 string,
println("body")
}

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

func multilineParamsListReturningNamedSingleValue(
p1 string,
p2 string,
) (err error) {
println("body")
return err
}

func multilineParamsListReturningSingleValue(
p1 string,
p2 string,
) error {
println("body")
return nil
}

func multilineParamsListReturningNamedMultiValues(
p1 string,
p2 string,
) (s string, err error) {
println("body")
return s, err
}

func multilineParamsListReturningMultiValues(
p1 string,
p2 string,
) (string, error) {
println("body")
return "", nil
}

func multilineParamsListReturningNamedMultiLineValuesList(
p1 string,
p2 string,
) (
s string,
err error,
) {
println("body")
return s, err
}

func multilineParamsListReturningMultiLineValues(
p1 string,
p2 string,
) (
string,
error,
) {
println("body")
return "", nil
}

func multilineParamsOneParamNoReturn(
p1 string,
) {
println("body")
}

func multilineParamsOneParamReturningNamedSingleValue(
p1 string,
) (err error) {
println("body")
return err
}

func multilineParamsOneParamReturningSingleValue(
p1 string,
) error {
println("body")
return nil
}

func multilineParamsOneParamReturningNamedMultiValues(
p1 string,
) (s string, err error) {
println("body")
return s, err
}

func multilineParamsOneParamReturningMultiValues(
p1 string,
) (string, error) {
println("body")
return "", nil
}

func multilineParamsOneParamReturningNamedMultiLineValuesList(
p1 string,
) (
s string,
err error,
) {
println("body")
return s, err
}

func multilineParamsOneParamReturningMultiLineValues(
p1 string,
) (
string,
error,
) {
println("body")
return "", nil
}

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

Expand All @@ -117,3 +394,22 @@ func multilineResultsUnambiguous() (p1 string,
func multilineNoFields() {
println("body")
}

func f(
foo int,
bar string,
/* baz */) {

body()
}

func f2(
foo int,
bar string,
) (
string,
error,
/* baz */) {

return "", nil
}

0 comments on commit 0ab4b7a

Please sign in to comment.