Skip to content

Commit 3affd63

Browse files
cushongoogle-java-format Team
authored and
google-java-format Team
committed
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
1 parent 7fd9300 commit 3affd63

File tree

4 files changed

+76
-3
lines changed

4 files changed

+76
-3
lines changed

core/src/main/java/com/google/googlejavaformat/java/java17/Java17InputAstVisitor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.google.common.base.Verify;
2121
import com.google.common.collect.ImmutableList;
22+
import com.google.googlejavaformat.Indent;
2223
import com.google.googlejavaformat.OpsBuilder;
2324
import com.google.googlejavaformat.OpsBuilder.BlankLineWanted;
2425
import com.google.googlejavaformat.java.JavaInputAstVisitor;
@@ -232,10 +233,11 @@ public Void visitCase(CaseTree node, Void unused) {
232233
&& !node.getBody().getKind().equals(Tree.Kind.BLOCK)
233234
? plusFour
234235
: ZERO);
236+
Indent commentIndent = node.getCaseKind().equals(CaseTree.CaseKind.RULE) ? ZERO : plusTwo;
235237
if (isDefault) {
236-
token("default", plusTwo);
238+
token("default", commentIndent);
237239
} else {
238-
token("case", plusTwo);
240+
token("case", commentIndent);
239241
builder.open(labels.size() > 1 ? plusFour : ZERO);
240242
builder.space();
241243
boolean afterFirstToken = false;

core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,16 @@ public class FormatterIntegrationTest {
4848

4949
private static final ImmutableMultimap<Integer, String> VERSIONED_TESTS =
5050
ImmutableMultimap.<Integer, String>builder()
51-
.putAll(14, "I477", "Records", "RSLs", "Var", "ExpressionSwitch", "I574", "I594")
51+
.putAll(
52+
14,
53+
"I477",
54+
"Records",
55+
"RSLs",
56+
"Var",
57+
"ExpressionSwitch",
58+
"I574",
59+
"I594",
60+
"SwitchComment")
5261
.putAll(15, "I603")
5362
.putAll(16, "I588", "Sealed")
5463
.putAll(17, "I683", "I684", "I696")
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class T {
2+
void f(String v) {
3+
int x =
4+
switch (v) {
5+
// this is a line comment about "zero"
6+
case "zero" -> 0;
7+
case "one" ->
8+
// this is a line comment about "one"
9+
1;
10+
case "two" -> // this is a line comment about "two"
11+
2;
12+
default -> -1;
13+
};
14+
}
15+
16+
void g(String v) {
17+
int x =
18+
switch (v) {
19+
// this is a line comment about "zero"
20+
case "zero":
21+
return 0;
22+
case "one":
23+
// this is a line comment about "one"
24+
return 1;
25+
case "two": // this is a line comment about "two"
26+
return 2;
27+
default:
28+
return -1;
29+
};
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class T {
2+
void f(String v) {
3+
int x =
4+
switch (v) {
5+
// this is a line comment about "zero"
6+
case "zero" -> 0;
7+
case "one" ->
8+
// this is a line comment about "one"
9+
1;
10+
case "two" -> // this is a line comment about "two"
11+
2;
12+
default -> -1;
13+
};
14+
}
15+
16+
void g(String v) {
17+
int x =
18+
switch (v) {
19+
// this is a line comment about "zero"
20+
case "zero":
21+
return 0;
22+
case "one":
23+
// this is a line comment about "one"
24+
return 1;
25+
case "two": // this is a line comment about "two"
26+
return 2;
27+
default:
28+
return -1;
29+
};
30+
}
31+
}

0 commit comments

Comments
 (0)