diff --git a/packages/prettier-plugin-java/src/printers/expressions.ts b/packages/prettier-plugin-java/src/printers/expressions.ts index 25ded33b..e79929bf 100644 --- a/packages/prettier-plugin-java/src/printers/expressions.ts +++ b/packages/prettier-plugin-java/src/printers/expressions.ts @@ -54,11 +54,8 @@ 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 { isArgumentListSingleLambda } from "../utils/expressions-utils.js"; -import { - printSingleLambdaInvocation, - printArgumentListWithBraces -} from "../utils/index.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"; import { concat, dedent, group, indent } from "./prettier-builder.js"; @@ -76,7 +73,7 @@ import { sortTokens } from "./printer-utils.js"; -const { ifBreak, line, softline, indentIfBreak } = builders; +const { hardline, ifBreak, line, softline } = builders; export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter { expression(ctx: ExpressionCtx, params: any) { @@ -85,28 +82,14 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter { lambdaExpression( ctx: LambdaExpressionCtx, - params?: { - lambdaParametersGroupId: symbol; - isInsideMethodInvocationSuffix: boolean; - } + params?: { shouldBreak?: boolean } ) { - const lambdaParameters = group( - this.visit(ctx.lambdaParameters, params), - params ? { id: params.lambdaParametersGroupId } : undefined - ); + const lambdaParameters = group(this.visit(ctx.lambdaParameters, params)); const lambdaBody = this.visit(ctx.lambdaBody); const isLambdaBodyABlock = ctx.lambdaBody[0].children.block !== undefined; if (isLambdaBodyABlock) { - return rejectAndJoin(" ", [ - lambdaParameters, - ctx.Arrow[0], - params?.lambdaParametersGroupId !== undefined - ? indentIfBreak(lambdaBody, { - groupId: params.lambdaParametersGroupId - }) - : lambdaBody - ]); + return rejectAndJoin(" ", [lambdaParameters, ctx.Arrow[0], lambdaBody]); } return group( @@ -121,7 +104,7 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter { lambdaParameters( ctx: LambdaParametersCtx, - params?: { isInsideMethodInvocationSuffix: boolean } + params?: { shouldBreak?: boolean } ) { if (ctx.lambdaParametersWithBraces) { return this.visitSingle(ctx, params); @@ -132,21 +115,18 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter { lambdaParametersWithBraces( ctx: LambdaParametersWithBracesCtx, - params?: { isInsideMethodInvocationSuffix: boolean } + params?: { shouldBreak?: boolean } ) { - const lambdaParameterList = this.visit(ctx.lambdaParameterList); + const lambdaParameterList = this.visit(ctx.lambdaParameterList, params); if (findDeepElementInPartsArray(lambdaParameterList, ",")) { + const separator = params?.shouldBreak === false ? "" : softline; const content = putIntoBraces( lambdaParameterList, - softline, + separator, ctx.LBrace[0], ctx.RBrace[0] ); - if (params?.isInsideMethodInvocationSuffix === true) { - return indent(concat([softline, content])); - } - return content; } @@ -170,27 +150,29 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter { return lambdaParameterList; } - lambdaParameterList(ctx: LambdaParameterListCtx) { - return this.visitSingle(ctx); + lambdaParameterList( + ctx: LambdaParameterListCtx, + params?: { shouldBreak?: boolean } + ) { + return this.visitSingle(ctx, params); } - inferredLambdaParameterList(ctx: InferredLambdaParameterListCtx) { - const commas = ctx.Comma - ? ctx.Comma.map(elt => { - return concat([elt, line]); - }) - : []; - + inferredLambdaParameterList( + ctx: InferredLambdaParameterListCtx, + params?: { shouldBreak?: boolean } + ) { + const commaSuffix = params?.shouldBreak === false ? " " : line; + const commas = ctx.Comma?.map(comma => concat([comma, commaSuffix])); return rejectAndJoinSeps(commas, ctx.Identifier); } - explicitLambdaParameterList(ctx: ExplicitLambdaParameterListCtx) { + explicitLambdaParameterList( + ctx: ExplicitLambdaParameterListCtx, + params?: { shouldBreak?: boolean } + ) { const lambdaParameter = this.mapVisit(ctx.lambdaParameter); - const commas = ctx.Comma - ? ctx.Comma.map(elt => { - return concat([elt, line]); - }) - : []; + const commaSuffix = params?.shouldBreak === false ? " " : line; + const commas = ctx.Comma?.map(comma => concat([comma, commaSuffix])); return rejectAndJoinSeps(commas, lambdaParameter); } @@ -322,10 +304,30 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter { params?: { addParenthesisToWrapStatement?: boolean } ) { const countMethodInvocation = isUniqueMethodInvocation(ctx.primarySuffix); + const newExpression = + ctx.primaryPrefix[0].children.newExpression?.[0].children; + const isBreakableNewExpression = + countMethodInvocation <= 1 && + this.isBreakableNewExpression(newExpression); + const fqnOrRefType = + ctx.primaryPrefix[0].children.fqnOrRefType?.[0].children; + const firstMethodInvocation = ctx.primarySuffix + ?.map(suffix => suffix.children.methodInvocationSuffix?.[0].children) + .find(methodInvocationSuffix => methodInvocationSuffix); + const isCapitalizedIdentifier = this.isCapitalizedIdentifier(fqnOrRefType); + const shouldBreakBeforeFirstMethodInvocation = + countMethodInvocation > 1 && + !(isCapitalizedIdentifier ?? true) && + firstMethodInvocation !== undefined; + const shouldBreakBeforeMethodInvocations = + shouldBreakBeforeFirstMethodInvocation || + countMethodInvocation > 2 || + (countMethodInvocation > 1 && newExpression) || + !firstMethodInvocation?.argumentList; const primaryPrefix = this.visit(ctx.primaryPrefix, { ...params, - shouldBreakBeforeFirstMethodInvocation: countMethodInvocation > 1 + shouldBreakBeforeFirstMethodInvocation }); const suffixes = []; @@ -345,37 +347,17 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter { } if ( - ctx.primarySuffix[0].children.Dot !== undefined && - ctx.primaryPrefix[0].children.newExpression !== undefined + newExpression && + !isBreakableNewExpression && + ctx.primarySuffix[0].children.Dot !== undefined ) { suffixes.push(softline); } - suffixes.push( - this.visit(ctx.primarySuffix[0], { - shouldDedent: - // dedent when simple method invocation - countMethodInvocation !== 1 && - // dedent when (chain) method invocation - ctx.primaryPrefix[0] && - ctx.primaryPrefix[0].children.fqnOrRefType && - !( - ctx.primaryPrefix[0].children.fqnOrRefType[0].children.Dot !== - undefined - ) && - // indent when lambdaExpression - ctx.primarySuffix[0].children.methodInvocationSuffix && - ctx.primarySuffix[0].children.methodInvocationSuffix[0].children - .argumentList && - ctx.primarySuffix[0].children.methodInvocationSuffix[0].children - .argumentList[0].children.expression && - ctx.primarySuffix[0].children.methodInvocationSuffix[0].children - .argumentList[0].children.expression[0].children - .lambdaExpression === undefined - }) - ); + suffixes.push(this.visit(ctx.primarySuffix[0])); for (let i = 1; i < ctx.primarySuffix.length; i++) { if ( + shouldBreakBeforeMethodInvocations && ctx.primarySuffix[i].children.Dot !== undefined && ctx.primarySuffix[i - 1].children.methodInvocationSuffix !== undefined ) { @@ -384,10 +366,7 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter { suffixes.push(this.visit(ctx.primarySuffix[i])); } - if ( - countMethodInvocation === 1 && - ctx.primaryPrefix[0].children.newExpression === undefined - ) { + if (!newExpression && countMethodInvocation === 1) { return group( rejectAndConcat([ primaryPrefix, @@ -398,8 +377,23 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter { } } + const methodInvocation = + ctx.primarySuffix?.[0].children.methodInvocationSuffix; + const isMethodInvocationWithArguments = + methodInvocation?.[0].children.argumentList !== undefined; + const isUnqualifiedMethodInvocation = + methodInvocation !== undefined && !fqnOrRefType?.Dot; return group( - rejectAndConcat([primaryPrefix, indent(rejectAndConcat(suffixes))]) + rejectAndConcat([ + primaryPrefix, + isCapitalizedIdentifier || isUnqualifiedMethodInvocation + ? suffixes.shift() + : "", + !isBreakableNewExpression && + (shouldBreakBeforeMethodInvocations || !isMethodInvocationWithArguments) + ? indent(concat(suffixes)) + : concat(suffixes) + ]) ); } @@ -622,38 +616,37 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter { return concat([ctx.Less[0], ctx.Greater[0]]); } - methodInvocationSuffix(ctx: MethodInvocationSuffixCtx, params: any) { - const isSingleLambda = isArgumentListSingleLambda(ctx.argumentList); - if (isSingleLambda) { - return printSingleLambdaInvocation.call( - this, - ctx.argumentList, - ctx.RBrace[0], - ctx.LBrace[0] - ); - } - - const argumentList = this.visit(ctx.argumentList); - - if (params && params.shouldDedent) { - return dedent( - putIntoBraces(argumentList, softline, ctx.LBrace[0], ctx.RBrace[0]) - ); - } - - return putIntoBraces(argumentList, softline, ctx.LBrace[0], ctx.RBrace[0]); + methodInvocationSuffix(ctx: MethodInvocationSuffixCtx) { + return printArgumentListWithBraces.call( + this, + ctx.argumentList, + ctx.RBrace[0], + ctx.LBrace[0] + ); } - argumentList( - ctx: ArgumentListCtx, - params?: { - lambdaParametersGroupId: symbol; - isInsideMethodInvocationSuffix: boolean; - } - ) { + argumentList(ctx: ArgumentListCtx, params?: { shouldBreak?: boolean }) { + const shouldBreak = params?.shouldBreak; const expressions = this.mapVisit(ctx.expression, params); - const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, line])) : []; - return rejectAndJoinSeps(commas, expressions); + + 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 && isArgumentListHuggable(ctx)) { + 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 + }) + ]); + } + return rejectAndConcat([otherArguments, lastArgument]); } arrayCreationExpression(ctx: ArrayCreationExpressionCtx) { @@ -763,4 +756,34 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter { isRefTypeInMethodRef() { return "isRefTypeInMethodRef"; } + + private isBreakableNewExpression(newExpression?: NewExpressionCtx) { + const arrayCreationExpression = + newExpression?.arrayCreationExpression?.[0].children; + const classInstanceCreationExpression = + newExpression?.unqualifiedClassInstanceCreationExpression?.[0].children; + return [ + arrayCreationExpression?.classOrInterfaceType?.[0].children.classType[0] + .children.typeArguments, + arrayCreationExpression?.arrayCreationExplicitInitSuffix?.[0].children + .arrayInitializer[0].children.variableInitializerList, + classInstanceCreationExpression?.classOrInterfaceTypeToInstantiate[0] + .children.typeArgumentsOrDiamond?.[0].children.typeArguments, + classInstanceCreationExpression?.argumentList + ].some(breakablePart => breakablePart !== undefined); + } + + private isCapitalizedIdentifier(fqnOrRefType?: FqnOrRefTypeCtx) { + const fqnOrRefTypeParts = [ + fqnOrRefType?.fqnOrRefTypePartFirst[0], + ...(fqnOrRefType?.fqnOrRefTypePartRest ?? []) + ]; + const nextToLastIdentifier = + fqnOrRefTypeParts[fqnOrRefTypeParts.length - 2]?.children + .fqnOrRefTypePartCommon[0].children.Identifier?.[0].image; + return ( + nextToLastIdentifier && + /^\p{Uppercase_Letter}/u.test(nextToLastIdentifier) + ); + } } diff --git a/packages/prettier-plugin-java/src/printers/printer-utils.ts b/packages/prettier-plugin-java/src/printers/printer-utils.ts index 0cb30c1f..41d1ec7d 100644 --- a/packages/prettier-plugin-java/src/printers/printer-utils.ts +++ b/packages/prettier-plugin-java/src/printers/printer-utils.ts @@ -31,10 +31,7 @@ import { getTokenLeadingComments, printTokenWithComments } from "./comments/format-comments.js"; - import { concat, group, ifBreak, join } from "./prettier-builder.js"; -import Indent = builders.Indent; -import IfBreak = builders.IfBreak; const { indent, hardline, line } = builders; @@ -549,9 +546,7 @@ export function getInterfaceBodyDeclarationsSeparator( ); } -function getAndRemoveLeadingComment( - doc: IToken | builders.Indent | builders.IfBreak | string -) { +function getAndRemoveLeadingComment(doc: IToken | Doc) { const isTokenWithLeadingComment = typeof doc !== "string" && "leadingComments" in doc; if (!isTokenWithLeadingComment) { @@ -568,7 +563,7 @@ export function putIntoBraces( argument: Doc, separator: Doc, LBrace: IToken, - RBrace: IToken | Indent | IfBreak | string + RBrace: IToken | Doc ) { const rightBraceLeadingComments = getAndRemoveLeadingComment(RBrace); const lastBreakLine = diff --git a/packages/prettier-plugin-java/src/utils/expressions-utils.ts b/packages/prettier-plugin-java/src/utils/expressions-utils.ts index eb95d097..223e5deb 100644 --- a/packages/prettier-plugin-java/src/utils/expressions-utils.ts +++ b/packages/prettier-plugin-java/src/utils/expressions-utils.ts @@ -1,37 +1,12 @@ -import { ArgumentListCstNode } from "java-parser"; +import { ArgumentListCtx } from "java-parser"; -export function isArgumentListSingleLambda( - argumentList: ArgumentListCstNode[] | undefined -) { - if (argumentList === undefined) { - return false; - } - - const args = argumentList[0].children.expression; - if (args.length !== 1) { - return false; - } - - const argument = args[0]; - return argument.children.lambdaExpression !== undefined; -} - -export const isSingleArgumentLambdaExpressionWithBlock = ( - argumentList: ArgumentListCstNode[] | undefined -) => { - if (argumentList === undefined) { - return false; - } - - const args = argumentList[0].children.expression; - if (args.length !== 1) { - return false; - } - - const argument = args[0]; +export function isArgumentListHuggable(argumentList: ArgumentListCtx) { + const expressions = argumentList.expression; return ( - argument.children.lambdaExpression !== undefined && - argument.children.lambdaExpression[0].children.lambdaBody[0].children - .block !== undefined + 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/index.ts b/packages/prettier-plugin-java/src/utils/index.ts index ff2a3ebc..a8dfd53c 100644 --- a/packages/prettier-plugin-java/src/utils/index.ts +++ b/packages/prettier-plugin-java/src/utils/index.ts @@ -1,3 +1,2 @@ export { default as printArgumentListWithBraces } from "./printArgumentListWithBraces.js"; -export { default as printSingleLambdaInvocation } from "./printSingleLambdaInvocation.js"; export { default as isEmptyDoc } from "./isEmptyDoc.js"; diff --git a/packages/prettier-plugin-java/src/utils/printArgumentListWithBraces.ts b/packages/prettier-plugin-java/src/utils/printArgumentListWithBraces.ts index 1cafc2b8..8a2255b9 100644 --- a/packages/prettier-plugin-java/src/utils/printArgumentListWithBraces.ts +++ b/packages/prettier-plugin-java/src/utils/printArgumentListWithBraces.ts @@ -1,28 +1,28 @@ import { ArgumentListCstNode, IToken } from "java-parser"; -import { builders } from "prettier/doc"; -import { isArgumentListSingleLambda } from "./expressions-utils.js"; +import { builders, utils } from "prettier/doc"; +import { isArgumentListHuggable } from "./expressions-utils.js"; import { putIntoBraces } from "../printers/printer-utils.js"; -import printSingleLambdaInvocation from "./printSingleLambdaInvocation.js"; -const { softline } = builders; +const { breakParent, conditionalGroup, softline } = builders; +const { willBreak } = utils; export default function printArgumentListWithBraces( - argumentListCtx: ArgumentListCstNode[] | undefined, + argumentListNodes: ArgumentListCstNode[] | undefined, rBrace: IToken, lBrace: IToken ) { - const isSingleLambda = isArgumentListSingleLambda(argumentListCtx); - if (isSingleLambda) { - return printSingleLambdaInvocation.call( - this, - argumentListCtx, - rBrace, - lBrace - ); + const argumentListCtx = argumentListNodes?.[0].children; + if (argumentListCtx && isArgumentListHuggable(argumentListCtx)) { + const [flat, expanded] = [false, true].map(shouldBreak => { + const argumentList = this.visit(argumentListNodes, { shouldBreak }); + return putIntoBraces(argumentList, "", lBrace, rBrace); + }); + return [ + willBreak(flat) ? breakParent : "", + conditionalGroup([flat, expanded]) + ]; } - const argumentList = this.visit(argumentListCtx, { - isInsideMethodInvocationSuffix: true - }); + const argumentList = this.visit(argumentListNodes); return putIntoBraces(argumentList, softline, lBrace, rBrace); } diff --git a/packages/prettier-plugin-java/src/utils/printSingleLambdaInvocation.ts b/packages/prettier-plugin-java/src/utils/printSingleLambdaInvocation.ts deleted file mode 100644 index 614c1a7b..00000000 --- a/packages/prettier-plugin-java/src/utils/printSingleLambdaInvocation.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { ArgumentListCstNode, IToken } from "java-parser"; -import { builders } from "prettier/doc"; -import { isSingleArgumentLambdaExpressionWithBlock } from "./expressions-utils.js"; -import { printTokenWithComments } from "../printers/comments/format-comments.js"; -import { concat, dedent, indent } from "../printers/prettier-builder.js"; -import { putIntoBraces } from "../printers/printer-utils.js"; - -const { softline, ifBreak } = builders; - -export default function printSingleLambdaInvocation( - argumentListCtx: ArgumentListCstNode[] | undefined, - rBrace: IToken, - lBrace: IToken -) { - const lambdaParametersGroupId = Symbol("lambdaParameters"); - const argumentList = this.visit(argumentListCtx, { - lambdaParametersGroupId, - isInsideMethodInvocationSuffix: true - }); - - const formattedRBrace = isSingleArgumentLambdaExpressionWithBlock( - argumentListCtx - ) - ? ifBreak( - indent(concat([softline, rBrace])), - printTokenWithComments(rBrace), - { groupId: lambdaParametersGroupId } - ) - : indent(concat([softline, rBrace])); - return dedent(putIntoBraces(argumentList, "", lBrace, formattedRBrace)); -} diff --git a/packages/prettier-plugin-java/test/unit-test/arrays/_input.java b/packages/prettier-plugin-java/test/unit-test/arrays/_input.java index 7eb78660..816582bd 100644 --- a/packages/prettier-plugin-java/test/unit-test/arrays/_input.java +++ b/packages/prettier-plugin-java/test/unit-test/arrays/_input.java @@ -1,3 +1,7 @@ class Array { boolean[] skip = new boolean[candidates.length]; + + Class aaaaaaaaaaaaaaaa = new Aaaaaaaaaaaaaaaa[1].getClass(); + Class aaaaaaaaaaaaaaaa = new Aaaaaaaaaaaaaaaa[1111111111111111111].getClass(); + Class aaaaaaaaaaaaaaaa = new Aaaaaaaaaaaaaaaa[]{ new Aaaaaaaaaaaaaaaa() }.getClass(); } diff --git a/packages/prettier-plugin-java/test/unit-test/arrays/_output.java b/packages/prettier-plugin-java/test/unit-test/arrays/_output.java index bc3c0110..dd745b23 100644 --- a/packages/prettier-plugin-java/test/unit-test/arrays/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/arrays/_output.java @@ -1,4 +1,13 @@ class Array { boolean[] skip = new boolean[candidates.length]; + + Class aaaaaaaaaaaaaaaa = new Aaaaaaaaaaaaaaaa< + Bbbbbbbbbbbbbbbb + >[1].getClass(); + Class aaaaaaaaaaaaaaaa = new Aaaaaaaaaaaaaaaa[1111111111111111111] + .getClass(); + Class aaaaaaaaaaaaaaaa = new Aaaaaaaaaaaaaaaa[] { + new Aaaaaaaaaaaaaaaa(), + }.getClass(); } diff --git a/packages/prettier-plugin-java/test/unit-test/binary_expressions/_output.java b/packages/prettier-plugin-java/test/unit-test/binary_expressions/_output.java index 0c1bd482..52b3d265 100644 --- a/packages/prettier-plugin-java/test/unit-test/binary_expressions/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/binary_expressions/_output.java @@ -77,10 +77,12 @@ public boolean binaryOperationWithComments() { } public void method() { - new Foo(stuff, thing, "auaaaaaaaaa some very long stuff", "some more") - .bar(10); - foo(stuff, thing, "some very longuuuuuuuuuuuuuu stuff", "some more") - .bar(10); + new Foo(stuff, thing, "auaaaaaaaaa some very long stuff", "some more").bar( + 10 + ); + foo(stuff, thing, "some very longuuuuuuuuuuuuuu stuff", "some more").bar( + 10 + ); } public void binaryExpressionWithCast() { diff --git a/packages/prettier-plugin-java/test/unit-test/comments/class/_output.java b/packages/prettier-plugin-java/test/unit-test/comments/class/_output.java index f0837874..809b8f55 100644 --- a/packages/prettier-plugin-java/test/unit-test/comments/class/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/comments/class/_output.java @@ -866,8 +866,9 @@ public void getAllCountries() throws Exception { jsonPath("$.[*].display").value(hasItem(DEFAULT_DISPLAY.booleanValue())) ) .andExpect( - jsonPath("$.[*].internationalDialingCode") - .value(hasItem(DEFAULT_INTERNATIONAL_DIALING_CODE.toString())) + jsonPath("$.[*].internationalDialingCode").value( + hasItem(DEFAULT_INTERNATIONAL_DIALING_CODE.toString()) + ) ); } } diff --git a/packages/prettier-plugin-java/test/unit-test/expressions/_input.java b/packages/prettier-plugin-java/test/unit-test/expressions/_input.java index d6ea4147..32763f85 100644 --- a/packages/prettier-plugin-java/test/unit-test/expressions/_input.java +++ b/packages/prettier-plugin-java/test/unit-test/expressions/_input.java @@ -186,6 +186,24 @@ public void unannTypePrimitiveWithMethodReferenceSuffix(String[] args) { List.of(new double[][] { 1,2,3,4.1,5.6846465}, new double[][] { 1,2,3,4.1,5.6846465}, new double[][] { 1,2,3,4.1,5.6846465}).toArray(double[][]::new); } + public void staticMethodInvocationWithSingleChainedMethodInvocation() { + List.of(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).chained(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa); + } + + public void staticMethodInvocationWithMultipleChainedMethodInvocation() { + List.of(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).chained(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).another(); + } + + public void nonStaticMultipleChainedMethodInvocations() { + something.of( + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + ).chained( + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + ); + } + public void typeExpressionsInFqnParts() { var myVariable = ImmutableMap.of::a(); } diff --git a/packages/prettier-plugin-java/test/unit-test/expressions/_output.java b/packages/prettier-plugin-java/test/unit-test/expressions/_output.java index 8392adac..5e81ddd8 100644 --- a/packages/prettier-plugin-java/test/unit-test/expressions/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/expressions/_output.java @@ -194,21 +194,49 @@ public void printSynchronized() { } public void longFullyQualifiedName() { - com.me.very.very.very.very.very.very.very.very.very.very.very.very.very.longg.fully.qualified.name.FullyQualifiedName - .builder() + com.me.very.very.very.very.very.very.very.very.very.very.very.very.very.longg.fully.qualified.name.FullyQualifiedName.builder() .build(); com.FullyQualifiedName.builder(); } public void unannTypePrimitiveWithMethodReferenceSuffix(String[] args) { - List - .of( - new double[][] { 1, 2, 3, 4.1, 5.6846465 }, - new double[][] { 1, 2, 3, 4.1, 5.6846465 }, - new double[][] { 1, 2, 3, 4.1, 5.6846465 } + List.of( + new double[][] { 1, 2, 3, 4.1, 5.6846465 }, + new double[][] { 1, 2, 3, 4.1, 5.6846465 }, + new double[][] { 1, 2, 3, 4.1, 5.6846465 } + ).toArray(double[][]::new); + } + + public void staticMethodInvocationWithSingleChainedMethodInvocation() { + List.of( + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + ).chained( + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + ); + } + + public void staticMethodInvocationWithMultipleChainedMethodInvocation() { + List.of( + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + ) + .chained( + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ) - .toArray(double[][]::new); + .another(); + } + + public void nonStaticMultipleChainedMethodInvocations() { + something + .of(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) + .chained( + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + ); } public void typeExpressionsInFqnParts() { diff --git a/packages/prettier-plugin-java/test/unit-test/indent/_output.java b/packages/prettier-plugin-java/test/unit-test/indent/_output.java index ee396745..b8eb58cc 100644 --- a/packages/prettier-plugin-java/test/unit-test/indent/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/indent/_output.java @@ -11,8 +11,7 @@ void indetMethod() { useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa, useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa, useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa - ) - .isEqualTo(); + ).isEqualTo(); assertThat( useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa, 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 1de5d55a..ef0f8104 100644 --- a/packages/prettier-plugin-java/test/unit-test/lambda/_input.java +++ b/packages/prettier-plugin-java/test/unit-test/lambda/_input.java @@ -151,6 +151,63 @@ public void testConstructor() { } ); } + + private static Function, T> createInstance( + Group entity + ) { + return ctor -> + Try.of(a, () -> { + @SuppressWarnings("unchecked") + var ng = (T) ctor.newInstance( + entity.getId(), + entity.getSystemGenerated(), + entity.getVersionKey() + ); + return ng; + }).getOrElseThrow(ex -> new RuntimeException(ex)); + } + + void singleLambdaWithBlockLastArgument() { + a.of(b, c, d, e -> { + return f; + }); + } + + void singleLambdaWithBlockLastArgumentAndLongArguments() { + a.of( + aaaaaaaaaaaaaaaaaaaaaaaaaa, + bbbbbbbbbbbbbbbbbbbbbbbbbb, + cccccccccccccccccccccccccc, + dddddddddddddddddddddddddd, + e -> { + return f; + } + ); + } + + void singleLambdaWithBlockLastArgumentAndLongLambdaArgument() { + a.of(b, c, d, eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee -> { + return f; + }); + } + + void singleLambdaWithBlockLastArgumentAndLongLambdaArguments() { + a.of(b, (cccccccccccccccccccccccccc, dddddddddddddddddddddddddd, eeeeeeeeeeeeeeeeeeeeeeeeee) -> { + return f; + }); + } + + void multipleLambdaArguments() { + a.of(b, c -> d, e -> { + return f; + }); + } + + void argumentAfterLambdaWithBlock() { + a.of(b, c, d, e -> { + return f; + }, g); + } } class T { @@ -190,6 +247,12 @@ class T { return n * 2; }); } + + T() { + super(a, () -> { + return b; + }); + } } enum Enum { 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 aab01499..2cc00ae9 100644 --- a/packages/prettier-plugin-java/test/unit-test/lambda/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/lambda/_output.java @@ -46,15 +46,15 @@ public void onlyOneMethodInBody() { } public void lambdaWithoutBracesWhichBreak() { - call(x -> - foo.isVeryVeryVeryLongConditionTrue() && - foo.isAnotherVeryVeryLongConditionTrue() + call( + x -> + foo.isVeryVeryVeryLongConditionTrue() && + foo.isAnotherVeryVeryLongConditionTrue() ); } public void chainCallWithLambda() { - Stream - .of(1, 2) + Stream.of(1, 2) .map(n -> { // testing method return n * 2; @@ -63,8 +63,7 @@ public void chainCallWithLambda() { } public void lambdaWithLongListOfParameters() { - final List values = Stream - .of(1, 2) + final List values = Stream.of(1, 2) .map( ( aVeryLongListOfParameter, @@ -80,8 +79,7 @@ public void lambdaWithLongListOfParameters() { ) .collect(Collectors.toList()); - final List values = Stream - .of(1, 2) + final List values = Stream.of(1, 2) .map( (aVeryLongListOfParameter, aVeryLongListOfParameter, aParameterTha) -> { // testing method @@ -143,6 +141,85 @@ public void testConstructor() { } ); } + + private static Function, T> createInstance( + Group entity + ) { + return ctor -> + Try.of(a, () -> { + @SuppressWarnings("unchecked") + var ng = (T) ctor.newInstance( + entity.getId(), + entity.getSystemGenerated(), + entity.getVersionKey() + ); + return ng; + }).getOrElseThrow(ex -> new RuntimeException(ex)); + } + + void singleLambdaWithBlockLastArgument() { + a.of(b, c, d, e -> { + return f; + }); + } + + void singleLambdaWithBlockLastArgumentAndLongArguments() { + a.of( + aaaaaaaaaaaaaaaaaaaaaaaaaa, + bbbbbbbbbbbbbbbbbbbbbbbbbb, + cccccccccccccccccccccccccc, + dddddddddddddddddddddddddd, + e -> { + return f; + } + ); + } + + void singleLambdaWithBlockLastArgumentAndLongLambdaArgument() { + a.of( + b, + c, + d, + eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee -> { + return f; + } + ); + } + + void singleLambdaWithBlockLastArgumentAndLongLambdaArguments() { + a.of( + b, + ( + cccccccccccccccccccccccccc, + dddddddddddddddddddddddddd, + eeeeeeeeeeeeeeeeeeeeeeeeee + ) -> { + return f; + } + ); + } + + void multipleLambdaArguments() { + a.of( + b, + c -> d, + e -> { + return f; + } + ); + } + + void argumentAfterLambdaWithBlock() { + a.of( + b, + c, + d, + e -> { + return f; + }, + g + ); + } } class T { @@ -185,6 +262,12 @@ class T { } ); } + + T() { + super(a, () -> { + return b; + }); + } } enum Enum { diff --git a/packages/prettier-plugin-java/test/unit-test/member_chain/_input.java b/packages/prettier-plugin-java/test/unit-test/member_chain/_input.java index b0a58b43..bf5219c3 100644 --- a/packages/prettier-plugin-java/test/unit-test/member_chain/_input.java +++ b/packages/prettier-plugin-java/test/unit-test/member_chain/_input.java @@ -20,4 +20,12 @@ public void doSomethingLongStatic() { return Object.something().more().and().that().as().well().but().not().something(); } + public void singleInvocationOnNewExpression() { + new Instance(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).invocation(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa); + } + + public void multipleInvocationsOnNewExpression() { + new Instance(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).invocation(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).andAnother(); + } + } diff --git a/packages/prettier-plugin-java/test/unit-test/member_chain/_output.java b/packages/prettier-plugin-java/test/unit-test/member_chain/_output.java index 7d5c7a78..9ebcdc64 100644 --- a/packages/prettier-plugin-java/test/unit-test/member_chain/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/member_chain/_output.java @@ -43,8 +43,7 @@ public void doSomethingLongNew2() { } public void doSomethingLongStatic() { - return Object - .something() + return Object.something() .more() .and() .that() @@ -54,4 +53,26 @@ public void doSomethingLongStatic() { .not() .something(); } + + public void singleInvocationOnNewExpression() { + new Instance( + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + ).invocation( + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + ); + } + + public void multipleInvocationsOnNewExpression() { + new Instance( + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + ) + .invocation( + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + ) + .andAnother(); + } } diff --git a/packages/prettier-plugin-java/test/unit-test/variables/_output.java b/packages/prettier-plugin-java/test/unit-test/variables/_output.java index 4f9459a3..033c473c 100644 --- a/packages/prettier-plugin-java/test/unit-test/variables/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/variables/_output.java @@ -313,8 +313,7 @@ > void breakOnTypeArguments( ExtremelyLongAndObnoxiousClassName >, ExtremelyLongAndObnoxiousClassName - >() - .method(); + >().method(); } public methodWithVariableInitializationWithComments() { @@ -328,8 +327,7 @@ public methodWithVariableInitializationWithComments() { void assignment() { fileSystemDetails = - FileHandlerDetails - .builder() + FileHandlerDetails.builder() .fileSystemType( EntityUtils.update( entity.getFileSystemDetails().getFileSystemType(),