Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format: exclude body on CaseClause len check #219

Merged
merged 1 commit into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion format/format.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)
}
}