diff --git a/format/format.go b/format/format.go index 7b9f455..c2ddcb0 100644 --- a/format/format.go +++ b/format/format.go @@ -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 } diff --git a/testdata/scripts/short-case.txt b/testdata/scripts/short-case.txt index 7a828c1..3baa2b6 100644 --- a/testdata/scripts/short-case.txt +++ b/testdata/scripts/short-case.txt @@ -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 @@ -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) + } +}