Skip to content

Commit

Permalink
Merge pull request #26 from cockroachdb/bugs
Browse files Browse the repository at this point in the history
Fix a few function signature formatting bugs
  • Loading branch information
benesch authored Nov 13, 2018
2 parents 6b96ff4 + 5058cd9 commit 3c56c65
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
13 changes: 9 additions & 4 deletions internal/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ func renderLineFuncField(w io.Writer, f *parser.File, param *ast.Field) {
}
fmt.Fprint(w, n.Name)
}
fmt.Fprintf(w, " %s,", f.Slice(param.Type.Pos(), param.Type.End()))
if len(param.Names) > 0 {
fmt.Fprint(w, " ")
}
fmt.Fprintf(w, "%s,", f.Slice(param.Type.Pos(), param.Type.End()))
if param.Comment != nil {
fmt.Fprintf(w, " %s", f.Slice(param.Comment.Pos(), param.Comment.End()))
}
Expand Down Expand Up @@ -150,13 +153,15 @@ func Func(w io.Writer, f *parser.File, fn *parser.FuncDecl, tabSize, wrapCol int
const paramsLineEndComma = `,`

var resultsBuf bytes.Buffer
var exactlyOneResult bool
if results != nil {
resultsPrefix := ""
for _, r := range results.List {
resultsBuf.WriteString(resultsPrefix)
resultsBuf.Write(f.Slice(r.Pos(), r.End()))
resultsPrefix = ", "
}
exactlyOneResult = len(results.List) == 1 && len(results.List[0].Names) == 0
}
resultsJoined := resultsBuf.Bytes()

Expand All @@ -165,7 +170,7 @@ func Func(w io.Writer, f *parser.File, fn *parser.FuncDecl, tabSize, wrapCol int
if results == nil || len(results.List) == 0 {
funcMid = `)`
funcEnd = ``
} else if len(results.List) == 1 && len(results.List[0].Names) == 0 {
} else if exactlyOneResult {
funcMid = `) `
funcEnd = ``
}
Expand Down Expand Up @@ -217,8 +222,8 @@ func Func(w io.Writer, f *parser.File, fn *parser.FuncDecl, tabSize, wrapCol int
}
}
fmt.Fprint(w, funcMid)
singleLineRetunsLen := resTypeStartingCol + len(funcMid) + len(resultsJoined) + len(funcEnd) + brace
if singleLineRetunsLen <= wrapCol && !resultsHaveComments {
singleLineResultsLen := resTypeStartingCol + len(funcMid) + len(resultsJoined) + len(funcEnd) + brace
if (singleLineResultsLen <= wrapCol || exactlyOneResult) && !resultsHaveComments {
w.Write(resultsJoined)
fmt.Fprint(w, funcEnd)
} else {
Expand Down
34 changes: 34 additions & 0 deletions testdata/funcsigs.in.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,46 @@ func someSigWithLongArgsAndElidedTypeShorthand(
) {
}

func someSigWithMisIndentedArgs________________________(
thereAreTwoSpacesBeforeThisArgWhenThereShouldBeJustATab string,
) (
thereAreTwoSpacesBeforeThisArgWhenThereShouldBeJustATab string,
weDefinitelyNeedAnotherLineForTheseReturnParams int,
)

func someSigWithMisIndentedArgsNoNames________________________(
func(iAmSuperLooooooooong_____________________________________________________________________),
) (
func(iAmSuperLooooooooong_____________________________________________________________________),
string,
)

func noParamsLongReturn() func(thisReturnParameterIsSoLongThatItWantsItsOwnLineButGofmtWontAllowIt int)

func shortParamsLongReturn(a int) func(thisReturnParameterIsSoLongThatItWantsItsOwnLineEvenThoughArgsAreShort int)

func longParamsLongReturn(loooooooooooooooooooooooooooooooooooooooooooooooooooooong string, long2 int) func(thisReturnParameterIsSoLongThatItWantsItsOwnLineButGofmtWontAllowIt_________________________ int)

type fooObj struct{}

func (f *fooObj) finalTxnStatsLocked() (duration, restarts int64, status int64) {
return
}

func (f *fooObj) someSigWithMisIndentedArgs________________________(
thereAreTwoSpacesBeforeThisArgWhenThereShouldBeJustATab string,
) (
thereAreTwoSpacesBeforeThisArgWhenThereShouldBeJustATab string,
weDefinitelyNeedAnotherLineForTheseReturnParams int,
)

func (f *fooObj) someSigWithMisIndentedArgsNoNames________________________(
func(iAmSuperLooooooooong_____________________________________________________________________),
) (
func(iAmSuperLooooooooong_____________________________________________________________________),
string,
)

func updateStatsOnPut(
key []byte,
origMetaKeySize, origMetaValSize,
Expand Down
38 changes: 38 additions & 0 deletions testdata/funcsigs.out.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,50 @@ func someSigWithLongArgsAndElidedTypeShorthand(
) {
}

func someSigWithMisIndentedArgs________________________(
thereAreTwoSpacesBeforeThisArgWhenThereShouldBeJustATab string,
) (
thereAreTwoSpacesBeforeThisArgWhenThereShouldBeJustATab string,
weDefinitelyNeedAnotherLineForTheseReturnParams int,
)

func someSigWithMisIndentedArgsNoNames________________________(
func(iAmSuperLooooooooong_____________________________________________________________________),
) (
func(iAmSuperLooooooooong_____________________________________________________________________),
string,
)

func noParamsLongReturn() func(thisReturnParameterIsSoLongThatItWantsItsOwnLineButGofmtWontAllowIt int)

func shortParamsLongReturn(
a int,
) func(thisReturnParameterIsSoLongThatItWantsItsOwnLineEvenThoughArgsAreShort int)

func longParamsLongReturn(
loooooooooooooooooooooooooooooooooooooooooooooooooooooong string, long2 int,
) func(thisReturnParameterIsSoLongThatItWantsItsOwnLineButGofmtWontAllowIt_________________________ int)

type fooObj struct{}

func (f *fooObj) finalTxnStatsLocked() (duration, restarts int64, status int64) {
return
}

func (f *fooObj) someSigWithMisIndentedArgs________________________(
thereAreTwoSpacesBeforeThisArgWhenThereShouldBeJustATab string,
) (
thereAreTwoSpacesBeforeThisArgWhenThereShouldBeJustATab string,
weDefinitelyNeedAnotherLineForTheseReturnParams int,
)

func (f *fooObj) someSigWithMisIndentedArgsNoNames________________________(
func(iAmSuperLooooooooong_____________________________________________________________________),
) (
func(iAmSuperLooooooooong_____________________________________________________________________),
string,
)

func updateStatsOnPut(
key []byte,
origMetaKeySize, origMetaValSize, metaKeySize, metaValSize int64,
Expand Down

0 comments on commit 3c56c65

Please sign in to comment.