From a356f53ffc99d0c9a7aaee65f0b6d289791c12fb Mon Sep 17 00:00:00 2001 From: Jordan Kiesel Date: Sat, 5 Aug 2023 20:29:29 -0600 Subject: [PATCH] fix: stable format for if/for/while/do/catch/switch with trailing comment closes #592 --- .../src/printers/blocks-and-statements.ts | 146 ++++++++++++----- .../src/printers/comments/handle-comments.ts | 152 +++++++++++++++++- .../complex/_output.java | 20 +-- .../if-statement/_input.java | 25 +++ .../if-statement/_output.java | 28 ++++ .../test/unit-test/constructors/_input.java | 30 ++++ .../test/unit-test/constructors/_output.java | 24 +++ .../unit-test/empty_statement/_output.java | 34 ++-- .../test/unit-test/for/_input.java | 74 +++++++++ .../test/unit-test/for/_output.java | 94 +++++++++++ .../test/unit-test/sealed/_output.java | 11 +- .../test/unit-test/switch/_input.java | 15 ++ .../test/unit-test/switch/_output.java | 17 ++ .../test/unit-test/try_catch/_input.java | 37 +++++ .../test/unit-test/try_catch/_output.java | 38 +++++ .../test/unit-test/while/_input.java | 68 ++++++++ .../test/unit-test/while/_output.java | 72 +++++++++ 17 files changed, 816 insertions(+), 69 deletions(-) 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 993ad624..7ba98fcb 100644 --- a/packages/prettier-plugin-java/src/printers/blocks-and-statements.ts +++ b/packages/prettier-plugin-java/src/printers/blocks-and-statements.ts @@ -1,8 +1,14 @@ "use strict"; import { builders } from "prettier/doc"; -import { concat, dedent, group, indent, join } from "./prettier-builder"; +import { concat, group, indent, join } from "./prettier-builder"; import { printTokenWithComments } from "./comments/format-comments"; +import { + handleCommentsBasicForStatement, + handleCommentsCatchClauseOrSwitchStatement, + handleCommentsDoStatement, + handleCommentsEnhancedForOrIfOrWhileStatement +} from "./comments/handle-comments"; import { hasLeadingLineComments, hasTrailingLineComments @@ -192,12 +198,19 @@ export class BlocksAndStatementPrettierVisitor extends BaseCstPrettierPrinter { } ifStatement(ctx: IfStatementCtx) { + handleCommentsEnhancedForOrIfOrWhileStatement(ctx); + const expression = this.visit(ctx.expression); const ifStatement = this.visit(ctx.statement[0], { allowEmptyStatement: true }); - const ifSeparator = isStatementEmptyStatement(ifStatement) ? "" : " "; + const ifStatementCtx = + ctx.statement[0].children.statementWithoutTrailingSubstatement?.[0] + .children; + const emptyIfStatement = ifStatementCtx?.emptyStatement !== undefined; + const hasIfBlock = ifStatementCtx?.block !== undefined; + const ifSeparator = emptyIfStatement ? "" : hasIfBlock ? " " : indent(line); let elsePart: Doc = ""; if (ctx.Else !== undefined) { @@ -208,7 +221,9 @@ export class BlocksAndStatementPrettierVisitor extends BaseCstPrettierPrinter { const elseOnSameLine = hasTrailingLineComments(ctx.statement[0]) || - hasLeadingLineComments(ctx.Else[0]) + hasLeadingLineComments(ctx.Else[0]) || + emptyIfStatement || + !hasIfBlock ? hardline : " "; @@ -226,7 +241,7 @@ export class BlocksAndStatementPrettierVisitor extends BaseCstPrettierPrinter { ifSeparator ]) ]), - ifStatement, + hasIfBlock ? ifStatement : indent(ifStatement), elsePart ]); } @@ -242,6 +257,8 @@ export class BlocksAndStatementPrettierVisitor extends BaseCstPrettierPrinter { } switchStatement(ctx: SwitchStatementCtx) { + handleCommentsCatchClauseOrSwitchStatement(ctx); + const expression = this.visit(ctx.expression); const switchBlock = this.visit(ctx.switchBlock); @@ -340,22 +357,36 @@ export class BlocksAndStatementPrettierVisitor extends BaseCstPrettierPrinter { } whileStatement(ctx: WhileStatementCtx) { + handleCommentsEnhancedForOrIfOrWhileStatement(ctx); + const expression = this.visit(ctx.expression); const statement = this.visit(ctx.statement[0], { allowEmptyStatement: true }); - const statementSeparator = isStatementEmptyStatement(statement) ? "" : " "; - return rejectAndJoin(" ", [ - ctx.While[0], - rejectAndJoin(statementSeparator, [ - putIntoBraces(expression, softline, ctx.LBrace[0], ctx.RBrace[0]), - statement + const hasBlock = + ctx.statement[0].children.statementWithoutTrailingSubstatement?.[0] + .children.block !== undefined; + const statementSeparator = isStatementEmptyStatement(statement) + ? "" + : hasBlock + ? " " + : indent(line); + + return group( + rejectAndJoin(" ", [ + ctx.While[0], + rejectAndJoin(statementSeparator, [ + putIntoBraces(expression, softline, ctx.LBrace[0], ctx.RBrace[0]), + hasBlock ? statement : indent(statement) + ]) ]) - ]); + ); } doStatement(ctx: DoStatementCtx) { + handleCommentsDoStatement(ctx); + const statement = this.visit(ctx.statement[0], { allowEmptyStatement: true }); @@ -378,31 +409,44 @@ export class BlocksAndStatementPrettierVisitor extends BaseCstPrettierPrinter { } basicForStatement(ctx: BasicForStatementCtx) { + handleCommentsBasicForStatement(ctx); + const forInit = this.visit(ctx.forInit); const expression = this.visit(ctx.expression); const forUpdate = this.visit(ctx.forUpdate); const statement = this.visit(ctx.statement[0], { allowEmptyStatement: true }); - const statementSeparator = isStatementEmptyStatement(statement) ? "" : " "; - return rejectAndConcat([ - rejectAndJoin(" ", [ + const headerSeparator = + ctx.forInit || ctx.expression || ctx.forUpdate ? line : ""; + const hasBlock = + ctx.statement[0].children.statementWithoutTrailingSubstatement?.[0] + .children.block !== undefined; + const statementSeparator = isStatementEmptyStatement(statement) + ? "" + : hasBlock + ? " " + : indent(line); + + return group( + join(" ", [ ctx.For[0], - putIntoBraces( - rejectAndConcat([ - forInit, - rejectAndJoin(line, [ctx.Semicolon[0], expression]), - rejectAndJoin(line, [ctx.Semicolon[1], forUpdate]) - ]), - softline, - ctx.LBrace[0], - ctx.RBrace[0] - ) - ]), - statementSeparator, - statement - ]); + join(statementSeparator, [ + putIntoBraces( + join(headerSeparator, [ + rejectAndConcat([forInit, ctx.Semicolon[0]]), + rejectAndConcat([expression, ctx.Semicolon[1]]), + forUpdate + ]), + softline, + ctx.LBrace[0], + ctx.RBrace[0] + ), + hasBlock ? statement : indent(statement) + ]) + ]) + ); } forInit(ctx: ForInitCtx) { @@ -424,6 +468,8 @@ export class BlocksAndStatementPrettierVisitor extends BaseCstPrettierPrinter { } enhancedForStatement(ctx: EnhancedForStatementCtx) { + handleCommentsEnhancedForOrIfOrWhileStatement(ctx); + const variableModifiers = this.mapVisit(ctx.variableModifier); const localVariableType = this.visit(ctx.localVariableType); const variableDeclaratorId = this.visit(ctx.variableDeclaratorId); @@ -431,20 +477,36 @@ export class BlocksAndStatementPrettierVisitor extends BaseCstPrettierPrinter { const statement = this.visit(ctx.statement[0], { allowEmptyStatement: true }); - const statementSeparator = isStatementEmptyStatement(statement) ? "" : " "; - return rejectAndConcat([ - rejectAndJoin(" ", [ctx.For[0], ctx.LBrace[0]]), - rejectAndJoin(" ", [ - rejectAndJoin(" ", variableModifiers), - localVariableType, - variableDeclaratorId - ]), - concat([" ", ctx.Colon[0], " "]), - expression, - concat([ctx.RBrace[0], statementSeparator]), - statement - ]); + const hasBlock = + ctx.statement[0].children.statementWithoutTrailingSubstatement?.[0] + .children.block !== undefined; + const statementSeparator = isStatementEmptyStatement(statement) + ? "" + : hasBlock + ? " " + : indent(line); + + return group( + join(" ", [ + ctx.For[0], + join(statementSeparator, [ + putIntoBraces( + join(" ", [ + ...variableModifiers, + localVariableType, + variableDeclaratorId, + ctx.Colon[0], + expression + ]), + "", + ctx.LBrace[0], + ctx.RBrace[0] + ), + hasBlock ? statement : indent(statement) + ]) + ]) + ); } breakStatement(ctx: BreakStatementCtx) { @@ -534,6 +596,8 @@ export class BlocksAndStatementPrettierVisitor extends BaseCstPrettierPrinter { } catchClause(ctx: CatchClauseCtx) { + handleCommentsCatchClauseOrSwitchStatement(ctx); + const catchFormalParameter = this.visit(ctx.catchFormalParameter); const block = this.visit(ctx.block); diff --git a/packages/prettier-plugin-java/src/printers/comments/handle-comments.ts b/packages/prettier-plugin-java/src/printers/comments/handle-comments.ts index 4348acf7..c3c346a5 100644 --- a/packages/prettier-plugin-java/src/printers/comments/handle-comments.ts +++ b/packages/prettier-plugin-java/src/printers/comments/handle-comments.ts @@ -1,10 +1,37 @@ import { hasLeadingComments, hasTrailingComments } from "./comments-utils"; import { + BasicForStatementCtx, BinaryExpressionCtx, + CatchClauseCtx, + DoStatementCtx, + EnhancedForStatementCtx, IToken, - UnaryExpressionCstNode + IfStatementCtx, + StatementWithoutTrailingSubstatementCtx, + SwitchStatementCtx, + UnaryExpressionCstNode, + WhileStatementCtx } from "java-parser"; +export function handleCommentsBasicForStatement(ctx: BasicForStatementCtx) { + const comments = removeRBraceTrailingAndEmptyStatementLeadingComments(ctx); + if (!comments.length) { + return; + } + const target = ctx.forUpdate?.[0] ?? ctx.expression?.[0] ?? ctx.forInit?.[0]; + if (target) { + if (!target.trailingComments) { + target.trailingComments = []; + } + target.trailingComments.push(...comments); + } else { + if (!ctx.For[0].leadingComments) { + ctx.For[0].leadingComments = []; + } + ctx.For[0].leadingComments.push(...comments); + } +} + export function handleCommentsBinaryExpression(ctx: BinaryExpressionCtx) { moveOperatorLeadingCommentsToNextExpression(ctx); moveExpressionTrailingCommentsToNextOperator(ctx); @@ -97,3 +124,126 @@ function moveExpressionTrailingCommentsToNextOperator( }); } } + +export function handleCommentsCatchClauseOrSwitchStatement( + ctx: CatchClauseCtx | SwitchStatementCtx +) { + const comments = removeRBraceTrailingComments(ctx); + if (!comments.length) { + return; + } + const parenthesesContents = + "expression" in ctx ? ctx.expression[0] : ctx.catchFormalParameter[0]; + if (!parenthesesContents.trailingComments) { + parenthesesContents.trailingComments = []; + } + parenthesesContents.trailingComments.push(...comments); +} + +export function handleCommentsDoStatement(ctx: DoStatementCtx) { + const comments = removeRBraceTrailingComments(ctx); + const semicolon = ctx.Semicolon[0]; + if (semicolon.leadingComments) { + comments.push(...semicolon.leadingComments); + delete semicolon.leadingComments; + } + if (!comments.length) { + return; + } + const expression = ctx.expression[0]; + if (!expression.trailingComments) { + expression.trailingComments = []; + } + expression.trailingComments.push(...comments); +} + +export function handleCommentsEnhancedForOrIfOrWhileStatement( + ctx: EnhancedForStatementCtx | IfStatementCtx | WhileStatementCtx +) { + const comments = removeRBraceTrailingAndEmptyStatementLeadingComments(ctx); + if (!comments.length) { + return; + } + const statement = + ctx.statement[0].children.statementWithoutTrailingSubstatement?.[0] + .children; + if (statement?.emptyStatement) { + const expression = ctx.expression[0]; + if (!expression.trailingComments) { + expression.trailingComments = []; + } + expression.trailingComments.push(...comments); + return; + } + const inner = statement ? innerStatement(statement) : undefined; + const block = statement?.block?.[0].children.blockStatements?.[0]; + const rCurly = statement?.block?.[0].children.RCurly[0]; + const target = + inner ?? + block ?? + rCurly ?? + ("For" in ctx ? ctx.For : "If" in ctx ? ctx.If : ctx.While)[0]; + if (block) { + block.location.startLine--; + } + if (!target.leadingComments) { + target.leadingComments = []; + } + target.leadingComments.unshift(...comments); +} + +function innerStatement(statement: StatementWithoutTrailingSubstatementCtx) { + return [ + statement.assertStatement, + statement.breakStatement, + statement.continueStatement, + statement.doStatement, + statement.expressionStatement, + statement.expressionStatement, + statement.returnStatement, + statement.switchStatement, + statement.synchronizedStatement, + statement.throwStatement, + statement.tryStatement, + statement.yieldStatement + ].find(s => s)?.[0]; +} + +function removeRBraceTrailingAndEmptyStatementLeadingComments( + ctx: + | BasicForStatementCtx + | EnhancedForStatementCtx + | IfStatementCtx + | WhileStatementCtx +) { + const comments = removeRBraceTrailingComments(ctx); + const statement = ctx.statement[0]; + if ( + statement?.children.statementWithoutTrailingSubstatement?.[0].children + .emptyStatement?.[0] && + statement.leadingComments + ) { + comments.push(...statement.leadingComments); + delete statement.leadingComments; + } + return comments; +} + +function removeRBraceTrailingComments( + ctx: + | BasicForStatementCtx + | CatchClauseCtx + | DoStatementCtx + | EnhancedForStatementCtx + | IfStatementCtx + | SwitchStatementCtx + | WhileStatementCtx +) { + const rBrace = ctx.RBrace[0]; + if (!rBrace.trailingComments) { + return []; + } + const comments = rBrace.trailingComments; + delete rBrace.trailingComments; + return comments; +} diff --git a/packages/prettier-plugin-java/test/unit-test/comments/comments-blocks-and-statements/complex/_output.java b/packages/prettier-plugin-java/test/unit-test/comments/comments-blocks-and-statements/complex/_output.java index 8d0a1a1e..e7226ea5 100644 --- a/packages/prettier-plugin-java/test/unit-test/comments/comments-blocks-and-statements/complex/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/comments/comments-blocks-and-statements/complex/_output.java @@ -23,9 +23,8 @@ private void myFunction( /* axis y */int arg2, /* axis z */int arg3 ) { - if (arg1 == 0 && arg2 == 0 && arg == 3) throw new RuntimeException( - "X Y Z cannot be all 0" - ); + if (arg1 == 0 && arg2 == 0 && arg == 3) + throw new RuntimeException("X Y Z cannot be all 0"); int /*variable name is of value var */var = arg1 + arg2 + arg3; if /*true*/(var == 0) { @@ -66,13 +65,14 @@ private void myFunction( } private synchronized void myFunction(int arg1, int arg2/*overloading*/) { - for (int i = 0; i < /*=*/arg1; i++) do /*dodododo*/{ //do whiles - //asserting - assert /*true*/true == true; - continue; - break/*dead code*/; - return/*dead code*/; - } /*at least one iteration !*/while (false); + for (int i = 0; i < /*=*/arg1; i++) + do /*dodododo*/{ //do whiles + //asserting + assert /*true*/true == true; + continue; + break/*dead code*/; + return/*dead code*/; + } /*at least one iteration !*/while (false); synchronized /*declares synchronizd statement*/(this) { while /*infinite*/(true) /*stop the program*/throw new RuntimeException(); } diff --git a/packages/prettier-plugin-java/test/unit-test/comments/comments-blocks-and-statements/if-statement/_input.java b/packages/prettier-plugin-java/test/unit-test/comments/comments-blocks-and-statements/if-statement/_input.java index d53f9178..2773d939 100644 --- a/packages/prettier-plugin-java/test/unit-test/comments/comments-blocks-and-statements/if-statement/_input.java +++ b/packages/prettier-plugin-java/test/unit-test/comments/comments-blocks-and-statements/if-statement/_input.java @@ -15,6 +15,29 @@ void commentsIfLineComment() { if ( // test t) { } + + if (true) // comment + System.out.println("Oops"); + + if (true) { + // comment + } + + if (true) // comment + {} + + if (true) // comment + { + System.out.println("Oops"); + } + + if (true) // comment + { + if (true) {} + } + + if (true) // comment + ; } void commentsIfBlockComment() { @@ -31,6 +54,8 @@ void commentsIfBlockComment() { if/* test */ (t) { } + + if (true) /*comment*/; } void commentsElseLineComment() { diff --git a/packages/prettier-plugin-java/test/unit-test/comments/comments-blocks-and-statements/if-statement/_output.java b/packages/prettier-plugin-java/test/unit-test/comments/comments-blocks-and-statements/if-statement/_output.java index 217808bd..f2f786d5 100644 --- a/packages/prettier-plugin-java/test/unit-test/comments/comments-blocks-and-statements/if-statement/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/comments/comments-blocks-and-statements/if-statement/_output.java @@ -14,6 +14,32 @@ void commentsIfLineComment() { if ( // test t ) {} + + if (true) + // comment + System.out.println("Oops"); + + if (true) { + // comment + } + + if (true) { + // comment + } + + if (true) { + // comment + System.out.println("Oops"); + } + + if (true) { + // comment + if (true) {} + } + + if ( + true // comment + ); } void commentsIfBlockComment() { @@ -24,6 +50,8 @@ void commentsIfBlockComment() { if (t) /* test */{} if /* test */(t) {} + + if (true/*comment*/); } void commentsElseLineComment() { diff --git a/packages/prettier-plugin-java/test/unit-test/constructors/_input.java b/packages/prettier-plugin-java/test/unit-test/constructors/_input.java index a8686bef..bddbc275 100644 --- a/packages/prettier-plugin-java/test/unit-test/constructors/_input.java +++ b/packages/prettier-plugin-java/test/unit-test/constructors/_input.java @@ -49,4 +49,34 @@ public Constructors() { public GenericConstructor(T genericParameter) {} public GenericConstructor(T genericParameter) {} + + Constructors() { + // comment + } + + Constructors() // comment + {} + + Constructors(String arg) // comment + {} + + Constructors() // comment + { + System.out.println("Oops"); + } + + Constructors(String arg) // comment + { + System.out.println("Oops"); + } + + Constructors() // comment + { + if (true) {} + } + + Constructors(String arg) // comment + { + if (true) {} + } } diff --git a/packages/prettier-plugin-java/test/unit-test/constructors/_output.java b/packages/prettier-plugin-java/test/unit-test/constructors/_output.java index cb354a9b..5f125cbf 100644 --- a/packages/prettier-plugin-java/test/unit-test/constructors/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/constructors/_output.java @@ -62,4 +62,28 @@ public Constructors() { public GenericConstructor(T genericParameter) {} public GenericConstructor(T genericParameter) {} + + Constructors() { + // comment + } + + Constructors() {} // comment + + Constructors(String arg) {} // comment + + Constructors() { // comment + System.out.println("Oops"); + } + + Constructors(String arg) { // comment + System.out.println("Oops"); + } + + Constructors() { // comment + if (true) {} + } + + Constructors(String arg) { // comment + if (true) {} + } } diff --git a/packages/prettier-plugin-java/test/unit-test/empty_statement/_output.java b/packages/prettier-plugin-java/test/unit-test/empty_statement/_output.java index f1b81fd0..75286c6d 100644 --- a/packages/prettier-plugin-java/test/unit-test/empty_statement/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/empty_statement/_output.java @@ -11,33 +11,38 @@ public void emptyStatementWithComment() { public void simpleForWithEmptyStatement() { for (;;); - for (;;) /*test*/; + /*test*/ + for (;;); for (;;);/*test*/ - for (;;) /*test*/;/*test*/ + /*test*/ + for (;;);/*test*/ } public void simpleForWithEmptyStatement() { for (;;); - for (;;) /*test*/; + /*test*/ + for (;;); for (;;);/*test*/ - for (;;) /*test*/;/*test*/ + /*test*/ + for (;;);/*test*/ } public void forEachWithEmptyStatement(List list) { for (String str : list); - for (String str : list) /*test*/; + for (String str : list/*test*/); for (String str : list);/*test*/ } public void ifElseWithEmptyStatements() { - if (test); else { + if (test); + else { System.out.println("one"); } @@ -45,15 +50,18 @@ public void ifElseWithEmptyStatements() { System.out.println("two"); } else; - if (test); else; + if (test); + else; } public void ifElseWithEmptyStatementsWithComments() { - if (test) /*test*/; else { + if (test/*test*/); + else { System.out.println("one"); } - if (test); /*test*/else { + if (test); + /*test*/else { System.out.println("one"); } @@ -65,15 +73,17 @@ public void ifElseWithEmptyStatementsWithComments() { System.out.println("two"); } else;/*test*/ - if (test); /*test*/else;/*test*/ + if (test); + /*test*/else;/*test*/ - if (test) /*test*/; else /*test*/; + if (test/*test*/); + else /*test*/; } public void simpleWhileWithEmptyStatement(boolean one) { while (one); - while (one) /*test*/; + while (one/*test*/); while (one);/*test*/ } diff --git a/packages/prettier-plugin-java/test/unit-test/for/_input.java b/packages/prettier-plugin-java/test/unit-test/for/_input.java index f6f74454..81048a1d 100644 --- a/packages/prettier-plugin-java/test/unit-test/for/_input.java +++ b/packages/prettier-plugin-java/test/unit-test/for/_input.java @@ -36,4 +36,78 @@ public void continueWithIdentifier() { } } + void basicLineComments() { + + for (int i = 0; i < 1; i++) // comment + System.out.println("Oops"); + + for (int i = 0; i < 1; i++) { + // comment + } + + for (int i = 0;;) //comment + ; + + for (int i = 0; i < 1;) // comment + {} + + for (;;) // comment + {} + + for (; i < 1;) // comment + {} + + for (;; i++) // comment + {} + + for ( + int i = 0; + i < 1; + i++ // hi + ) // comment + { + System.out.println("Oops"); + } + + for (int i = 0; i < 1; i++) // comment + { + if (true) {} + } + } + + void basicBlockComments() { + for (;;)/*comment*/; + + for (int i = 0; i < 1; i++) /*comment*/; + } + + void eachLineComments() { + for (String s : strings) // comment + System.out.println("Oops"); + + for (String s : strings) { + // comment + } + + for (String s : strings) // comment + {} + + for ( + String s : strings // comment + ) {} + + for (String s : strings) // comment + { + System.out.println("Oops"); + } + + for (String s : strings) // comment + { + if (true) {} + } + } + + void eachBlockComments() { + for (String s : strings)/*comment*/; + } } diff --git a/packages/prettier-plugin-java/test/unit-test/for/_output.java b/packages/prettier-plugin-java/test/unit-test/for/_output.java index fe269b6f..7046855d 100644 --- a/packages/prettier-plugin-java/test/unit-test/for/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/for/_output.java @@ -35,4 +35,98 @@ public void continueWithIdentifier() { System.out.println(i); } } + + void basicLineComments() { + for ( + int i = 0; + i < 1; + i++ // comment + ) + System.out.println("Oops"); + + for (int i = 0; i < 1; i++) { + // comment + } + + for ( + int i = 0; //comment + ; + + ); + + for ( + int i = 0; + i < 1; // comment + + ) {} + + // comment + for (;;) {} + + for ( + ; + i < 1; // comment + + ) {} + + for ( + ; + ; + i++ // comment + ) {} + + for ( + int i = 0; + i < 1; + i++ // hi + // comment + ) { + System.out.println("Oops"); + } + + for ( + int i = 0; + i < 1; + i++ // comment + ) { + if (true) {} + } + } + + void basicBlockComments() { + /*comment*/ + for (;;); + + for (int i = 0; i < 1; i++/*comment*/); + } + + void eachLineComments() { + for (String s : strings) + // comment + System.out.println("Oops"); + + for (String s : strings) { + // comment + } + + for (String s : strings) { + // comment + } + + for (String s : strings) {} // comment + + for (String s : strings) { + // comment + System.out.println("Oops"); + } + + for (String s : strings) { + // comment + if (true) {} + } + } + + void eachBlockComments() { + for (String s : strings/*comment*/); + } } diff --git a/packages/prettier-plugin-java/test/unit-test/sealed/_output.java b/packages/prettier-plugin-java/test/unit-test/sealed/_output.java index c0d51562..dcbe8c06 100644 --- a/packages/prettier-plugin-java/test/unit-test/sealed/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/sealed/_output.java @@ -46,11 +46,12 @@ default Shape rotate(double angle) { } default String areaMessage() { - if (this instanceof Circle) return "Circle: " + area(); else if ( - this instanceof Rectangle - ) return "Rectangle: " + area(); else if ( - this instanceof RightTriangle - ) return "Triangle: " + area(); + if (this instanceof Circle) + return "Circle: " + area(); + else if (this instanceof Rectangle) + return "Rectangle: " + area(); + else if (this instanceof RightTriangle) + return "Triangle: " + area(); // :( throw new IllegalArgumentException(); } 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 637949be..77bc51b5 100644 --- a/packages/prettier-plugin-java/test/unit-test/switch/_input.java +++ b/packages/prettier-plugin-java/test/unit-test/switch/_input.java @@ -136,4 +136,19 @@ static String shouldFormatSwitchBlocksWithEmptyLastBlock(Object o) { log.info("Done !"); } + + void comments() { + switch (value) { + // comment + } + + switch (value) // comment + {} + + switch (value) // comment + { + case "a": + System.out.println("Oops"); + } + } } 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 2af1d6ec..55bac60a 100644 --- a/packages/prettier-plugin-java/test/unit-test/switch/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/switch/_output.java @@ -151,4 +151,21 @@ static String shouldFormatSwitchBlocksWithEmptyLastBlock(Object o) { log.info("Done !"); } + + void comments() { + switch (value) { + // comment + } + + switch ( + value // comment + ) {} + + switch ( + value // comment + ) { + case "a": + System.out.println("Oops"); + } + } } diff --git a/packages/prettier-plugin-java/test/unit-test/try_catch/_input.java b/packages/prettier-plugin-java/test/unit-test/try_catch/_input.java index cbd661ce..16a6cc97 100644 --- a/packages/prettier-plugin-java/test/unit-test/try_catch/_input.java +++ b/packages/prettier-plugin-java/test/unit-test/try_catch/_input.java @@ -62,4 +62,41 @@ void multiResourceTry() { } } + void catchComments() { + try {} catch (Exception e) { + // comment + } + + try {} catch (Exception e) // comment + {} + + try {} catch (Exception e) // comment + { + System.out.println("Oops"); + } + + try {} catch (Exception e) // comment + { + if (true) {} + } + } + + void resourcesComments() { + try (InputStream in = getClass().getResourceAsStream("file.txt")) { + // comment + } + + try (InputStream in = getClass().getResourceAsStream("file.txt")) // comment + {} + + try (InputStream in = getClass().getResourceAsStream("file.txt")) // comment + { + System.out.println("Oops"); + } + + try (InputStream in = getClass().getResourceAsStream("file.txt")) // comment + { + if (true) {} + } + } } diff --git a/packages/prettier-plugin-java/test/unit-test/try_catch/_output.java b/packages/prettier-plugin-java/test/unit-test/try_catch/_output.java index e45188cf..ba8b29df 100644 --- a/packages/prettier-plugin-java/test/unit-test/try_catch/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/try_catch/_output.java @@ -68,4 +68,42 @@ void multiResourceTry() { System.out.println("Warning: Not breaking multi exceptions"); } } + + void catchComments() { + try {} catch (Exception e) { + // comment + } + + try {} catch ( + Exception e // comment + ) {} + + try {} catch ( + Exception e // comment + ) { + System.out.println("Oops"); + } + + try {} catch ( + Exception e // comment + ) { + if (true) {} + } + } + + void resourcesComments() { + try (InputStream in = getClass().getResourceAsStream("file.txt")) { + // comment + } + + try (InputStream in = getClass().getResourceAsStream("file.txt")) {} // comment + + try (InputStream in = getClass().getResourceAsStream("file.txt")) { // comment + System.out.println("Oops"); + } + + try (InputStream in = getClass().getResourceAsStream("file.txt")) { // comment + if (true) {} + } + } } diff --git a/packages/prettier-plugin-java/test/unit-test/while/_input.java b/packages/prettier-plugin-java/test/unit-test/while/_input.java index d621bf20..0a9b1879 100644 --- a/packages/prettier-plugin-java/test/unit-test/while/_input.java +++ b/packages/prettier-plugin-java/test/unit-test/while/_input.java @@ -12,4 +12,72 @@ public void doWhile(boolean one) { } while (one); } + void whileLineComments() { + while (true) // comment + System.out.println("Oops"); + + while (true) throw new RuntimeException(); + + while (true) { + // comment + } + + while (true) // comment + {} + + while (true) // comment + { + System.out.println("Oops"); + } + + while ( + true // test + ) // comment + { + if (true) {} + } + + while (true) // comment + ; + } + + void whileBlockComments() { + while (true) /*comment*/; + } + + void doWhileLineComments() { + do { + // comment + } while (true); + + do // comment + {} while (true); + + do // comment + { + System.out.println("Oops"); + } while (true); + + do // comment + { + if (true) {} + } while (true); + + do {} while (true) // comment + ; + + do { + System.out.println("Oops"); + } while (true) // comment + ; + + do { + if (true) {} + } while (true) // comment + ; + } + + void doWhileBlockComments() { + do {} while (true) /*comment*/; + } } diff --git a/packages/prettier-plugin-java/test/unit-test/while/_output.java b/packages/prettier-plugin-java/test/unit-test/while/_output.java index 5bc96df4..c5cd2cee 100644 --- a/packages/prettier-plugin-java/test/unit-test/while/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/while/_output.java @@ -11,4 +11,76 @@ public void doWhile(boolean one) { System.out.println("one"); } while (one); } + + void whileLineComments() { + while (true) + // comment + System.out.println("Oops"); + + while (true) throw new RuntimeException(); + + while (true) { + // comment + } + + while (true) { + // comment + } + + while (true) { + // comment + System.out.println("Oops"); + } + + while ( + true // test + ) { + // comment + if (true) {} + } + + while ( + true // comment + ); + } + + void whileBlockComments() { + while (true/*comment*/); + } + + void doWhileLineComments() { + do { + // comment + } while (true); + + do {} while (true); // comment + + do { // comment + System.out.println("Oops"); + } while (true); + + do { // comment + if (true) {} + } while (true); + + do {} while ( + true // comment + ); + + do { + System.out.println("Oops"); + } while ( + true // comment + ); + + do { + if (true) {} + } while ( + true // comment + ); + } + + void doWhileBlockComments() { + do {} while (true/*comment*/); + } }