Skip to content

Commit

Permalink
Adjust indentation of line comments inside expression switches
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cushon authored and google-java-format Team committed Jun 17, 2024
1 parent 7fd9300 commit 3affd63
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,16 @@ public class FormatterIntegrationTest {

private static final ImmutableMultimap<Integer, String> VERSIONED_TESTS =
ImmutableMultimap.<Integer, String>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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
};
}
}
Original file line number Diff line number Diff line change
@@ -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;
};
}
}

0 comments on commit 3affd63

Please sign in to comment.