@@ -383,7 +383,7 @@ class AstNodeVisitor extends ThrowingAstVisitor<Piece> with PieceFactory {
383
383
if (node.redirectedConstructor case var constructor? ) {
384
384
redirect = AssignPiece (
385
385
tokenPiece (node.separator! ), nodePiece (constructor),
386
- isValueDelimited : false );
386
+ allowInnerSplit : false );
387
387
} else if (node.initializers.isNotEmpty) {
388
388
initializerSeparator = tokenPiece (node.separator! );
389
389
initializers = createList (node.initializers,
@@ -595,7 +595,7 @@ class AstNodeVisitor extends ThrowingAstVisitor<Piece> with PieceFactory {
595
595
var expression = nodePiece (node.expression);
596
596
597
597
b.add (AssignPiece (operatorPiece, expression,
598
- isValueDelimited : node.expression.canBlockSplit));
598
+ allowInnerSplit : node.expression.canBlockSplit));
599
599
b.token (node.semicolon);
600
600
});
601
601
}
@@ -693,117 +693,36 @@ class AstNodeVisitor extends ThrowingAstVisitor<Piece> with PieceFactory {
693
693
694
694
@override
695
695
Piece visitForElement (ForElement node) {
696
- throw UnimplementedError ();
697
- }
698
-
699
- @override
700
- Piece visitForStatement (ForStatement node) {
701
696
var forKeyword = buildPiece ((b) {
702
697
b.modifier (node.awaitKeyword);
703
698
b.token (node.forKeyword);
704
699
});
705
700
706
- Piece forPartsPiece;
707
- switch (node.forLoopParts) {
708
- // Edge case: A totally empty for loop is formatted just as `(;;)` with
709
- // no splits or spaces anywhere.
710
- case ForPartsWithExpression (
711
- initialization: null ,
712
- leftSeparator: Token (precedingComments: null ),
713
- condition: null ,
714
- rightSeparator: Token (precedingComments: null ),
715
- updaters: NodeList (isEmpty: true ),
716
- ) &&
717
- var forParts
718
- when node.rightParenthesis.precedingComments == null :
719
- forPartsPiece = buildPiece ((b) {
720
- b.token (node.leftParenthesis);
721
- b.token (forParts.leftSeparator);
722
- b.token (forParts.rightSeparator);
723
- b.token (node.rightParenthesis);
724
- });
725
-
726
- case ForParts forParts &&
727
- ForPartsWithDeclarations (variables: AstNode ? initializer):
728
- case ForParts forParts &&
729
- ForPartsWithExpression (initialization: AstNode ? initializer):
730
- // In a C-style for loop, treat the for loop parts like an argument list
731
- // where each clause is a separate argument. This means that when they
732
- // split, they split like:
733
- //
734
- // for (
735
- // initializerClause;
736
- // conditionClause;
737
- // incrementClause
738
- // ) {
739
- // body;
740
- // }
741
- var partsList =
742
- DelimitedListBuilder (this , const ListStyle (commas: Commas .none));
743
- partsList.leftBracket (node.leftParenthesis);
744
-
745
- // The initializer clause.
746
- if (initializer != null ) {
747
- partsList.addCommentsBefore (initializer.beginToken);
748
- partsList.add (buildPiece ((b) {
749
- b.visit (initializer);
750
- b.token (forParts.leftSeparator);
751
- }));
752
- } else {
753
- // No initializer, so look at the comments before `;`.
754
- partsList.addCommentsBefore (forParts.leftSeparator);
755
- partsList.add (tokenPiece (forParts.leftSeparator));
756
- }
757
-
758
- // The condition clause.
759
- if (forParts.condition case var conditionExpression? ) {
760
- partsList.addCommentsBefore (conditionExpression.beginToken);
761
- partsList.add (buildPiece ((b) {
762
- b.visit (conditionExpression);
763
- b.token (forParts.rightSeparator);
764
- }));
765
- } else {
766
- partsList.addCommentsBefore (forParts.rightSeparator);
767
- partsList.add (tokenPiece (forParts.rightSeparator));
768
- }
769
-
770
- // The update clauses.
771
- if (forParts.updaters.isNotEmpty) {
772
- partsList.addCommentsBefore (forParts.updaters.first.beginToken);
773
- partsList.add (createList (forParts.updaters,
774
- style: const ListStyle (commas: Commas .nonTrailing)));
775
- }
701
+ var forPartsPiece = createForLoopParts (
702
+ node.leftParenthesis, node.forLoopParts, node.rightParenthesis);
703
+ var body = nodePiece (node.body);
776
704
777
- partsList. rightBracket (node.rightParenthesis);
778
- forPartsPiece = partsList. build ( );
705
+ var forPiece = ForPiece (forKeyword, forPartsPiece, body,
706
+ hasBlockBody : node.body.isSpreadCollection );
779
707
780
- case ForPartsWithPattern ():
781
- throw UnimplementedError ();
708
+ // It looks weird to have multiple nested control flow elements on the same
709
+ // line, so force the outer one to split if there is an inner one.
710
+ if (node.body.isControlFlowElement) {
711
+ forPiece.pin (State .split);
712
+ }
782
713
783
- case ForEachParts forEachParts &&
784
- ForEachPartsWithDeclaration (loopVariable: AstNode variable):
785
- case ForEachParts forEachParts &&
786
- ForEachPartsWithIdentifier (identifier: AstNode variable):
787
- // If a for-in loop, treat the for parts like an assignment, so they
788
- // split like:
789
- //
790
- // for (var variable in [
791
- // initializer,
792
- // ]) {
793
- // body;
794
- // }
795
- forPartsPiece = buildPiece ((b) {
796
- b.token (node.leftParenthesis);
797
- b.add (createAssignment (
798
- variable, forEachParts.inKeyword, forEachParts.iterable,
799
- splitBeforeOperator: true ));
800
- b.token (node.rightParenthesis);
801
- });
714
+ return forPiece;
715
+ }
802
716
803
- case ForEachPartsWithPattern ():
804
- throw UnimplementedError ();
805
- }
717
+ @override
718
+ Piece visitForStatement (ForStatement node) {
719
+ var forKeyword = buildPiece ((b) {
720
+ b.modifier (node.awaitKeyword);
721
+ b.token (node.forKeyword);
722
+ });
806
723
724
+ var forPartsPiece = createForLoopParts (
725
+ node.leftParenthesis, node.forLoopParts, node.rightParenthesis);
807
726
var body = nodePiece (node.body);
808
727
809
728
return ForPiece (forKeyword, forPartsPiece, body,
@@ -1824,7 +1743,7 @@ class AstNodeVisitor extends ThrowingAstVisitor<Piece> with PieceFactory {
1824
1743
var initializerPiece = nodePiece (initializer, commaAfter: true );
1825
1744
1826
1745
variables.add (AssignPiece (variablePiece, initializerPiece,
1827
- isValueDelimited : initializer.canBlockSplit));
1746
+ allowInnerSplit : initializer.canBlockSplit));
1828
1747
} else {
1829
1748
variables.add (tokenPiece (variable.name, commaAfter: true ));
1830
1749
}
0 commit comments