@@ -41,7 +41,6 @@ import {
41
41
ReferenceTypeCastExpressionCtx ,
42
42
RegularLambdaParameterCtx ,
43
43
TernaryExpressionCtx ,
44
- TypeArgumentListCtx ,
45
44
TypeArgumentsOrDiamondCtx ,
46
45
TypePatternCtx ,
47
46
UnaryExpressionCtx ,
@@ -50,7 +49,6 @@ import {
50
49
} from "java-parser/api" ;
51
50
52
51
import forEach from "lodash/forEach" ;
53
- import { Doc } from "prettier" ;
54
52
import { builders } from "prettier/doc" ;
55
53
import { BaseCstPrettierPrinter } from "../base-cst-printer" ;
56
54
import { isAnnotationCstNode } from "../types/utils" ;
@@ -63,20 +61,18 @@ import { printTokenWithComments } from "./comments/format-comments";
63
61
import { handleCommentsBinaryExpression } from "./comments/handle-comments" ;
64
62
import { concat , dedent , group , indent } from "./prettier-builder" ;
65
63
import {
64
+ binary ,
66
65
findDeepElementInPartsArray ,
67
66
isExplicitLambdaParameter ,
68
- isShiftOperator ,
69
67
isUniqueMethodInvocation ,
70
- matchCategory ,
71
68
putIntoBraces ,
72
69
rejectAndConcat ,
73
70
rejectAndJoin ,
74
71
rejectAndJoinSeps ,
75
- separateTokensIntoGroups ,
76
72
sortAnnotationIdentifier ,
77
- sortNodes
73
+ sortNodes ,
74
+ sortTokens
78
75
} from "./printer-utils" ;
79
- import join = builders . join ;
80
76
81
77
const { ifBreak, line, softline, indentIfBreak } = builders ;
82
78
@@ -244,104 +240,54 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
244
240
return binaryExpression ;
245
241
}
246
242
247
- binaryExpression ( ctx : BinaryExpressionCtx , params : any ) {
243
+ binaryExpression (
244
+ ctx : BinaryExpressionCtx ,
245
+ params ?: { addParenthesisToWrapStatement ?: boolean }
246
+ ) {
248
247
handleCommentsBinaryExpression ( ctx ) ;
249
248
250
- const instanceofReferences = this . mapVisit (
251
- sortNodes ( [ ctx . pattern , ctx . referenceType ] )
252
- ) ;
253
- const expression = this . mapVisit ( ctx . expression ) ;
254
- const unaryExpression = this . mapVisit ( ctx . unaryExpression ) ;
255
-
256
- const { groupsOfOperator, sortedBinaryOperators } =
257
- separateTokensIntoGroups ( ctx ) ;
258
- const segmentsSplitByBinaryOperator : any [ ] = [ ] ;
259
- let currentSegment = [ ] ;
249
+ const sortedNodes = sortNodes ( [
250
+ ctx . pattern ,
251
+ ctx . referenceType ,
252
+ ctx . expression ,
253
+ ctx . unaryExpression
254
+ ] ) ;
260
255
261
- if ( groupsOfOperator . length === 1 && groupsOfOperator [ 0 ] . length === 0 ) {
262
- return unaryExpression . shift ( ) ;
263
- }
256
+ const nodes = this . mapVisit (
257
+ sortedNodes ,
258
+ sortedNodes . length === 1 ? params : undefined
259
+ ) ;
260
+ const tokens = sortTokens ( [
261
+ ctx . Instanceof ,
262
+ ctx . AssignmentOperator ,
263
+ ctx . Less ,
264
+ ctx . Greater ,
265
+ ctx . BinaryOperator
266
+ ] ) ;
267
+ const hasTokens = tokens . length > 0 ;
264
268
265
- groupsOfOperator . forEach ( subgroup => {
266
- currentSegment = [ unaryExpression . shift ( ) ] ;
267
- for ( let i = 0 ; i < subgroup . length ; i ++ ) {
268
- const token = subgroup [ i ] ;
269
- const shiftOperator = isShiftOperator ( subgroup , i ) ;
270
- if ( token . tokenType . name === "Instanceof" ) {
271
- currentSegment . push (
272
- rejectAndJoin ( " " , [
273
- ctx . Instanceof ! [ 0 ] ,
274
- instanceofReferences . shift ( )
275
- ] )
276
- ) ;
277
- } else if ( matchCategory ( token , "'AssignmentOperator'" ) ) {
278
- currentSegment . push (
279
- indent ( rejectAndJoin ( line , [ token , expression . shift ( ) ] ) )
280
- ) ;
281
- } else if (
282
- shiftOperator === "leftShift" ||
283
- shiftOperator === "rightShift"
284
- ) {
285
- currentSegment . push (
286
- rejectAndJoin ( " " , [
287
- rejectAndConcat ( [ token , subgroup [ i + 1 ] ] ) ,
288
- unaryExpression . shift ( )
289
- ] )
290
- ) ;
291
- i ++ ;
292
- } else if ( shiftOperator === "doubleRightShift" ) {
293
- currentSegment . push (
294
- rejectAndJoin ( " " , [
295
- rejectAndConcat ( [ token , subgroup [ i + 1 ] , subgroup [ i + 2 ] ] ) ,
296
- unaryExpression . shift ( )
297
- ] )
298
- ) ;
299
- i += 2 ;
300
- } else if ( matchCategory ( token , "'BinaryOperator'" ) ) {
301
- currentSegment . push (
302
- rejectAndJoin ( line , [ token , unaryExpression . shift ( ) ] )
303
- ) ;
304
- }
305
- }
306
- segmentsSplitByBinaryOperator . push (
307
- group ( rejectAndJoin ( " " , currentSegment ) )
308
- ) ;
309
- } ) ;
269
+ const content = binary ( nodes , tokens , true ) ;
310
270
311
- if ( params !== undefined && params . addParenthesisToWrapStatement ) {
312
- return group (
313
- concat ( [
314
- ifBreak ( "(" , "" ) ,
315
- indent (
316
- concat ( [
317
- softline ,
318
- group (
319
- rejectAndJoinSeps (
320
- sortedBinaryOperators . map ( elt => concat ( [ " " , elt , line ] ) ) ,
321
- segmentsSplitByBinaryOperator
322
- )
323
- )
324
- ] )
325
- ) ,
326
- softline ,
327
- ifBreak ( ")" )
328
- ] )
329
- ) ;
330
- }
331
-
332
- return group (
333
- rejectAndJoinSeps (
334
- sortedBinaryOperators . map ( elt => concat ( [ " " , elt , line ] ) ) ,
335
- segmentsSplitByBinaryOperator
336
- )
337
- ) ;
271
+ return hasTokens && params ?. addParenthesisToWrapStatement
272
+ ? group (
273
+ concat ( [
274
+ ifBreak ( "(" ) ,
275
+ indent ( concat ( [ softline , content ] ) ) ,
276
+ softline ,
277
+ ifBreak ( ")" )
278
+ ] )
279
+ )
280
+ : content ;
338
281
}
339
282
340
- unaryExpression ( ctx : UnaryExpressionCtx ) {
283
+ unaryExpression (
284
+ ctx : UnaryExpressionCtx ,
285
+ params ?: { addParenthesisToWrapStatement ?: boolean }
286
+ ) {
341
287
const unaryPrefixOperator = ctx . UnaryPrefixOperator
342
288
? ctx . UnaryPrefixOperator
343
289
: [ ] ;
344
- const primary = this . visit ( ctx . primary ) ;
290
+ const primary = this . visit ( ctx . primary , params ) ;
345
291
const unarySuffixOperator = ctx . UnarySuffixOperator
346
292
? ctx . UnarySuffixOperator
347
293
: [ ] ;
@@ -369,10 +315,14 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
369
315
] ) ;
370
316
}
371
317
372
- primary ( ctx : PrimaryCtx ) {
318
+ primary (
319
+ ctx : PrimaryCtx ,
320
+ params ?: { addParenthesisToWrapStatement ?: boolean }
321
+ ) {
373
322
const countMethodInvocation = isUniqueMethodInvocation ( ctx . primarySuffix ) ;
374
323
375
324
const primaryPrefix = this . visit ( ctx . primaryPrefix , {
325
+ ...params ,
376
326
shouldBreakBeforeFirstMethodInvocation : countMethodInvocation > 1
377
327
} ) ;
378
328
@@ -561,9 +511,13 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
561
511
return rejectAndConcat ( [ keyWord , typeArguments ] ) ;
562
512
}
563
513
564
- parenthesisExpression ( ctx : ParenthesisExpressionCtx ) {
514
+ parenthesisExpression (
515
+ ctx : ParenthesisExpressionCtx ,
516
+ params ?: { addParenthesisToWrapStatement ?: boolean }
517
+ ) {
565
518
const expression = this . visit ( ctx . expression ) ;
566
- return putIntoBraces ( expression , softline , ctx . LBrace [ 0 ] , ctx . RBrace [ 0 ] ) ;
519
+ const separator = params ?. addParenthesisToWrapStatement ? softline : "" ;
520
+ return putIntoBraces ( expression , separator , ctx . LBrace [ 0 ] , ctx . RBrace [ 0 ] ) ;
567
521
}
568
522
569
523
castExpression ( ctx : CastExpressionCtx ) {
0 commit comments