From 3affd633ce2e798051defe2e09090b46c0a49098 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Mon, 17 Jun 2024 16:05:35 -0700 Subject: [PATCH] Adjust indentation of line comments inside expression switches For statement switches there's some ambiguity about whether a comment documents the previous or next case: ``` case 1: // case 1 falls through to 2 case 2: doSomething() ``` ``` // this is information about case 1 case 1: // this is information about case 2 case 2: doSomething() ``` For expression switches there is no fall through, so assume that a line comments before a case label always apply to the case label after it. PiperOrigin-RevId: 644163145 --- .../java/java17/Java17InputAstVisitor.java | 6 ++-- .../java/FormatterIntegrationTest.java | 11 ++++++- .../java/testdata/SwitchComment.input | 31 +++++++++++++++++++ .../java/testdata/SwitchComment.output | 31 +++++++++++++++++++ 4 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchComment.input create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchComment.output diff --git a/core/src/main/java/com/google/googlejavaformat/java/java17/Java17InputAstVisitor.java b/core/src/main/java/com/google/googlejavaformat/java/java17/Java17InputAstVisitor.java index e502f49a1..486324fb7 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/java17/Java17InputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/java17/Java17InputAstVisitor.java @@ -19,6 +19,7 @@ import com.google.common.base.Verify; import com.google.common.collect.ImmutableList; +import com.google.googlejavaformat.Indent; import com.google.googlejavaformat.OpsBuilder; import com.google.googlejavaformat.OpsBuilder.BlankLineWanted; import com.google.googlejavaformat.java.JavaInputAstVisitor; @@ -232,10 +233,11 @@ public Void visitCase(CaseTree node, Void unused) { && !node.getBody().getKind().equals(Tree.Kind.BLOCK) ? plusFour : ZERO); + Indent commentIndent = node.getCaseKind().equals(CaseTree.CaseKind.RULE) ? ZERO : plusTwo; if (isDefault) { - token("default", plusTwo); + token("default", commentIndent); } else { - token("case", plusTwo); + token("case", commentIndent); builder.open(labels.size() > 1 ? plusFour : ZERO); builder.space(); boolean afterFirstToken = false; diff --git a/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java b/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java index c7142b793..3e4e175e6 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java @@ -48,7 +48,16 @@ public class FormatterIntegrationTest { private static final ImmutableMultimap VERSIONED_TESTS = ImmutableMultimap.builder() - .putAll(14, "I477", "Records", "RSLs", "Var", "ExpressionSwitch", "I574", "I594") + .putAll( + 14, + "I477", + "Records", + "RSLs", + "Var", + "ExpressionSwitch", + "I574", + "I594", + "SwitchComment") .putAll(15, "I603") .putAll(16, "I588", "Sealed") .putAll(17, "I683", "I684", "I696") diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchComment.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchComment.input new file mode 100644 index 000000000..f43acd351 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchComment.input @@ -0,0 +1,31 @@ +class T { + void f(String v) { + int x = + switch (v) { + // this is a line comment about "zero" + case "zero" -> 0; + case "one" -> + // this is a line comment about "one" + 1; + case "two" -> // this is a line comment about "two" + 2; + default -> -1; + }; + } + + void g(String v) { + int x = + switch (v) { + // this is a line comment about "zero" + case "zero": + return 0; + case "one": + // this is a line comment about "one" + return 1; + case "two": // this is a line comment about "two" + return 2; + default: + return -1; + }; + } +} \ No newline at end of file diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchComment.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchComment.output new file mode 100644 index 000000000..ff4753b1e --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchComment.output @@ -0,0 +1,31 @@ +class T { + void f(String v) { + int x = + switch (v) { + // this is a line comment about "zero" + case "zero" -> 0; + case "one" -> + // this is a line comment about "one" + 1; + case "two" -> // this is a line comment about "two" + 2; + default -> -1; + }; + } + + void g(String v) { + int x = + switch (v) { + // this is a line comment about "zero" + case "zero": + return 0; + case "one": + // this is a line comment about "one" + return 1; + case "two": // this is a line comment about "two" + return 2; + default: + return -1; + }; + } +}