From b5619f8b06cad833eac1c454a48024599d8f5192 Mon Sep 17 00:00:00 2001 From: Takbir Newaz Date: Sun, 3 Apr 2022 21:54:41 +0600 Subject: [PATCH] format: exclude body on CaseClause len check fixes #217 --- format/format.go | 8 ++++- testdata/scripts/short-case.txt | 64 +++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) 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) + } +}