Skip to content

Commit

Permalink
format: exclude body on CaseClause len check
Browse files Browse the repository at this point in the history
fixes #217
  • Loading branch information
Oiyoo authored and mvdan committed Apr 19, 2022
1 parent 6105233 commit b5619f8
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
8 changes: 7 additions & 1 deletion format/format.go
Expand Up @@ -594,7 +594,13 @@ func (f *fumpter) applyPre(c *astutil.Cursor) {
// don't move comments
break
}
if f.printLength(node) > shortLineLimit {
// check the length excluding the body
nodeWithoutBody := &ast.CaseClause{
Case: node.Case,
List: node.List,
Colon: node.Colon,
}
if f.printLength(nodeWithoutBody) > shortLineLimit {
// too long to collapse
break
}
Expand Down
64 changes: 64 additions & 0 deletions testdata/scripts/short-case.txt
Expand Up @@ -50,6 +50,40 @@ func f(r rune) {
}
}
}

func s(x int) {
switch x {
case
shortConstant1,
shortConstant2:
// A comment.
fmt.Println(x)
case
shortConstant3,
shortConstant4:
// Do nothing.
default:
// Another comment.
fmt.Println(x * 2)
}
}

func s(x int) {
switch x {
case
longerConstantName1,
longerConstantName2:
// A comment.
fmt.Println(x)
case
longerConstantName3,
longerConstantName4:
// Do nothing.
default:
// Another comment.
fmt.Println(x * 2)
}
}
-- foo.go.golden --
package p

Expand Down Expand Up @@ -91,3 +125,33 @@ func f(r rune) {
}
}
}

func s(x int) {
switch x {
case shortConstant1, shortConstant2:
// A comment.
fmt.Println(x)
case shortConstant3, shortConstant4:
// Do nothing.
default:
// Another comment.
fmt.Println(x * 2)
}
}

func s(x int) {
switch x {
case
longerConstantName1,
longerConstantName2:
// A comment.
fmt.Println(x)
case
longerConstantName3,
longerConstantName4:
// Do nothing.
default:
// Another comment.
fmt.Println(x * 2)
}
}

0 comments on commit b5619f8

Please sign in to comment.