1
1
"use strict" ;
2
2
3
- import { BaseCstPrettierPrinter } from "../base-cst-printer" ;
4
3
import {
5
4
ArgumentListCtx ,
6
5
ArrayAccessSuffixCtx ,
@@ -50,40 +49,64 @@ import {
50
49
} from "java-parser/api" ;
51
50
52
51
import forEach from "lodash/forEach" ;
53
- import { concat , group , indent , dedent } 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 { isArgumentListSingleLambda } from "../utils/expressions-utils" ;
57
+ import {
58
+ printSingleLambdaInvocation ,
59
+ printArgumentListWithBraces
60
+ } from "../utils" ;
54
61
import { printTokenWithComments } from "./comments/format-comments" ;
55
62
import { handleCommentsBinaryExpression } from "./comments/handle-comments" ;
63
+ import { concat , dedent , group , indent } from "./prettier-builder" ;
56
64
import {
57
- matchCategory ,
58
- rejectAndJoin ,
59
- rejectAndConcat ,
60
- sortAnnotationIdentifier ,
61
- sortNodes ,
62
- rejectAndJoinSeps ,
63
65
findDeepElementInPartsArray ,
64
66
isExplicitLambdaParameter ,
67
+ isShiftOperator ,
68
+ isUniqueMethodInvocation ,
69
+ matchCategory ,
65
70
putIntoBraces ,
71
+ rejectAndConcat ,
72
+ rejectAndJoin ,
73
+ rejectAndJoinSeps ,
66
74
separateTokensIntoGroups ,
67
- isShiftOperator ,
68
- isUniqueMethodInvocation
75
+ sortAnnotationIdentifier ,
76
+ sortNodes
69
77
} from "./printer-utils" ;
70
- import { builders } from "prettier/doc" ;
71
- import { Doc } from "prettier" ;
72
- import { isAnnotationCstNode , isCstNode } from "../types/utils" ;
73
- const { ifBreak, line, softline } = builders ;
78
+
79
+ const { ifBreak, line, softline, indentIfBreak } = builders ;
74
80
75
81
export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
76
82
expression ( ctx : ExpressionCtx , params : any ) {
77
83
return this . visitSingle ( ctx , params ) ;
78
84
}
79
85
80
- lambdaExpression ( ctx : LambdaExpressionCtx ) {
81
- const lambdaParameters = this . visit ( ctx . lambdaParameters ) ;
86
+ lambdaExpression (
87
+ ctx : LambdaExpressionCtx ,
88
+ params ?: {
89
+ lambdaParametersGroupId : symbol ;
90
+ isInsideMethodInvocationSuffix : boolean ;
91
+ }
92
+ ) {
93
+ const lambdaParameters = group (
94
+ this . visit ( ctx . lambdaParameters , params ) ,
95
+ params ? { id : params . lambdaParametersGroupId } : undefined
96
+ ) ;
82
97
const lambdaBody = this . visit ( ctx . lambdaBody ) ;
83
98
84
99
const isLambdaBodyABlock = ctx . lambdaBody [ 0 ] . children . block !== undefined ;
85
100
if ( isLambdaBodyABlock ) {
86
- return rejectAndJoin ( " " , [ lambdaParameters , ctx . Arrow [ 0 ] , lambdaBody ] ) ;
101
+ return rejectAndJoin ( " " , [
102
+ lambdaParameters ,
103
+ ctx . Arrow [ 0 ] ,
104
+ params ?. lambdaParametersGroupId !== undefined
105
+ ? indentIfBreak ( lambdaBody , {
106
+ groupId : params . lambdaParametersGroupId
107
+ } )
108
+ : lambdaBody
109
+ ] ) ;
87
110
}
88
111
89
112
return group (
@@ -96,23 +119,35 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
96
119
) ;
97
120
}
98
121
99
- lambdaParameters ( ctx : LambdaParametersCtx ) {
122
+ lambdaParameters (
123
+ ctx : LambdaParametersCtx ,
124
+ params ?: { isInsideMethodInvocationSuffix : boolean }
125
+ ) {
100
126
if ( ctx . lambdaParametersWithBraces ) {
101
- return this . visitSingle ( ctx ) ;
127
+ return this . visitSingle ( ctx , params ) ;
102
128
}
103
129
104
130
return printTokenWithComments ( this . getSingle ( ctx ) as IToken ) ;
105
131
}
106
132
107
- lambdaParametersWithBraces ( ctx : LambdaParametersWithBracesCtx ) {
133
+ lambdaParametersWithBraces (
134
+ ctx : LambdaParametersWithBracesCtx ,
135
+ params ?: { isInsideMethodInvocationSuffix : boolean }
136
+ ) {
108
137
const lambdaParameterList = this . visit ( ctx . lambdaParameterList ) ;
109
138
110
139
if ( findDeepElementInPartsArray ( lambdaParameterList , "," ) ) {
111
- return rejectAndConcat ( [
112
- ctx . LBrace [ 0 ] ,
140
+ const content = putIntoBraces (
113
141
lambdaParameterList ,
142
+ softline ,
143
+ ctx . LBrace [ 0 ] ,
114
144
ctx . RBrace [ 0 ]
115
- ] ) ;
145
+ ) ;
146
+ if ( params ?. isInsideMethodInvocationSuffix === true ) {
147
+ return indent ( concat ( [ softline , content ] ) ) ;
148
+ }
149
+
150
+ return content ;
116
151
}
117
152
118
153
// removing braces when only no comments attached
@@ -142,7 +177,7 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
142
177
inferredLambdaParameterList ( ctx : InferredLambdaParameterListCtx ) {
143
178
const commas = ctx . Comma
144
179
? ctx . Comma . map ( elt => {
145
- return concat ( [ elt , " " ] ) ;
180
+ return concat ( [ elt , line ] ) ;
146
181
} )
147
182
: [ ] ;
148
183
@@ -153,7 +188,7 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
153
188
const lambdaParameter = this . mapVisit ( ctx . lambdaParameter ) ;
154
189
const commas = ctx . Comma
155
190
? ctx . Comma . map ( elt => {
156
- return concat ( [ elt , " " ] ) ;
191
+ return concat ( [ elt , line ] ) ;
157
192
} )
158
193
: [ ] ;
159
194
return rejectAndJoinSeps ( commas , lambdaParameter ) ;
@@ -595,15 +630,22 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
595
630
const classOrInterfaceTypeToInstantiate = this . visit (
596
631
ctx . classOrInterfaceTypeToInstantiate
597
632
) ;
598
- 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
+
599
641
const classBody = this . visit ( ctx . classBody ) ;
600
642
601
643
return rejectAndJoin ( " " , [
602
644
ctx . New [ 0 ] ,
603
645
rejectAndConcat ( [
604
646
typeArguments ,
605
647
classOrInterfaceTypeToInstantiate ,
606
- putIntoBraces ( argumentList , softline , ctx . LBrace [ 0 ] , ctx . RBrace [ 0 ] )
648
+ content
607
649
] ) ,
608
650
classBody
609
651
] ) ;
@@ -642,17 +684,35 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
642
684
}
643
685
644
686
methodInvocationSuffix ( ctx : MethodInvocationSuffixCtx , params : any ) {
687
+ const isSingleLambda = isArgumentListSingleLambda ( ctx . argumentList ) ;
688
+ if ( isSingleLambda ) {
689
+ return printSingleLambdaInvocation . call (
690
+ this ,
691
+ ctx . argumentList ,
692
+ ctx . RBrace [ 0 ] ,
693
+ ctx . LBrace [ 0 ]
694
+ ) ;
695
+ }
696
+
645
697
const argumentList = this . visit ( ctx . argumentList ) ;
698
+
646
699
if ( params && params . shouldDedent ) {
647
700
return dedent (
648
701
putIntoBraces ( argumentList , softline , ctx . LBrace [ 0 ] , ctx . RBrace [ 0 ] )
649
702
) ;
650
703
}
704
+
651
705
return putIntoBraces ( argumentList , softline , ctx . LBrace [ 0 ] , ctx . RBrace [ 0 ] ) ;
652
706
}
653
707
654
- argumentList ( ctx : ArgumentListCtx ) {
655
- const expressions = this . mapVisit ( ctx . expression ) ;
708
+ argumentList (
709
+ ctx : ArgumentListCtx ,
710
+ params ?: {
711
+ lambdaParametersGroupId : symbol ;
712
+ isInsideMethodInvocationSuffix : boolean ;
713
+ }
714
+ ) {
715
+ const expressions = this . mapVisit ( ctx . expression , params ) ;
656
716
const commas = ctx . Comma ? ctx . Comma . map ( elt => concat ( [ elt , line ] ) ) : [ ] ;
657
717
return rejectAndJoinSeps ( commas , expressions ) ;
658
718
}
0 commit comments