From 79257edbf26894102793b00a19fe4b68d83e5622 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Wed, 25 Aug 2021 13:24:47 -0700 Subject: [PATCH] Fix indentation of case statements on JDK 17 Fixes https://github.com/google/google-java-format/issues/643 PiperOrigin-RevId: 392966720 --- .../java/java14/Java14InputAstVisitor.java | 2 +- .../googlejavaformat/java/testdata/I643.input | 14 ++++++++++++++ .../googlejavaformat/java/testdata/I643.output | 16 ++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/I643.input create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/I643.output diff --git a/core/src/main/java/com/google/googlejavaformat/java/java14/Java14InputAstVisitor.java b/core/src/main/java/com/google/googlejavaformat/java/java14/Java14InputAstVisitor.java index b0d2a7675..3facbd711 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/java14/Java14InputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/java14/Java14InputAstVisitor.java @@ -251,7 +251,7 @@ public Void visitCase(CaseTree node, Void unused) { token("default", plusTwo); } else { token("case", plusTwo); - builder.open(plusFour); + builder.open(node.getExpressions().size() > 1 ? plusFour : ZERO); builder.space(); boolean first = true; for (ExpressionTree expression : node.getExpressions()) { diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I643.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I643.input new file mode 100644 index 000000000..a31a230be --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I643.input @@ -0,0 +1,14 @@ +public class Foo { + static final int VERBOSE_WORDY_AND_LENGTHY_ONE = 1; + static final int VERBOSE_WORDY_AND_LENGTHY_TWO = 2; + static final int VERBOSE_WORDY_AND_LENGTHY_FOUR = 4; + + public static int fn(int x) { + switch (x) { + case VERBOSE_WORDY_AND_LENGTHY_ONE | VERBOSE_WORDY_AND_LENGTHY_TWO | VERBOSE_WORDY_AND_LENGTHY_FOUR: + return 0; + default: + return 1; + } + } +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I643.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I643.output new file mode 100644 index 000000000..945cbea14 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I643.output @@ -0,0 +1,16 @@ +public class Foo { + static final int VERBOSE_WORDY_AND_LENGTHY_ONE = 1; + static final int VERBOSE_WORDY_AND_LENGTHY_TWO = 2; + static final int VERBOSE_WORDY_AND_LENGTHY_FOUR = 4; + + public static int fn(int x) { + switch (x) { + case VERBOSE_WORDY_AND_LENGTHY_ONE + | VERBOSE_WORDY_AND_LENGTHY_TWO + | VERBOSE_WORDY_AND_LENGTHY_FOUR: + return 0; + default: + return 1; + } + } +}