Skip to content

Commit de2a03b

Browse files
feat: improve lambda formatting in constructors and enums
1 parent 9c52aef commit de2a03b

File tree

5 files changed

+299
-30
lines changed

5 files changed

+299
-30
lines changed

packages/prettier-plugin-java/src/printers/classes.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ import {
9191
} from "java-parser";
9292
import { Doc } from "prettier";
9393
import { isAnnotationCstNode, isTypeArgumentsCstNode } from "../types/utils";
94+
import { printArgumentListWithBraces } from "../utils/printArgumentListWithBraces";
9495

9596
const { line, softline, hardline } = builders;
9697

@@ -734,16 +735,16 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
734735
) {
735736
const typeArguments = this.visit(ctx.typeArguments);
736737
const keyWord = ctx.This ? ctx.This[0] : ctx.Super![0];
737-
const argumentList = this.visit(ctx.argumentList);
738+
const argumentList = printArgumentListWithBraces.call(
739+
this,
740+
ctx.argumentList,
741+
ctx.RBrace![0],
742+
ctx.LBrace[0]
743+
);
738744
return rejectAndConcat([
739745
typeArguments,
740746
keyWord,
741-
group(
742-
rejectAndConcat([
743-
putIntoBraces(argumentList, softline, ctx.LBrace[0], ctx.RBrace[0]),
744-
ctx.Semicolon[0]
745-
])
746-
)
747+
group(rejectAndConcat([argumentList, ctx.Semicolon[0]]))
747748
]);
748749
}
749750

@@ -752,19 +753,19 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
752753
) {
753754
const expressionName = this.visit(ctx.expressionName);
754755
const typeArguments = this.visit(ctx.typeArguments);
755-
const argumentList = this.visit(ctx.argumentList);
756+
const argumentList = printArgumentListWithBraces.call(
757+
this,
758+
ctx.argumentList,
759+
ctx.RBrace![0],
760+
ctx.LBrace[0]
761+
);
756762

757763
return rejectAndConcat([
758764
expressionName,
759765
ctx.Dot[0],
760766
typeArguments,
761767
ctx.Super[0],
762-
group(
763-
rejectAndConcat([
764-
putIntoBraces(argumentList, softline, ctx.LBrace[0], ctx.RBrace[0]),
765-
ctx.Semicolon[0]
766-
])
767-
)
768+
group(rejectAndConcat([argumentList, ctx.Semicolon[0]]))
768769
]);
769770
}
770771

@@ -842,18 +843,22 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
842843
const otherModifiers = this.mapVisit(modifiers[1]);
843844

844845
const identifier = ctx.Identifier[0];
845-
const argumentList = this.visit(ctx.argumentList);
846846
const classBody = this.visit(ctx.classBody);
847847

848-
const optionnalBracesAndArgumentList = ctx.LBrace
849-
? putIntoBraces(argumentList, softline, ctx.LBrace[0], ctx.RBrace![0])
848+
const optionalBracesAndArgumentList = ctx.LBrace
849+
? printArgumentListWithBraces.call(
850+
this,
851+
ctx.argumentList,
852+
ctx.RBrace![0],
853+
ctx.LBrace[0]
854+
)
850855
: "";
851856

852857
return rejectAndJoin(hardline, [
853858
rejectAndJoin(hardline, firstAnnotations),
854859
rejectAndJoin(" ", [
855860
rejectAndJoin(" ", otherModifiers),
856-
rejectAndConcat([identifier, optionnalBracesAndArgumentList]),
861+
rejectAndConcat([identifier, optionalBracesAndArgumentList]),
857862
classBody
858863
])
859864
]);

packages/prettier-plugin-java/src/printers/expressions.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"use strict";
22

3-
import { BaseCstPrettierPrinter } from "../base-cst-printer";
43
import {
54
ArgumentListCtx,
65
ArrayAccessSuffixCtx,
@@ -50,9 +49,18 @@ import {
5049
} from "java-parser/api";
5150

5251
import forEach from "lodash/forEach";
53-
import { concat, dedent, group, indent } from "./prettier-builder";
52+
import { Doc } from "prettier";
53+
import { builders } from "prettier/doc";
54+
import { BaseCstPrettierPrinter } from "../base-cst-printer";
55+
import { isAnnotationCstNode } from "../types/utils";
56+
import {
57+
isArgumentListSingleLambda,
58+
isSingleArgumentLambdaExpressionWithBlock
59+
} from "../utils/expressions-utils";
60+
import { printArgumentListWithBraces } from "../utils/printArgumentListWithBraces";
5461
import { printTokenWithComments } from "./comments/format-comments";
5562
import { handleCommentsBinaryExpression } from "./comments/handle-comments";
63+
import { concat, dedent, group, indent } from "./prettier-builder";
5664
import {
5765
findDeepElementInPartsArray,
5866
isExplicitLambdaParameter,
@@ -67,13 +75,6 @@ import {
6775
sortAnnotationIdentifier,
6876
sortNodes
6977
} from "./printer-utils";
70-
import { builders } from "prettier/doc";
71-
import { Doc } from "prettier";
72-
import { isAnnotationCstNode } from "../types/utils";
73-
import {
74-
isArgumentListSingleLambda,
75-
isSingleArgumentLambdaExpressionWithBlock
76-
} from "../utils/expressions-utils";
7778

7879
const { ifBreak, line, softline, indentIfBreak } = builders;
7980

@@ -629,15 +630,22 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
629630
const classOrInterfaceTypeToInstantiate = this.visit(
630631
ctx.classOrInterfaceTypeToInstantiate
631632
);
632-
const argumentList = this.visit(ctx.argumentList);
633+
634+
let content = printArgumentListWithBraces.call(
635+
this,
636+
ctx.argumentList,
637+
ctx.RBrace[0],
638+
ctx.LBrace[0]
639+
);
640+
633641
const classBody = this.visit(ctx.classBody);
634642

635643
return rejectAndJoin(" ", [
636644
ctx.New[0],
637645
rejectAndConcat([
638646
typeArguments,
639647
classOrInterfaceTypeToInstantiate,
640-
putIntoBraces(argumentList, softline, ctx.LBrace[0], ctx.RBrace[0])
648+
content
641649
]),
642650
classBody
643651
]);
@@ -706,7 +714,10 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
706714

707715
argumentList(
708716
ctx: ArgumentListCtx,
709-
params: { lambdaParametersGroupId: symbol }
717+
params?: {
718+
lambdaParametersGroupId: symbol;
719+
isInsideMethodInvocationSuffix: boolean;
720+
}
710721
) {
711722
const expressions = this.mapVisit(ctx.expression, params);
712723
const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, line])) : [];
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { ArgumentListCstNode, IToken } from "java-parser";
2+
import { Doc } from "prettier";
3+
import { builders } from "prettier/doc";
4+
import {
5+
isArgumentListSingleLambda,
6+
isSingleArgumentLambdaExpressionWithBlock
7+
} from "./expressions-utils";
8+
import { printTokenWithComments } from "../printers/comments/format-comments";
9+
import { concat, dedent, indent } from "../printers/prettier-builder";
10+
import { putIntoBraces } from "../printers/printer-utils";
11+
12+
const { softline, ifBreak } = builders;
13+
14+
export function printArgumentListWithBraces(
15+
argumentListCtx: ArgumentListCstNode[] | undefined,
16+
rBrace: IToken,
17+
lBrace: IToken
18+
) {
19+
const lambdaParametersGroupId = Symbol("lambdaParameters");
20+
const argumentList = this.visit(argumentListCtx, {
21+
lambdaParametersGroupId,
22+
isInsideMethodInvocationSuffix: true
23+
});
24+
const isSingleLambda = isArgumentListSingleLambda(argumentListCtx);
25+
let sep = isSingleLambda ? "" : softline;
26+
let content: Doc;
27+
if (isSingleLambda) {
28+
const formattedRBrace = isSingleArgumentLambdaExpressionWithBlock(
29+
argumentListCtx
30+
)
31+
? ifBreak(
32+
indent(concat([softline, rBrace])),
33+
printTokenWithComments(rBrace),
34+
{ groupId: lambdaParametersGroupId }
35+
)
36+
: indent(concat([softline, rBrace]));
37+
return dedent(putIntoBraces(argumentList, sep, lBrace, formattedRBrace));
38+
} else {
39+
return putIntoBraces(argumentList, softline, lBrace, rBrace);
40+
}
41+
}

packages/prettier-plugin-java/test/unit-test/lambda/_input.java

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,111 @@ public void callWithLambdaAndExtraParameter() {
115115
executor
116116
);
117117
}
118+
119+
public void testConstructor() {
120+
new Value(
121+
(
122+
x
123+
124+
) -> {
125+
// testing method
126+
return n * 2;
127+
}
128+
);
129+
130+
new Value(
131+
(
132+
aVeryLongListOfParameter,
133+
aVeryLongListOfParameter
134+
) -> {
135+
// testing method
136+
return n * 2;
137+
}
138+
);
139+
140+
new Value(
141+
(
142+
aVeryLongListOfParameter,
143+
aVeryLongListOfParameter,
144+
aVeryLongListOfParameter,
145+
aVeryLongListOfParameter,
146+
aVeryLongListOfParameter,
147+
aVeryLongListOfParameter
148+
) -> {
149+
// testing method
150+
return n * 2;
151+
}
152+
);
153+
}
154+
}
155+
156+
class T {
157+
T() {
158+
super(x -> {
159+
// testing method
160+
return n * 2;
161+
});
162+
}
163+
164+
T() {
165+
super((x,y) -> {
166+
// testing method
167+
return n * 2;
168+
});
169+
}
170+
171+
T() {
172+
super((aVeryLongListOfParameter,
173+
aVeryLongListOfParameter,
174+
aVeryLongListOfParameter,
175+
aVeryLongListOfParameter,
176+
aVeryLongListOfParameter,
177+
aVeryLongListOfParameter) -> {
178+
// testing method
179+
return n * 2;
180+
});
181+
}
182+
183+
T() {
184+
super((
185+
aVeryLongListOfParameter,
186+
aVeryLongListOfParameter,
187+
aParameterThatS
188+
) -> {
189+
// testing method
190+
return n * 2;
191+
});
192+
}
193+
}
194+
195+
enum Enum {
196+
VALUE(x -> {
197+
// testing method
198+
return n * 2;
199+
}),
200+
VALUE((x,y) -> {
201+
// testing method
202+
return n * 2;
203+
}),
204+
VALUE((aVeryLongListOfParameter,
205+
aVeryLongListOfParameter,
206+
aVeryLongListOfParameter,
207+
aVeryLongListOfParameter,
208+
aVeryLongListOfParameter,
209+
aVeryLongListOfParameter) -> {
210+
// testing method
211+
return n * 2;
212+
}),
213+
VALUE((
214+
aVeryLongListOfParameter,
215+
aVeryLongListOfParameter,
216+
aParameterThatS
217+
) -> {
218+
// testing method
219+
return n * 2;
220+
}),
221+
VALUE(x -> {
222+
// testing method
223+
return n * 2;
224+
}, other)
118225
}

0 commit comments

Comments
 (0)