Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -1854,6 +1854,7 @@ public Void visitSwitch(SwitchTree node, Void unused) {
}

protected void visitSwitch(ExpressionTree expression, List<? extends CaseTree> cases) {
builder.open(ZERO);
token("switch");
builder.space();
token("(");
Expand All @@ -1874,7 +1875,8 @@ protected void visitSwitch(ExpressionTree expression, List<? extends CaseTree> c
builder.close();
builder.forcedBreak();
builder.blankLineWanted(BlankLineWanted.NO);
token("}", plusFour);
token("}", plusTwo);
builder.close();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,20 @@ class ExpressionSwitch {
case 2 -> throw new IllegalArgumentException();
default -> throw new IllegalStateException();};
}


public void testWithComments(int y) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this the intended snippet for the before? I couldn't see what the difference was manually, and both diff and difft report no differences.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, without this change, the formatter would put the "}" on the same line as the comment - which breaks the compilation eg.

        x = switch (y) {
            case 1 -> 1;
            // after case 1
            case 2 -> throw new IllegalArgumentException();
            // after case 2
            default -> throw new IllegalStateException();
            // after default case};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, are you able to put that in the description, it's quite hard to tell what's being fixed from just the code + PR description

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed the input to a wrongly formatted switch expression here

int x;
x = switch (y) {
case 1 ->
1;
// after case 1
case 2 ->
throw new IllegalArgumentException();
// after case 2
default ->
throw new IllegalStateException();
// after default case
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,16 @@ class ExpressionSwitch {
default -> throw new IllegalStateException();
};
}

public void testWithComments(int y) {
int x;
x = switch (y) {
case 1 -> 1;
// after case 1
case 2 -> throw new IllegalArgumentException();
// after case 2
default -> throw new IllegalStateException();
// after default case
};
}
}