Skip to content

Commit

Permalink
format: exclude body on CaseClause len check
Browse files Browse the repository at this point in the history
fixes mvdan#217
  • Loading branch information
Oiyoo committed Apr 3, 2022
1 parent 6105233 commit df00374
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 2 deletions.
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
2 changes: 1 addition & 1 deletion main_test.go
Expand Up @@ -26,7 +26,7 @@ var update = flag.Bool("u", false, "update testscript output files")
func TestScripts(t *testing.T) {
t.Parallel()
p := testscript.Params{
Dir: filepath.Join("testdata", "scripts"),
Dir: filepath.Join("testdata", "s"),
UpdateScripts: *update,
}
err := gotooltest.Setup(&p)
Expand Down
157 changes: 157 additions & 0 deletions testdata/s/short-case.txt
@@ -0,0 +1,157 @@
gofumpt -w foo.go
cmp foo.go foo.go.golden

gofumpt -d foo.go.golden
! stdout .

-- foo.go --
package p

func f(r rune) {
switch r {
case 'a',
'b',
'c':

case 'd', 'e', 'f':

case 'a', 'b',
'c':

case 'v', 'e', 'r', 'y', 'l', 'o', 'n', 'g',
'l', 'i', 's', 't', '.', '.', '.':

// before
case 'a',
'b': // inline
// after

case 'a', // middle
'b':

case 'a', 'b', 'c', 'd', 'e', 'f',
'g': // very very long inline comment at the end

case 'a', 'b', 'c',
'd': // short comment
}
{
{
{
{
{
switch r {
case 'i', 'n', 'd', 'e',
'n', 't', 'e', 'd':
}
}
}
}
}
}
}

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

func f(r rune) {
switch r {
case 'a', 'b', 'c':

case 'd', 'e', 'f':

case 'a', 'b', 'c':

case 'v', 'e', 'r', 'y', 'l', 'o', 'n', 'g',
'l', 'i', 's', 't', '.', '.', '.':

// before
case 'a', 'b': // inline
// after

case 'a', // middle
'b':

case 'a', 'b', 'c', 'd', 'e', 'f',
'g': // very very long inline comment at the end

case 'a', 'b', 'c', 'd': // short comment
}
{
{
{
{
{
switch r {
case 'i', 'n', 'd', 'e',
'n', 't', 'e', 'd':
}
}
}
}
}
}
}

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)
}
}
34 changes: 34 additions & 0 deletions testdata/scripts/short-case.txt
Expand Up @@ -50,6 +50,23 @@ func f(r rune) {
}
}
}

func s(x int) {
switch x {
case
longConstantName1,
longConstantName2:
// A comment.
fmt.Println(x)
case
longConstantName3,
longConstantName4:
// Do nothing.
default:
// Another comment.
fmt.Println(x * 2)
}
}
-- foo.go.golden --
package p

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

func s(x int) {
switch x {
case
longConstantName1,
longConstantName2:
// A comment.
fmt.Println(x)
case
longConstantName3,
longConstantName4:
// Do nothing.
default:
// Another comment.
fmt.Println(x * 2)
}
}

0 comments on commit df00374

Please sign in to comment.