Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline case statements with single block body #252

Merged
merged 3 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-252.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: improvement
improvement:
description: Case statements with a body consisting of a single block now inline
their block.
links:
- https://github.com/palantir/palantir-java-format/pull/252
Original file line number Diff line number Diff line change
Expand Up @@ -1511,7 +1511,7 @@ private void methodBody(MethodTree node) {
builder.open(plusTwo);
builder.forcedBreak();
builder.blankLineWanted(BlankLineWanted.PRESERVE);
visitStatements(node.getBody().getStatements());
visitStatements(node.getBody().getStatements(), false);
builder.close();
builder.forcedBreak();
builder.blankLineWanted(BlankLineWanted.NO);
Expand Down Expand Up @@ -1817,8 +1817,13 @@ public Void visitCase(CaseTree node, Void unused) {
scan(node.getExpression(), null);
token(":");
}
builder.open(plusTwo);
visitStatements(node.getStatements());
boolean isBlock =
node.getStatements().size() == 1 && node.getStatements().get(0).getKind() == BLOCK;
builder.open(isBlock ? ZERO : plusTwo);
if (isBlock) {
builder.space();
}
visitStatements(node.getStatements(), isBlock);
builder.close();
return null;
}
Expand Down Expand Up @@ -2115,7 +2120,7 @@ private void visitBlock(
} else {
builder.blankLineWanted(BlankLineWanted.PRESERVE);
}
visitStatements(node.getStatements());
visitStatements(node.getStatements(), false);
builder.close();
builder.forcedBreak();
builder.close();
Expand Down Expand Up @@ -2149,13 +2154,15 @@ private void visitStatement(
}
}

private void visitStatements(List<? extends StatementTree> statements) {
private void visitStatements(List<? extends StatementTree> statements, boolean inlineFirst) {
boolean first = true;
PeekingIterator<StatementTree> it = Iterators.peekingIterator(statements.iterator());
dropEmptyDeclarations();
while (it.hasNext()) {
StatementTree tree = it.next();
builder.forcedBreak();
if (!(inlineFirst && first)) {
builder.forcedBreak();
}
if (!first) {
builder.blankLineWanted(BlankLineWanted.PRESERVE);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class PalantirCaseStatementsBlock {
private void foo() {
switch (true) {
case false:
{
String foo = "bar";
return 5;
}
{
// second block should cause first one not to inline
}
case true:
{
String foo = "bar";
return 5;
}
default:
{
{
String foo = "bar";
}
return 5;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
public class PalantirCaseStatementsBlock {
private void foo() {
switch (true) {
case false:
{
String foo = "bar";
return 5;
}
{
// second block should cause first one not to inline
}
case true: {
String foo = "bar";
return 5;
}
default: {
{
String foo = "bar";
}
return 5;
}
}
}
}