Skip to content

Commit

Permalink
Collapse empty call expressions (#1308)
Browse files Browse the repository at this point in the history
* Collapse empty call expressions

* Unset ForceMultiLine in rewrite.go so parse tree doesn't change when formatting
  • Loading branch information
snady authored Jan 21, 2025
1 parent 3648bec commit 50e9b3f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions build/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,10 @@ func (p *printer) useCompactMode(start *Position, list *[]Expr, end *End, mode s
if mode == modeSeq {
return true
}
// Use compact mode for empty call expressions if ForceMultiLine is not set
if mode == modeCall && len(*list) == 0 && !forceMultiLine {
return true
}

// In the Default and .bzl printing modes try to keep the original printing style.
// Non-top-level statements and lists of arguments of a function definition
Expand Down
15 changes: 15 additions & 0 deletions build/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ var rewrites = []struct {
{"reorderarguments", reorderArguments, scopeBoth},
{"editoctal", editOctals, scopeBoth},
{"editfloat", editFloats, scopeBoth},
{"collapseEmpty", collapseEmpty, scopeBoth},
}

// leaveAlone reports whether any of the nodes on the stack are marked
Expand Down Expand Up @@ -1459,3 +1460,17 @@ func removeParens(f *File, _ *Rewriter) {

Edit(f, simplify)
}

// collapseEmpty unsets ForceMultiLine for empty call expressions.
func collapseEmpty(f *File, _ *Rewriter) {
Walk(f, func(expr Expr, stack []Expr) {
c, ok := expr.(*CallExpr)
if !ok {
return
}

if len(c.List) == 0 { // No arguments
c.ForceMultiLine = false
}
})
}
18 changes: 18 additions & 0 deletions build/testdata/076.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Test collapse empty call expressions

func()

func()

# before comment
func()

func() # after comment

func(
# line comment
)

func(
name = "not_empty",
)
22 changes: 22 additions & 0 deletions build/testdata/076.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Test collapse empty call expressions

func(
)

func(
)

# before comment
func(
)

func(
) # after comment

func(
# line comment
)

func(
name = "not_empty",
)

0 comments on commit 50e9b3f

Please sign in to comment.