Skip to content

Commit

Permalink
make "split long lines" idempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdan committed May 13, 2020
1 parent 572ac90 commit 8be3105
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 43 deletions.
85 changes: 46 additions & 39 deletions format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func File(fset *token.FileSet, file *ast.File, goVersion string) {
return true
}
post := func(c *astutil.Cursor) bool {
f.applyPost(c)
switch c.Node().(type) {
case *ast.FieldList:
f.longLineExtra = 0
Expand Down Expand Up @@ -257,7 +258,6 @@ func (f *fumpter) lineEnd(line int) token.Pos {
// //sys(nb)? | syscall function wrapper prototypes
var rxCommentDirective = regexp.MustCompile(`^([a-z]+:|line\b|export\b|extern\b|sys(nb)?\b)`)

// visit takes either an ast.Node or a []ast.Stmt.
func (f *fumpter) applyPre(c *astutil.Cursor) {
f.splitLongLine(c)

Expand Down Expand Up @@ -429,6 +429,51 @@ func (f *fumpter) applyPre(c *astutil.Cursor) {
f.removeLinesBetween(node.Lbrace, bodyPos)
f.removeLinesBetween(bodyEnd, node.Rbrace)

case *ast.CaseClause:
f.stmts(node.Body)
openLine := f.Line(node.Case)
closeLine := f.Line(node.Colon)
if openLine == closeLine {
// nothing to do
break
}
if len(f.commentsBetween(node.Case, node.Colon)) > 0 {
// don't move comments
break
}
if f.printLength(node) > shortLineLimit {
// too long to collapse
break
}
f.removeLines(openLine, closeLine)

case *ast.CommClause:
f.stmts(node.Body)

case *ast.FieldList:
switch c.Parent().(type) {
case *ast.FuncDecl, *ast.FuncType, *ast.InterfaceType:
node.List = f.mergeAdjacentFields(node.List)
c.Replace(node)
case *ast.StructType:
// Do not merge adjacent fields in structs.
}

case *ast.BasicLit:
if semver.Compare(f.goVersion, "v1.13") >= 0 {
if node.Kind == token.INT && rxOctalInteger.MatchString(node.Value) {
node.Value = "0o" + node.Value[1:]
c.Replace(node)
}
}
}
}

func (f *fumpter) applyPost(c *astutil.Cursor) {
switch node := c.Node().(type) {
// Adding newlines to composite literals happens as a "post" step, so
// that we can take into account whether "pre" steps added any newlines
// that would affect us here.
case *ast.CompositeLit:
if len(node.Elts) == 0 {
// doesn't have elements
Expand Down Expand Up @@ -493,44 +538,6 @@ func (f *fumpter) applyPre(c *astutil.Cursor) {
f.addNewline(elem1.End())
}
}

case *ast.CaseClause:
f.stmts(node.Body)
openLine := f.Line(node.Case)
closeLine := f.Line(node.Colon)
if openLine == closeLine {
// nothing to do
break
}
if len(f.commentsBetween(node.Case, node.Colon)) > 0 {
// don't move comments
break
}
if f.printLength(node) > shortLineLimit {
// too long to collapse
break
}
f.removeLines(openLine, closeLine)

case *ast.CommClause:
f.stmts(node.Body)

case *ast.FieldList:
switch c.Parent().(type) {
case *ast.FuncDecl, *ast.FuncType, *ast.InterfaceType:
node.List = f.mergeAdjacentFields(node.List)
c.Replace(node)
case *ast.StructType:
// Do not merge adjacent fields in structs.
}

case *ast.BasicLit:
if semver.Compare(f.goVersion, "v1.13") >= 0 {
if node.Kind == token.INT && rxOctalInteger.MatchString(node.Value) {
node.Value = "0o" + node.Value[1:]
c.Replace(node)
}
}
}
}

Expand Down
14 changes: 10 additions & 4 deletions testdata/scripts/long-lines.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
gofumpt -w .
gofumpt -w foo.go
cmp foo.go foo.go.golden

gofumpt -d foo.go.golden
! stdout .

-- foo.go --
package p

Expand Down Expand Up @@ -82,15 +85,18 @@ func _() {

// Allow splitting at the start of sub-lists too.
if err := f(argument1, argument2, argument3, argument4, argument5, argument6, someComplex{
argument7, argument8, argument9}); err != nil {
argument7, argument8, argument9,
}); err != nil {
panic(err)
}
if err := f(argument1, argument2, argument3, argument4, argument5, argument6, &someComplex{
argument7, argument8, argument9}); err != nil {
argument7, argument8, argument9,
}); err != nil {
panic(err)
}
if err := f(argument1, argument2, argument3, argument4, argument5, argument6, []someSlice{
argument7, argument8, argument9}); err != nil {
argument7, argument8, argument9,
}); err != nil {
panic(err)
}
}
Expand Down

0 comments on commit 8be3105

Please sign in to comment.