diff --git a/packages/prettier-plugin-java/src/printers/expressions.ts b/packages/prettier-plugin-java/src/printers/expressions.ts index e79929bf..8ac90bf4 100644 --- a/packages/prettier-plugin-java/src/printers/expressions.ts +++ b/packages/prettier-plugin-java/src/printers/expressions.ts @@ -54,7 +54,6 @@ import forEach from "lodash/forEach.js"; import { builders } from "prettier/doc"; import { BaseCstPrettierPrinter } from "../base-cst-printer.js"; import { isAnnotationCstNode } from "../types/utils.js"; -import { isArgumentListHuggable } from "../utils/expressions-utils.js"; import { printArgumentListWithBraces } from "../utils/index.js"; import { printTokenWithComments } from "./comments/format-comments.js"; import { handleCommentsBinaryExpression } from "./comments/handle-comments.js"; @@ -625,7 +624,7 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter { ); } - argumentList(ctx: ArgumentListCtx, params?: { shouldBreak?: boolean }) { + argumentList(ctx: ArgumentListCtx, params?: { isHuggable?: boolean, shouldBreak?: boolean }) { const shouldBreak = params?.shouldBreak; const expressions = this.mapVisit(ctx.expression, params); @@ -635,7 +634,7 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter { const commas = ctx.Comma?.map(comma => concat([comma, commaSuffix])); const otherArguments = rejectAndJoinSeps(commas, expressions); - if (lastArgument && isArgumentListHuggable(ctx)) { + if (lastArgument && params?.isHuggable) { const argumentListGroupId = Symbol("argumentList"); const separator = shouldBreak === true ? hardline : shouldBreak === false ? "" : softline; diff --git a/packages/prettier-plugin-java/src/utils/expressions-utils.ts b/packages/prettier-plugin-java/src/utils/expressions-utils.ts deleted file mode 100644 index 223e5deb..00000000 --- a/packages/prettier-plugin-java/src/utils/expressions-utils.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { ArgumentListCtx } from "java-parser"; - -export function isArgumentListHuggable(argumentList: ArgumentListCtx) { - const expressions = argumentList.expression; - return ( - expressions.filter(expression => expression.children.lambdaExpression) - .length === 1 && - (expressions.length === 1 || - expressions[expressions.length - 1].children.lambdaExpression?.[0] - .children.lambdaBody[0].children.block !== undefined) - ); -} diff --git a/packages/prettier-plugin-java/src/utils/printArgumentListWithBraces.ts b/packages/prettier-plugin-java/src/utils/printArgumentListWithBraces.ts index 8a2255b9..8d17d3d7 100644 --- a/packages/prettier-plugin-java/src/utils/printArgumentListWithBraces.ts +++ b/packages/prettier-plugin-java/src/utils/printArgumentListWithBraces.ts @@ -1,6 +1,5 @@ -import { ArgumentListCstNode, IToken } from "java-parser"; +import type { ArgumentListCstNode, ArgumentListCtx, IToken } from "java-parser"; import { builders, utils } from "prettier/doc"; -import { isArgumentListHuggable } from "./expressions-utils.js"; import { putIntoBraces } from "../printers/printer-utils.js"; const { breakParent, conditionalGroup, softline } = builders; @@ -11,10 +10,17 @@ export default function printArgumentListWithBraces( rBrace: IToken, lBrace: IToken ) { - const argumentListCtx = argumentListNodes?.[0].children; - if (argumentListCtx && isArgumentListHuggable(argumentListCtx)) { + if ( + argumentListNodes && + !argumentListNodes[0].leadingComments && + !rBrace.leadingComments && + isArgumentListHuggable(argumentListNodes[0].children) + ) { const [flat, expanded] = [false, true].map(shouldBreak => { - const argumentList = this.visit(argumentListNodes, { shouldBreak }); + const argumentList = this.visit(argumentListNodes, { + isHuggable: true, + shouldBreak + }); return putIntoBraces(argumentList, "", lBrace, rBrace); }); return [ @@ -26,3 +32,13 @@ export default function printArgumentListWithBraces( const argumentList = this.visit(argumentListNodes); return putIntoBraces(argumentList, softline, lBrace, rBrace); } + +function isArgumentListHuggable(argumentList: ArgumentListCtx) { + const expressions = argumentList.expression; + return ( + (expressions.length === 1 || + expressions[expressions.length - 1].children.lambdaExpression?.[0] + .children.lambdaBody[0].children.block !== undefined) && + expressions.filter(({ children }) => children.lambdaExpression).length === 1 + ); +} diff --git a/packages/prettier-plugin-java/test/unit-test/lambda/_input.java b/packages/prettier-plugin-java/test/unit-test/lambda/_input.java index ef0f8104..5cc2c075 100644 --- a/packages/prettier-plugin-java/test/unit-test/lambda/_input.java +++ b/packages/prettier-plugin-java/test/unit-test/lambda/_input.java @@ -208,6 +208,26 @@ void argumentAfterLambdaWithBlock() { return f; }, g); } + + void lambdaWithLeadingComments() { + System.out.println( + List.of(1, 2, 3).stream().map( + // a very long comment which explains the beatifullness of multiplication by 2 + // yes this is very important + v -> v * 2 + ).collect(Collectors.summingInt(v -> v)) + ); + } + + void lambdaWithTrailingComments() { + System.out.println( + List.of(1, 2, 3).stream().map( + v -> v * 2 + // a very long comment which explains the beatifullness of multiplication by 2 + // yes this is very important + ).collect(Collectors.summingInt(v -> v)) + ); + } } class T { diff --git a/packages/prettier-plugin-java/test/unit-test/lambda/_output.java b/packages/prettier-plugin-java/test/unit-test/lambda/_output.java index 2cc00ae9..8fd77136 100644 --- a/packages/prettier-plugin-java/test/unit-test/lambda/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/lambda/_output.java @@ -220,6 +220,32 @@ void argumentAfterLambdaWithBlock() { g ); } + + void lambdaWithLeadingComments() { + System.out.println( + List.of(1, 2, 3) + .stream() + .map( + // a very long comment which explains the beatifullness of multiplication by 2 + // yes this is very important + v -> v * 2 + ) + .collect(Collectors.summingInt(v -> v)) + ); + } + + void lambdaWithTrailingComments() { + System.out.println( + List.of(1, 2, 3) + .stream() + .map( + v -> v * 2 + // a very long comment which explains the beatifullness of multiplication by 2 + // yes this is very important + ) + .collect(Collectors.summingInt(v -> v)) + ); + } } class T {