Skip to content

Commit 921b635

Browse files
authored
Inline case statements with single block body (#252)
Case statements with a body consisting of a single block now inline their block.
1 parent e73b06b commit 921b635

File tree

4 files changed

+69
-6
lines changed

4 files changed

+69
-6
lines changed

changelog/@unreleased/pr-252.v2.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
type: improvement
2+
improvement:
3+
description: Case statements with a body consisting of a single block now inline
4+
their block.
5+
links:
6+
- https://github.com/palantir/palantir-java-format/pull/252

palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,7 +1511,7 @@ private void methodBody(MethodTree node) {
15111511
builder.open(plusTwo);
15121512
builder.forcedBreak();
15131513
builder.blankLineWanted(BlankLineWanted.PRESERVE);
1514-
visitStatements(node.getBody().getStatements());
1514+
visitStatements(node.getBody().getStatements(), false);
15151515
builder.close();
15161516
builder.forcedBreak();
15171517
builder.blankLineWanted(BlankLineWanted.NO);
@@ -1817,8 +1817,13 @@ public Void visitCase(CaseTree node, Void unused) {
18171817
scan(node.getExpression(), null);
18181818
token(":");
18191819
}
1820-
builder.open(plusTwo);
1821-
visitStatements(node.getStatements());
1820+
boolean isBlock =
1821+
node.getStatements().size() == 1 && node.getStatements().get(0).getKind() == BLOCK;
1822+
builder.open(isBlock ? ZERO : plusTwo);
1823+
if (isBlock) {
1824+
builder.space();
1825+
}
1826+
visitStatements(node.getStatements(), isBlock);
18221827
builder.close();
18231828
return null;
18241829
}
@@ -2115,7 +2120,7 @@ private void visitBlock(
21152120
} else {
21162121
builder.blankLineWanted(BlankLineWanted.PRESERVE);
21172122
}
2118-
visitStatements(node.getStatements());
2123+
visitStatements(node.getStatements(), false);
21192124
builder.close();
21202125
builder.forcedBreak();
21212126
builder.close();
@@ -2149,13 +2154,15 @@ private void visitStatement(
21492154
}
21502155
}
21512156

2152-
private void visitStatements(List<? extends StatementTree> statements) {
2157+
private void visitStatements(List<? extends StatementTree> statements, boolean inlineFirst) {
21532158
boolean first = true;
21542159
PeekingIterator<StatementTree> it = Iterators.peekingIterator(statements.iterator());
21552160
dropEmptyDeclarations();
21562161
while (it.hasNext()) {
21572162
StatementTree tree = it.next();
2158-
builder.forcedBreak();
2163+
if (!(inlineFirst && first)) {
2164+
builder.forcedBreak();
2165+
}
21592166
if (!first) {
21602167
builder.blankLineWanted(BlankLineWanted.PRESERVE);
21612168
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class PalantirCaseStatementsBlock {
2+
private void foo() {
3+
switch (true) {
4+
case false:
5+
{
6+
String foo = "bar";
7+
return 5;
8+
}
9+
{
10+
// second block should cause first one not to inline
11+
}
12+
case true:
13+
{
14+
String foo = "bar";
15+
return 5;
16+
}
17+
default:
18+
{
19+
{
20+
String foo = "bar";
21+
}
22+
return 5;
23+
}
24+
}
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class PalantirCaseStatementsBlock {
2+
private void foo() {
3+
switch (true) {
4+
case false:
5+
{
6+
String foo = "bar";
7+
return 5;
8+
}
9+
{
10+
// second block should cause first one not to inline
11+
}
12+
case true: {
13+
String foo = "bar";
14+
return 5;
15+
}
16+
default: {
17+
{
18+
String foo = "bar";
19+
}
20+
return 5;
21+
}
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)