diff --git a/packages/prettier-plugin-java/src/printers/classes.ts b/packages/prettier-plugin-java/src/printers/classes.ts index 78f3645e..8c906fb9 100644 --- a/packages/prettier-plugin-java/src/printers/classes.ts +++ b/packages/prettier-plugin-java/src/printers/classes.ts @@ -24,6 +24,7 @@ import { hasLeadingComments, hasLeadingLineComments } from "./comments/comments-utils.js"; +import { handleCommentsParameters } from "./comments/handle-comments.js"; import { builders } from "prettier/doc"; import { BaseCstPrettierPrinter } from "../base-cst-printer.js"; import { @@ -569,6 +570,11 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter { } methodDeclarator(ctx: MethodDeclaratorCtx) { + const parameters = [ + ...(ctx.receiverParameter ?? []), + ...(ctx.formalParameterList?.[0].children.formalParameter ?? []) + ]; + handleCommentsParameters(ctx.LBrace[0], parameters, ctx.RBrace[0]); const identifier = printTokenWithComments(ctx.Identifier[0]); const receiverParameter = this.visit(ctx.receiverParameter); const formalParameterList = this.visit(ctx.formalParameterList); @@ -719,6 +725,11 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter { } constructorDeclarator(ctx: ConstructorDeclaratorCtx) { + const parameters = + ctx.receiverParameter ?? + ctx.formalParameterList?.[0].children.formalParameter ?? + []; + handleCommentsParameters(ctx.LBrace[0], parameters, ctx.RBrace[0]); const typeParameters = this.visit(ctx.typeParameters); const simpleTypeName = this.visit(ctx.simpleTypeName); const receiverParameter = this.visit(ctx.receiverParameter); @@ -948,6 +959,9 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter { ]); } recordHeader(ctx: RecordHeaderCtx) { + const recordComponents = + ctx.recordComponentList?.[0].children.recordComponent ?? []; + handleCommentsParameters(ctx.LBrace[0], recordComponents, ctx.RBrace[0]); const recordComponentList = this.visit(ctx.recordComponentList); return putIntoBraces( recordComponentList, 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 3aeed30e..bb6ac85f 100644 --- a/packages/prettier-plugin-java/src/printers/comments/handle-comments.ts +++ b/packages/prettier-plugin-java/src/printers/comments/handle-comments.ts @@ -1,6 +1,7 @@ import { hasLeadingComments, hasTrailingComments } from "./comments-utils.js"; import { BinaryExpressionCtx, + CstNode, IToken, UnaryExpressionCstNode } from "java-parser"; @@ -10,6 +11,38 @@ export function handleCommentsBinaryExpression(ctx: BinaryExpressionCtx) { moveExpressionTrailingCommentsToNextOperator(ctx); } +export function handleCommentsParameters( + lBrace: IToken, + parameters: CstNode[] | IToken[], + rBrace: IToken +) { + const lBraceTrailingComments = lBrace.trailingComments; + const firstParameter = parameters.at(0); + if (lBraceTrailingComments && firstParameter) { + delete lBrace.trailingComments; + firstParameter.leadingComments = [ + ...lBraceTrailingComments, + ...(firstParameter.leadingComments ?? []) + ]; + } + const lastParameter = parameters.at(-1); + const rBraceLeadingComments = rBrace.leadingComments; + if (rBraceLeadingComments) { + delete rBrace.leadingComments; + if (lastParameter) { + lastParameter.trailingComments = [ + ...(lastParameter.trailingComments ?? []), + ...rBraceLeadingComments + ]; + } else { + lBrace.trailingComments = [ + ...(lBrace.trailingComments ?? []), + ...rBraceLeadingComments + ]; + } + } +} + function moveOperatorLeadingCommentsToNextExpression(ctx: BinaryExpressionCtx) { let unaryExpressionIndex = 1; ctx.BinaryOperator?.forEach(binaryOperator => { diff --git a/packages/prettier-plugin-java/src/printers/expressions.ts b/packages/prettier-plugin-java/src/printers/expressions.ts index df04bbcb..e3bd9742 100644 --- a/packages/prettier-plugin-java/src/printers/expressions.ts +++ b/packages/prettier-plugin-java/src/printers/expressions.ts @@ -56,13 +56,16 @@ import { } from "java-parser/api"; import forEach from "lodash/forEach.js"; -import { builders } from "prettier/doc"; +import { builders, utils } from "prettier/doc"; import { BaseCstPrettierPrinter } from "../base-cst-printer.js"; import { isAnnotationCstNode } from "../types/utils.js"; import { printArgumentListWithBraces } from "../utils/index.js"; import { printTokenWithComments } from "./comments/format-comments.js"; -import { handleCommentsBinaryExpression } from "./comments/handle-comments.js"; -import { concat, dedent, fill, group, indent } from "./prettier-builder.js"; +import { + handleCommentsBinaryExpression, + handleCommentsParameters +} from "./comments/handle-comments.js"; +import { concat, dedent, group, indent, join } from "./prettier-builder.js"; import { binary, findDeepElementInPartsArray, @@ -77,61 +80,70 @@ import { sortTokens } from "./printer-utils.js"; -const { hardline, ifBreak, line, lineSuffixBoundary, softline } = builders; +const { + breakParent, + conditionalGroup, + ifBreak, + label, + line, + lineSuffixBoundary, + softline +} = builders; +const { removeLines, willBreak } = utils; export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter { expression(ctx: ExpressionCtx, params: any) { - return this.visitSingle(ctx, params); + const expression = this.visitSingle(ctx, params); + return params?.hug && expression.label !== undefined + ? label(expression.label, expression) + : expression; } - lambdaExpression( - ctx: LambdaExpressionCtx, - params?: { shouldBreak?: boolean } - ) { - const lambdaParameters = group(this.visit(ctx.lambdaParameters, params)); + lambdaExpression(ctx: LambdaExpressionCtx, params?: { hug?: boolean }) { + const lambdaParameters = group(this.visit(ctx.lambdaParameters)); const lambdaBody = this.visit(ctx.lambdaBody); const isLambdaBodyABlock = ctx.lambdaBody[0].children.block !== undefined; - if (isLambdaBodyABlock) { - return rejectAndJoin(" ", [lambdaParameters, ctx.Arrow[0], lambdaBody]); + const suffix = [ + " ", + ctx.Arrow[0], + ...(isLambdaBodyABlock + ? [" ", lambdaBody] + : [group(indent([line, lambdaBody]))]) + ]; + if (params?.hug) { + return willBreak(lambdaParameters) + ? label({ huggable: false }, concat([lambdaParameters, ...suffix])) + : concat([removeLines(lambdaParameters), ...suffix]); } - - return group( - indent( - rejectAndJoin(line, [ - rejectAndJoin(" ", [lambdaParameters, ctx.Arrow[0]]), - lambdaBody - ]) - ) - ); + return concat([lambdaParameters, ...suffix]); } - lambdaParameters( - ctx: LambdaParametersCtx, - params?: { shouldBreak?: boolean } - ) { + lambdaParameters(ctx: LambdaParametersCtx) { if (ctx.lambdaParametersWithBraces) { - return this.visitSingle(ctx, params); + return this.visitSingle(ctx); } return printTokenWithComments(this.getSingle(ctx) as IToken); } - lambdaParametersWithBraces( - ctx: LambdaParametersWithBracesCtx, - params?: { shouldBreak?: boolean } - ) { - const lambdaParameterList = this.visit(ctx.lambdaParameterList, params); + lambdaParametersWithBraces(ctx: LambdaParametersWithBracesCtx) { + const lambdaParameters = + ctx.lambdaParameterList?.[0].children.explicitLambdaParameterList?.[0] + .children.lambdaParameter ?? + ctx.lambdaParameterList?.[0].children.inferredLambdaParameterList?.[0] + .children.Identifier ?? + []; + handleCommentsParameters(ctx.LBrace[0], lambdaParameters, ctx.RBrace[0]); + const lambdaParameterList = this.visit(ctx.lambdaParameterList); if (findDeepElementInPartsArray(lambdaParameterList, ",")) { - const separator = params?.shouldBreak === false ? "" : softline; - const content = putIntoBraces( - lambdaParameterList, - separator, + return concat([ ctx.LBrace[0], + indent([softline, lambdaParameterList]), + softline, ctx.RBrace[0] - ); - return content; + ]); } // removing braces when only no comments attached @@ -154,29 +166,18 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter { return lambdaParameterList; } - lambdaParameterList( - ctx: LambdaParameterListCtx, - params?: { shouldBreak?: boolean } - ) { - return this.visitSingle(ctx, params); + lambdaParameterList(ctx: LambdaParameterListCtx) { + return this.visitSingle(ctx); } - inferredLambdaParameterList( - ctx: InferredLambdaParameterListCtx, - params?: { shouldBreak?: boolean } - ) { - const commaSuffix = params?.shouldBreak === false ? " " : line; - const commas = ctx.Comma?.map(comma => concat([comma, commaSuffix])); + inferredLambdaParameterList(ctx: InferredLambdaParameterListCtx) { + const commas = ctx.Comma?.map(comma => concat([comma, line])); return rejectAndJoinSeps(commas, ctx.Identifier); } - explicitLambdaParameterList( - ctx: ExplicitLambdaParameterListCtx, - params?: { shouldBreak?: boolean } - ) { + explicitLambdaParameterList(ctx: ExplicitLambdaParameterListCtx) { const lambdaParameter = this.mapVisit(ctx.lambdaParameter); - const commaSuffix = params?.shouldBreak === false ? " " : line; - const commas = ctx.Comma?.map(comma => concat([comma, commaSuffix])); + const commas = ctx.Comma?.map(comma => concat([comma, line])); return rejectAndJoinSeps(commas, lambdaParameter); } @@ -626,31 +627,44 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter { ); } - argumentList( - ctx: ArgumentListCtx, - params?: { isHuggable?: boolean; shouldBreak?: boolean } - ) { - const shouldBreak = params?.shouldBreak; - const expressions = this.mapVisit(ctx.expression, params); - - const lastArgument = expressions.pop(); - const commaSuffix = - shouldBreak === true ? hardline : shouldBreak === false ? " " : line; - const commas = ctx.Comma?.map(comma => concat([comma, commaSuffix])); - const otherArguments = rejectAndJoinSeps(commas, expressions); - - if (lastArgument && params?.isHuggable) { - const argumentListGroupId = Symbol("argumentList"); - const separator = - shouldBreak === true ? hardline : shouldBreak === false ? "" : softline; - return concat([ - group([separator, otherArguments], { id: argumentListGroupId }), - ifBreak([lastArgument, dedent(separator)], dedent(lastArgument), { - groupId: argumentListGroupId - }) - ]); + argumentList(ctx: ArgumentListCtx) { + const headArgs = this.mapVisit(ctx.expression.slice(0, -1)).map( + (expression, index) => concat([expression, ctx.Comma![index], line]) + ); + const lastExpression = ctx.expression.at(-1); + const lastArg = this.visit(lastExpression); + + if (this.isArgumentListHuggable(ctx)) { + const huggedLastArg = this.visit(lastExpression, { hug: true }); + const lastArgNotHuggable = + typeof huggedLastArg === "object" && + !Array.isArray(huggedLastArg) && + huggedLastArg.type === "label" && + huggedLastArg.label?.huggable === false; + + if (lastArgNotHuggable || headArgs.some(willBreak)) { + return group([indent([line, ...headArgs, lastArg]), line], { + shouldBreak: true + }); + } + const suffix = lastExpression?.children.lambdaExpression?.[0].children + .lambdaBody[0].children.block + ? "" + : line; + const hugged = [ + ...headArgs, + group([huggedLastArg, suffix], { shouldBreak: true }) + ]; + const expanded = group([indent([line, ...headArgs, lastArg]), line], { + shouldBreak: true + }); + + return willBreak(huggedLastArg) + ? [breakParent, conditionalGroup([hugged, expanded])] + : conditionalGroup([[...headArgs, huggedLastArg], hugged, expanded]); } - return rejectAndConcat([otherArguments, lastArgument]); + + return group([indent([softline, ...headArgs, lastArg]), softline]); } arrayCreationExpression(ctx: ArrayCreationExpressionCtx) { @@ -760,6 +774,9 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter { } recordPattern(ctx: RecordPatternCtx) { + const componentPatterns = + ctx.componentPatternList?.[0].children.componentPattern ?? []; + handleCommentsParameters(ctx.LBrace[0], componentPatterns, ctx.RBrace[0]); const referenceType = this.visit(ctx.referenceType); const componentPatternList = this.visit(ctx.componentPatternList); return concat([ @@ -799,6 +816,26 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter { return "isRefTypeInMethodRef"; } + private isArgumentListHuggable(argumentList: ArgumentListCtx) { + const expressions = argumentList.expression; + const lastArgument = expressions.at(-1); + const lastArgumentLambdaBodyExpression = + lastArgument?.children.lambdaExpression?.[0].children.lambdaBody[0] + .children.expression?.[0].children; + const lastArgumentLambdaBodyTernaryExpression = + lastArgumentLambdaBodyExpression?.ternaryExpression?.[0].children; + return ( + !lastArgument?.leadingComments && + !lastArgument?.trailingComments && + (!lastArgumentLambdaBodyExpression || + lastArgumentLambdaBodyTernaryExpression?.QuestionMark !== undefined || + lastArgumentLambdaBodyTernaryExpression?.binaryExpression[0].children + .unaryExpression.length === 1) && + expressions.findIndex(({ children }) => children.lambdaExpression) === + expressions.length - 1 + ); + } + private isBreakableNewExpression(newExpression?: NewExpressionCtx) { const arrayCreationExpression = newExpression?.arrayCreationExpression?.[0].children; diff --git a/packages/prettier-plugin-java/src/printers/interfaces.ts b/packages/prettier-plugin-java/src/printers/interfaces.ts index 1daf4737..ae00d687 100644 --- a/packages/prettier-plugin-java/src/printers/interfaces.ts +++ b/packages/prettier-plugin-java/src/printers/interfaces.ts @@ -1,5 +1,6 @@ import { concat, group, indent } from "./prettier-builder.js"; import { printTokenWithComments } from "./comments/format-comments.js"; +import { handleCommentsParameters } from "./comments/handle-comments.js"; import { displaySemicolon, getInterfaceBodyDeclarationsSeparator, @@ -278,6 +279,11 @@ export class InterfacesPrettierVisitor extends BaseCstPrettierPrinter { let annoArgs: Doc = ""; if (ctx.LBrace) { + const elementValues = + ctx.elementValuePairList?.[0].children.elementValuePair ?? + ctx.elementValue ?? + []; + handleCommentsParameters(ctx.LBrace[0], elementValues, ctx.RBrace![0]); if (ctx.elementValuePairList) { annoArgs = putIntoBraces( this.visit(ctx.elementValuePairList), diff --git a/packages/prettier-plugin-java/src/printers/printer-utils.ts b/packages/prettier-plugin-java/src/printers/printer-utils.ts index f408935d..75232c0f 100644 --- a/packages/prettier-plugin-java/src/printers/printer-utils.ts +++ b/packages/prettier-plugin-java/src/printers/printer-utils.ts @@ -39,7 +39,7 @@ import { join } from "./prettier-builder.js"; -const { indent, hardline, line } = builders; +const { indent, hardline, line, lineSuffixBoundary, softline } = builders; const orderedModifiers = [ "Public", @@ -583,7 +583,11 @@ export function putIntoBraces( if (isEmptyDoc(argument)) { if (rightBraceLeadingComments.length === 0) { - return concat([LBrace, RBrace]); + return group([ + indent(printTokenWithComments(LBrace)), + ...(LBrace.trailingComments ? [softline, lineSuffixBoundary] : []), + RBrace + ]); } contentInsideBraces = [separator, ...rightBraceLeadingComments]; } else if (rightBraceLeadingComments.length !== 0) { @@ -650,11 +654,11 @@ export function binary(nodes: Doc[], tokens: IToken[], isRoot = false): Doc { return group(join(line, level)); } } else { - const content = indent(binary(nodes, tokens)); + const content = binary(nodes, tokens); nodes.unshift( levelOperator !== undefined && needsParentheses(nextOperator, levelOperator) - ? concat(["(", content, ")"]) + ? concat(["(", indent(content), ")"]) : content ); } diff --git a/packages/prettier-plugin-java/src/utils/printArgumentListWithBraces.ts b/packages/prettier-plugin-java/src/utils/printArgumentListWithBraces.ts index 8d17d3d7..95702286 100644 --- a/packages/prettier-plugin-java/src/utils/printArgumentListWithBraces.ts +++ b/packages/prettier-plugin-java/src/utils/printArgumentListWithBraces.ts @@ -1,44 +1,44 @@ import type { ArgumentListCstNode, ArgumentListCtx, IToken } from "java-parser"; -import { builders, utils } from "prettier/doc"; -import { putIntoBraces } from "../printers/printer-utils.js"; +import { builders } from "prettier/doc"; +import { handleCommentsParameters } from "../printers/comments/handle-comments.js"; +import { indent } from "../printers/prettier-builder.js"; +import { rejectAndConcat } from "../printers/printer-utils.js"; -const { breakParent, conditionalGroup, softline } = builders; -const { willBreak } = utils; +const { lineSuffixBoundary, softline } = builders; export default function printArgumentListWithBraces( argumentListNodes: ArgumentListCstNode[] | undefined, rBrace: IToken, lBrace: IToken ) { - if ( - argumentListNodes && - !argumentListNodes[0].leadingComments && - !rBrace.leadingComments && - isArgumentListHuggable(argumentListNodes[0].children) - ) { - const [flat, expanded] = [false, true].map(shouldBreak => { - const argumentList = this.visit(argumentListNodes, { - isHuggable: true, - shouldBreak - }); - return putIntoBraces(argumentList, "", lBrace, rBrace); - }); - return [ - willBreak(flat) ? breakParent : "", - conditionalGroup([flat, expanded]) - ]; + const argumentListNode = argumentListNodes?.[0]; + const expressions = argumentListNode?.children.expression ?? []; + if (argumentListNode) { + const { leadingComments, trailingComments } = argumentListNode; + delete argumentListNode.leadingComments; + delete argumentListNode.trailingComments; + if (leadingComments) { + const firstExpression = expressions[0]; + firstExpression.leadingComments = [ + ...leadingComments, + ...(firstExpression.leadingComments ?? []) + ]; + } + if (trailingComments) { + const lastExpression = expressions.at(-1)!; + lastExpression.trailingComments = [ + ...(lastExpression.trailingComments ?? []), + ...trailingComments + ]; + } } + handleCommentsParameters(lBrace, expressions, rBrace); 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 - ); + const contents = argumentList + ? [argumentList] + : lBrace.trailingComments + ? [softline, lineSuffixBoundary] + : []; + return rejectAndConcat([indent(lBrace), ...contents, rBrace]); } diff --git a/packages/prettier-plugin-java/test/unit-test/comments/comments-blocks-and-statements/end-of-block/_output.java b/packages/prettier-plugin-java/test/unit-test/comments/comments-blocks-and-statements/end-of-block/_output.java index 25707462..89884e3d 100644 --- a/packages/prettier-plugin-java/test/unit-test/comments/comments-blocks-and-statements/end-of-block/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/comments/comments-blocks-and-statements/end-of-block/_output.java @@ -15,7 +15,8 @@ class D { /* gamma */ } -class E {} // alpha +class E { // alpha +} class F { /* alpha */ @@ -59,9 +60,11 @@ void four( /* gamma */ ) {} - void five() {} // alpha + void five( // alpha + ) {} - void fiveBis() { // alpha + void fiveBis( // alpha + ) { int i; } 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 5cc2c075..1fca347e 100644 --- a/packages/prettier-plugin-java/test/unit-test/lambda/_input.java +++ b/packages/prettier-plugin-java/test/unit-test/lambda/_input.java @@ -209,6 +209,95 @@ void argumentAfterLambdaWithBlock() { }, g); } + void huggableArguments() { + aaaaaaaaaaaaaaaaaaaaaaaa((bbbbbbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccccccc, dddddddddddddddddddddddd) -> eeeeeeeeeeeeeeeeeeeeeeee.ffffffffffffffffffffffff()); + + a.b(c -> d -> eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk)); + + a.b(c -> d && eeeeeeeeee.ffffffffff() ? g && hhhhhhhhhh.iiiiiiiiii() : j && kkkkkkkkkk.llllllllll()); + + a.b(c -> d && eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk) > 0); + + a.b(c, (c0, c1) -> d && eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk) > 0); + + a.b(c -> eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk) > 0); + + a.b(c, (c0, c1) -> eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk) > 0); + + a.b(c -> d && eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk)); + + a.b(c, (c0, c1) -> d && eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk)); + + a.b(c -> eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk)); + + a.b(c -> { + eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk); + }); + + a.b((c0, c1) -> eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk)); + + a.b(c, (c0, c1) -> eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk)); + + a( // comment + (b, c, d) -> e.f()); + + a(( // comment + b, c, d) -> e.f()); + + a((b, // comment + c, d) -> e.f()); + + a((b, c, d // comment + ) -> e.f()); + + a((b, c, d + // comment + ) -> e.f()); + + a( /* comment */ (b, c, d) -> e.f()); + + a(( /* comment */ + b, c, d) -> e.f()); + + a((b, /* comment */ c, d) -> e.f()); + + a((b, c, d /* comment */ + ) -> e.f()); + + a((b, c, d + /* comment */ + ) -> e.f()); + + aaaaaaaaaaaaaaaaaaaaaaaa((bbbbbbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccccccc, dddddddddddddddddddddddd + // comment + ) -> eeeeeeeeeeeeeeeeeeeeeeee.ffffffffffffffffffffffff()); + + aaaaaaaaaaaaaaaaaaaaaaaa( /* comment */ + (bbbbbbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccccccc, dddddddddddddddddddddddd) -> eeeeeeeeeeeeeeeeeeeeeeee.ffffffffffffffffffffffff()); + + aaaaaaaaaaaaaaaaaaaaaaaa( /* comment */ (bbbbbbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccccccc, dddddddddddddddddddddddd) -> eeeeeeeeeeeeeeeeeeeeeeee.ffffffffffffffffffffffff()); + + a.b(c, (c0, c1 + // comment + ) -> d && eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk) > 0); + + a.b(c, (c0, c1 + // comment + ) -> eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk) > 0); + + a.b(c, (c0, c1 + // comment + ) -> d && eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk)); + + a.b((c0, c1 + // comment + ) -> eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk)); + + a.b(c, (c0, c1 + // comment + ) -> eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk)); + } + void lambdaWithLeadingComments() { System.out.println( List.of(1, 2, 3).stream().map( 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 8fd77136..bda0d7c1 100644 --- a/packages/prettier-plugin-java/test/unit-test/lambda/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/lambda/_output.java @@ -221,6 +221,323 @@ void argumentAfterLambdaWithBlock() { ); } + void huggableArguments() { + aaaaaaaaaaaaaaaaaaaaaaaa( + ( + bbbbbbbbbbbbbbbbbbbbbbbb, + cccccccccccccccccccccccc, + dddddddddddddddddddddddd + ) -> eeeeeeeeeeeeeeeeeeeeeeee.ffffffffffffffffffffffff() + ); + + a.b( + c -> + d -> + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) + ); + + a.b(c -> + d && eeeeeeeeee.ffffffffff() + ? g && hhhhhhhhhh.iiiiiiiiii() + : j && kkkkkkkkkk.llllllllll() + ); + + a.b( + c -> + d && + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) > + 0 + ); + + a.b( + c, + (c0, c1) -> + d && + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) > + 0 + ); + + a.b( + c -> + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) > + 0 + ); + + a.b( + c, + (c0, c1) -> + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) > + 0 + ); + + a.b( + c -> + d && + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) + ); + + a.b( + c, + (c0, c1) -> + d && + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) + ); + + a.b(c -> + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) + ); + + a.b(c -> { + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ); + }); + + a.b((c0, c1) -> + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) + ); + + a.b(c, (c0, c1) -> + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) + ); + + a( + // comment + (b, c, d) -> e.f() + ); + + a( + ( + // comment + b, + c, + d + ) -> e.f() + ); + + a( + ( + b, // comment + c, + d + ) -> e.f() + ); + + a( + ( + b, + c, + d // comment + ) -> e.f() + ); + + a( + ( + b, + c, + d + // comment + ) -> e.f() + ); + + a(/* comment */(b, c, d) -> e.f()); + + a( + ( + /* comment */ + b, + c, + d + ) -> e.f() + ); + + a((b, /* comment */c, d) -> e.f()); + + a((b, c, d/* comment */) -> e.f()); + + a( + ( + b, + c, + d + /* comment */ + ) -> e.f() + ); + + aaaaaaaaaaaaaaaaaaaaaaaa( + ( + bbbbbbbbbbbbbbbbbbbbbbbb, + cccccccccccccccccccccccc, + dddddddddddddddddddddddd + // comment + ) -> eeeeeeeeeeeeeeeeeeeeeeee.ffffffffffffffffffffffff() + ); + + aaaaaaaaaaaaaaaaaaaaaaaa( + /* comment */ + ( + bbbbbbbbbbbbbbbbbbbbbbbb, + cccccccccccccccccccccccc, + dddddddddddddddddddddddd + ) -> eeeeeeeeeeeeeeeeeeeeeeee.ffffffffffffffffffffffff() + ); + + aaaaaaaaaaaaaaaaaaaaaaaa( + /* comment */( + bbbbbbbbbbbbbbbbbbbbbbbb, + cccccccccccccccccccccccc, + dddddddddddddddddddddddd + ) -> eeeeeeeeeeeeeeeeeeeeeeee.ffffffffffffffffffffffff() + ); + + a.b( + c, + ( + c0, + c1 + // comment + ) -> + d && + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) > + 0 + ); + + a.b( + c, + ( + c0, + c1 + // comment + ) -> + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) > + 0 + ); + + a.b( + c, + ( + c0, + c1 + // comment + ) -> + d && + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) + ); + + a.b( + ( + c0, + c1 + // comment + ) -> + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) + ); + + a.b( + c, + ( + c0, + c1 + // comment + ) -> + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) + ); + } + void lambdaWithLeadingComments() { System.out.println( List.of(1, 2, 3) diff --git a/packages/prettier-plugin-java/test/unit-test/unnamed-variables-and-patterns/_output.java b/packages/prettier-plugin-java/test/unit-test/unnamed-variables-and-patterns/_output.java index 03484247..9c2376a6 100644 --- a/packages/prettier-plugin-java/test/unit-test/unnamed-variables-and-patterns/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/unnamed-variables-and-patterns/_output.java @@ -39,7 +39,8 @@ void multipleCatchClauses() { } void tryWithResources() { - try (var _ = ScopedContext.acquire()) {} // Unnamed variable + try (var _ = ScopedContext.acquire()) { // Unnamed variable + } } void lambda() {