diff --git a/packages/prettier-plugin-java/src/printers/blocks-and-statements.ts b/packages/prettier-plugin-java/src/printers/blocks-and-statements.ts index 2f0ffa9e..9d70fe16 100644 --- a/packages/prettier-plugin-java/src/printers/blocks-and-statements.ts +++ b/packages/prettier-plugin-java/src/printers/blocks-and-statements.ts @@ -265,13 +265,20 @@ export class BlocksAndStatementPrettierVisitor extends BaseCstPrettierPrinter { switchBlockStatementGroup(ctx: SwitchBlockStatementGroupCtx) { const switchLabel = this.visit(ctx.switchLabel); - const blockStatements = this.visit(ctx.blockStatements); + const statements = ctx.blockStatements?.[0].children.blockStatement; + const hasSingleStatementBlock = + statements?.length === 1 && + statements[0].children.statement?.[0].children + .statementWithoutTrailingSubstatement?.[0].children.block !== undefined; + return concat([ switchLabel, ctx.Colon[0], - blockStatements && indent([hardline, blockStatements]) + hasSingleStatementBlock + ? concat([" ", blockStatements]) + : blockStatements && indent([hardline, blockStatements]) ]); } diff --git a/packages/prettier-plugin-java/test/unit-test/switch/_input.java b/packages/prettier-plugin-java/test/unit-test/switch/_input.java index 20c279cd..388b1604 100644 --- a/packages/prettier-plugin-java/test/unit-test/switch/_input.java +++ b/packages/prettier-plugin-java/test/unit-test/switch/_input.java @@ -48,6 +48,41 @@ public String shouldWrapEvenForSmallSwitchCases() { switch (answer) { case "YES": return "YES"; default: return "NO"; } } + void switchCaseWithBlock1() { + switch (a) { + case 0: {} + default: {} + } + } + + void switchCaseWithBlock2() { + switch (a) { + case 0: { b(); } + default: { c(); } + } + } + + void switchCaseWithBlock3() { + switch (a) { + case 0: { b(); } { c(); } + default: { d(); } { e(); } + } + } + + void switchCaseWithBlock4() { + switch (a) { + case 0: b(); { c(); } + default: d(); { e(); } + } + } + + void switchCaseWithBlock5() { + switch (a) { + case 0: { b(); } c(); + default: { d(); } e(); + } + } + // Switch rules static void howManyAgain(int k) { switch (k) { diff --git a/packages/prettier-plugin-java/test/unit-test/switch/_output.java b/packages/prettier-plugin-java/test/unit-test/switch/_output.java index 45868c7e..0b5085b7 100644 --- a/packages/prettier-plugin-java/test/unit-test/switch/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/switch/_output.java @@ -53,6 +53,73 @@ public String shouldWrapEvenForSmallSwitchCases() { } } + void switchCaseWithBlock1() { + switch (a) { + case 0: {} + default: {} + } + } + + void switchCaseWithBlock2() { + switch (a) { + case 0: { + b(); + } + default: { + c(); + } + } + } + + void switchCaseWithBlock3() { + switch (a) { + case 0: + { + b(); + } + { + c(); + } + default: + { + d(); + } + { + e(); + } + } + } + + void switchCaseWithBlock4() { + switch (a) { + case 0: + b(); + { + c(); + } + default: + d(); + { + e(); + } + } + } + + void switchCaseWithBlock5() { + switch (a) { + case 0: + { + b(); + } + c(); + default: + { + d(); + } + e(); + } + } + // Switch rules static void howManyAgain(int k) { switch (k) {