@@ -179,12 +179,12 @@ else if( !(node.getElseBlock() instanceof EmptyStatement) ) {
179
179
}
180
180
}
181
181
182
- private Expression currentStmtExpr ;
182
+ private Expression currentRootExpr ;
183
183
184
184
@ Override
185
185
public void visitExpressionStatement (ExpressionStatement node ) {
186
- var cse = currentStmtExpr ;
187
- currentStmtExpr = node .getExpression ();
186
+ var cre = currentRootExpr ;
187
+ currentRootExpr = node .getExpression ();
188
188
appendLeadingComments (node );
189
189
appendIndent ();
190
190
if ( node .getStatementLabels () != null ) {
@@ -195,16 +195,19 @@ public void visitExpressionStatement(ExpressionStatement node) {
195
195
}
196
196
visit (node .getExpression ());
197
197
appendNewLine ();
198
- currentStmtExpr = cse ;
198
+ currentRootExpr = cre ;
199
199
}
200
200
201
201
@ Override
202
202
public void visitReturnStatement (ReturnStatement node ) {
203
+ var cre = currentRootExpr ;
204
+ currentRootExpr = node .getExpression ();
203
205
appendLeadingComments (node );
204
206
appendIndent ();
205
207
append ("return " );
206
208
visit (node .getExpression ());
207
209
appendNewLine ();
210
+ currentRootExpr = cre ;
208
211
}
209
212
210
213
@ Override
@@ -359,9 +362,13 @@ public void visitBinaryExpression(BinaryExpression node) {
359
362
inVariableDeclaration = true ;
360
363
visit (node .getLeftExpression ());
361
364
inVariableDeclaration = false ;
362
- if ( !(node .getRightExpression () instanceof EmptyExpression ) ) {
365
+ var source = node .getRightExpression ();
366
+ if ( !(source instanceof EmptyExpression ) ) {
363
367
append (" = " );
364
- visit (node .getRightExpression ());
368
+ var cre = currentRootExpr ;
369
+ currentRootExpr = source ;
370
+ visit (source );
371
+ currentRootExpr = cre ;
365
372
}
366
373
return ;
367
374
}
@@ -378,12 +385,6 @@ public void visitBinaryExpression(BinaryExpression node) {
378
385
if ( beginWrappedPipeChain )
379
386
inWrappedPipeChain = true ;
380
387
381
- Expression cse = null ;
382
- if ( currentStmtExpr == node && node .getOperation ().isA (Types .ASSIGNMENT_OPERATOR ) ) {
383
- cse = currentStmtExpr ;
384
- currentStmtExpr = node .getRightExpression ();
385
- }
386
-
387
388
visit (node .getLeftExpression ());
388
389
389
390
if ( inWrappedPipeChain ) {
@@ -399,15 +400,21 @@ public void visitBinaryExpression(BinaryExpression node) {
399
400
400
401
var iwpc = inWrappedPipeChain ;
401
402
inWrappedPipeChain = false ;
402
- visit (node .getRightExpression ());
403
+ if ( node .getOperation ().isA (Types .ASSIGNMENT_OPERATOR ) ) {
404
+ var source = node .getRightExpression ();
405
+ var cre = currentRootExpr ;
406
+ currentRootExpr = source ;
407
+ visit (source );
408
+ currentRootExpr = cre ;
409
+ }
410
+ else {
411
+ visit (node .getRightExpression ());
412
+ }
403
413
inWrappedPipeChain = iwpc ;
404
414
405
415
if ( inWrappedPipeChain )
406
416
decIndent ();
407
417
408
- if ( cse != null )
409
- currentStmtExpr = cse ;
410
-
411
418
if ( beginWrappedPipeChain )
412
419
inWrappedPipeChain = false ;
413
420
}
@@ -485,7 +492,7 @@ public void visitParameters(Parameter[] parameters) {
485
492
}
486
493
append (param .getName ());
487
494
if ( param .hasInitialExpression () ) {
488
- append ('=' );
495
+ append (" = " );
489
496
visit (param .getInitialExpression ());
490
497
}
491
498
if ( i + 1 < parameters .length )
@@ -510,7 +517,7 @@ public void visitTupleExpression(TupleExpression node) {
510
517
511
518
@ Override
512
519
public void visitListExpression (ListExpression node ) {
513
- var wrap = shouldWrapExpression (node );
520
+ var wrap = hasTrailingComma ( node ) || shouldWrapExpression (node );
514
521
append ('[' );
515
522
if ( wrap )
516
523
incIndent ();
@@ -525,13 +532,14 @@ public void visitListExpression(ListExpression node) {
525
532
526
533
protected void visitPositionalArgs (List <Expression > args , boolean wrap ) {
527
534
var comma = wrap ? "," : ", " ;
535
+ var trailingComma = wrap && args .size () > 1 ;
528
536
for ( int i = 0 ; i < args .size (); i ++ ) {
529
537
if ( wrap ) {
530
538
appendNewLine ();
531
539
appendIndent ();
532
540
}
533
541
visit (args .get (i ));
534
- if ( i + 1 < args .size () )
542
+ if ( trailingComma || i + 1 < args .size () )
535
543
append (comma );
536
544
}
537
545
}
@@ -542,7 +550,7 @@ public void visitMapExpression(MapExpression node) {
542
550
append ("[:]" );
543
551
return ;
544
552
}
545
- var wrap = shouldWrapExpression (node );
553
+ var wrap = hasTrailingComma ( node ) || shouldWrapExpression (node );
546
554
append ('[' );
547
555
if ( wrap )
548
556
incIndent ();
@@ -557,13 +565,14 @@ public void visitMapExpression(MapExpression node) {
557
565
558
566
protected void visitNamedArgs (List <MapEntryExpression > args , boolean wrap ) {
559
567
var comma = wrap ? "," : ", " ;
568
+ var trailingComma = wrap && args .size () > 1 ;
560
569
for ( int i = 0 ; i < args .size (); i ++ ) {
561
570
if ( wrap ) {
562
571
appendNewLine ();
563
572
appendIndent ();
564
573
}
565
574
visit (args .get (i ));
566
- if ( i + 1 < args .size () )
575
+ if ( trailingComma || i + 1 < args .size () )
567
576
append (comma );
568
577
}
569
578
}
@@ -713,6 +722,10 @@ public void visit(Expression node) {
713
722
714
723
// helpers
715
724
725
+ private static boolean hasTrailingComma (Expression node ) {
726
+ return node .getNodeMetaData (TRAILING_COMMA ) != null ;
727
+ }
728
+
716
729
public static boolean isLegacyType (ClassNode cn ) {
717
730
return cn .getNodeMetaData (LEGACY_TYPE ) != null ;
718
731
}
@@ -722,13 +735,15 @@ private boolean shouldWrapExpression(Expression node) {
722
735
}
723
736
724
737
private boolean shouldWrapMethodCall (MethodCallExpression node ) {
738
+ if ( hasTrailingComma (node .getArguments ()) )
739
+ return true ;
725
740
var start = node .getMethod ();
726
741
var end = node .getArguments ();
727
742
return start .getLineNumber () < end .getLastLineNumber ();
728
743
}
729
744
730
745
private boolean shouldWrapMethodChain (MethodCallExpression node ) {
731
- if ( currentStmtExpr != node )
746
+ if ( currentRootExpr != node )
732
747
return false ;
733
748
if ( !shouldWrapExpression (node ) )
734
749
return false ;
@@ -746,7 +761,7 @@ private boolean shouldWrapMethodChain(MethodCallExpression node) {
746
761
}
747
762
748
763
private boolean shouldWrapPipeExpression (BinaryExpression node ) {
749
- return currentStmtExpr == node && node .getOperation ().isA (Types .PIPE ) && shouldWrapExpression (node );
764
+ return currentRootExpr == node && node .getOperation ().isA (Types .PIPE ) && shouldWrapExpression (node );
750
765
}
751
766
752
767
private static final String SLASH_STR = "/" ;
@@ -761,6 +776,7 @@ private boolean shouldWrapPipeExpression(BinaryExpression node) {
761
776
private static final String LEADING_COMMENTS = "_LEADING_COMMENTS" ;
762
777
private static final String LEGACY_TYPE = "_LEGACY_TYPE" ;
763
778
private static final String QUOTE_CHAR = "_QUOTE_CHAR" ;
779
+ private static final String TRAILING_COMMA = "_TRAILING_COMMA" ;
764
780
private static final String TRAILING_COMMENT = "_TRAILING_COMMENT" ;
765
781
private static final String VERBATIM_TEXT = "_VERBATIM_TEXT" ;
766
782
0 commit comments