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