-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: avoid breaking on certain method chains and arguments
- Loading branch information
Showing
16 changed files
with
310 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 9 additions & 34 deletions
43
packages/prettier-plugin-java/src/utils/expressions-utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
); | ||
}; | ||
} |
12 changes: 6 additions & 6 deletions
12
packages/prettier-plugin-java/src/utils/printArgumentListWithBraces.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 12 additions & 12 deletions
24
packages/prettier-plugin-java/src/utils/printSingleLambdaInvocation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,31 @@ | ||
import { ArgumentListCstNode, IToken } from "java-parser"; | ||
import { builders } from "prettier/doc"; | ||
import { isSingleArgumentLambdaExpressionWithBlock } from "./expressions-utils"; | ||
import { printTokenWithComments } from "../printers/comments/format-comments"; | ||
import { concat, dedent, indent } from "../printers/prettier-builder"; | ||
import { putIntoBraces } from "../printers/printer-utils"; | ||
|
||
const { softline, ifBreak } = builders; | ||
|
||
export default function printSingleLambdaInvocation( | ||
argumentListCtx: ArgumentListCstNode[] | undefined, | ||
argumentListNodes: ArgumentListCstNode[], | ||
rBrace: IToken, | ||
lBrace: IToken | ||
) { | ||
const lambdaParametersGroupId = Symbol("lambdaParameters"); | ||
const argumentList = this.visit(argumentListCtx, { | ||
const argumentList = this.visit(argumentListNodes, { | ||
lambdaParametersGroupId, | ||
isInsideMethodInvocationSuffix: true | ||
}); | ||
|
||
const formattedRBrace = isSingleArgumentLambdaExpressionWithBlock( | ||
argumentListCtx | ||
) | ||
? ifBreak( | ||
indent(concat([softline, rBrace])), | ||
printTokenWithComments(rBrace), | ||
{ groupId: lambdaParametersGroupId } | ||
) | ||
: indent(concat([softline, rBrace])); | ||
const expressions = argumentListNodes[0].children.expression; | ||
const isLastArgumentLambdaWithBlock = | ||
expressions[expressions.length - 1].children.lambdaExpression?.[0].children | ||
.lambdaBody[0].children.block !== undefined; | ||
const breakableRBrace = indent(concat([softline, rBrace])); | ||
const formattedRBrace = isLastArgumentLambdaWithBlock | ||
? ifBreak(breakableRBrace, printTokenWithComments(rBrace), { | ||
groupId: lambdaParametersGroupId | ||
}) | ||
: breakableRBrace; | ||
return dedent(putIntoBraces(argumentList, "", lBrace, formattedRBrace)); | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/prettier-plugin-java/test/unit-test/arrays/_input.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
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(); | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/prettier-plugin-java/test/unit-test/arrays/_output.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.