@@ -8,15 +8,14 @@ import '../ast_extensions.dart';
8
8
import '../piece/adjacent.dart' ;
9
9
import '../piece/assign.dart' ;
10
10
import '../piece/clause.dart' ;
11
+ import '../piece/control_flow.dart' ;
11
12
import '../piece/for.dart' ;
12
13
import '../piece/function.dart' ;
13
14
import '../piece/if_case.dart' ;
14
15
import '../piece/infix.dart' ;
15
16
import '../piece/list.dart' ;
16
17
import '../piece/piece.dart' ;
17
- import '../piece/postfix.dart' ;
18
18
import '../piece/sequence.dart' ;
19
- import '../piece/try.dart' ;
20
19
import '../piece/type.dart' ;
21
20
import '../piece/variable.dart' ;
22
21
import 'ast_node_visitor.dart' ;
@@ -506,12 +505,21 @@ mixin PieceFactory {
506
505
_ => false ,
507
506
};
508
507
509
- var forPiece = ForPiece (forKeywordPiece, forPartsPiece, bodyPiece,
510
- indentForParts: indentHeader, hasBlockBody: hasBlockBody);
511
-
512
- if (forceSplitBody) forPiece.pin (State .split);
513
-
514
- pieces.add (forPiece);
508
+ if (hasBlockBody) {
509
+ pieces
510
+ .add (ForPiece (forKeywordPiece, forPartsPiece, indent: indentHeader));
511
+ pieces.space ();
512
+ pieces.add (bodyPiece);
513
+ } else {
514
+ var forPiece = ControlFlowPiece ();
515
+ forPiece.add (
516
+ ForPiece (forKeywordPiece, forPartsPiece, indent: indentHeader),
517
+ bodyPiece,
518
+ isBlock: false );
519
+
520
+ if (forceSplitBody) forPiece.pin (State .split);
521
+ pieces.add (forPiece);
522
+ }
515
523
}
516
524
517
525
/// Writes a normal (not function-typed) formal parameter with a name and/or
@@ -738,43 +746,40 @@ mixin PieceFactory {
738
746
739
747
/// Writes a [TryPiece] for try statement.
740
748
void writeTry (TryStatement tryStatement) {
741
- var piece = TryPiece ();
742
-
743
- var tryHeader = tokenPiece (tryStatement.tryKeyword);
744
- var tryBlock = pieces.build (() {
745
- writeBlock (tryStatement.body);
746
- });
747
- piece.add (tryHeader, tryBlock);
749
+ pieces.token (tryStatement.tryKeyword);
750
+ pieces.space ();
751
+ writeBlock (tryStatement.body);
748
752
749
753
for (var i = 0 ; i < tryStatement.catchClauses.length; i++ ) {
750
754
var catchClause = tryStatement.catchClauses[i];
751
755
752
- var catchClauseHeader = pieces.build (() {
753
- if (catchClause.onKeyword case var onKeyword? ) {
754
- pieces.token (onKeyword, spaceAfter: true );
755
- pieces.visit (catchClause.exceptionType);
756
- }
756
+ pieces.space ();
757
+ if (catchClause.onKeyword case var onKeyword? ) {
758
+ pieces.token (onKeyword, spaceAfter: true );
759
+ pieces.visit (catchClause.exceptionType);
760
+ }
757
761
758
- if (catchClause.onKeyword != null && catchClause.catchKeyword != null ) {
759
- pieces.space ();
760
- }
762
+ if (catchClause.onKeyword != null && catchClause.catchKeyword != null ) {
763
+ pieces.space ();
764
+ }
761
765
762
- if (catchClause.catchKeyword case var catchKeyword? ) {
763
- pieces.token (catchKeyword);
764
- pieces.space ();
766
+ if (catchClause.catchKeyword case var catchKeyword? ) {
767
+ pieces.token (catchKeyword);
768
+ pieces.space ();
765
769
766
- var parameters = DelimitedListBuilder (this );
767
- parameters.leftBracket (catchClause.leftParenthesis! );
768
- if (catchClause.exceptionParameter case var exceptionParameter? ) {
769
- parameters.visit (exceptionParameter);
770
- }
771
- if (catchClause.stackTraceParameter case var stackTraceParameter? ) {
772
- parameters.visit (stackTraceParameter);
773
- }
774
- parameters.rightBracket (catchClause.rightParenthesis! );
775
- pieces.add (parameters.build ());
770
+ var parameters = DelimitedListBuilder (this );
771
+ parameters.leftBracket (catchClause.leftParenthesis! );
772
+ if (catchClause.exceptionParameter case var exceptionParameter? ) {
773
+ parameters.visit (exceptionParameter);
776
774
}
777
- });
775
+ if (catchClause.stackTraceParameter case var stackTraceParameter? ) {
776
+ parameters.visit (stackTraceParameter);
777
+ }
778
+ parameters.rightBracket (catchClause.rightParenthesis! );
779
+ pieces.add (parameters.build ());
780
+ }
781
+
782
+ pieces.space ();
778
783
779
784
// Edge case: When there's another catch/on/finally after this one, we
780
785
// want to force the block to split even if it's empty.
@@ -787,72 +792,70 @@ mixin PieceFactory {
787
792
// }
788
793
var forceSplit = i < tryStatement.catchClauses.length - 1 ||
789
794
tryStatement.finallyBlock != null ;
790
- var catchClauseBody = pieces.build (() {
791
- writeBlock (catchClause.body, forceSplit: forceSplit);
792
- });
793
- piece.add (catchClauseHeader, catchClauseBody);
795
+ writeBlock (catchClause.body, forceSplit: forceSplit);
794
796
}
795
797
796
798
if (tryStatement.finallyBlock case var finallyBlock? ) {
797
- var finallyHeader = tokenPiece (tryStatement.finallyKeyword! );
798
- var finallyBody = pieces.build (() {
799
- writeBlock (finallyBlock);
800
- });
801
- piece.add (finallyHeader, finallyBody);
799
+ pieces.space ();
800
+ pieces.token (tryStatement.finallyKeyword! );
801
+ pieces.space ();
802
+ writeBlock (finallyBlock);
802
803
}
803
-
804
- pieces.add (piece);
805
804
}
806
805
807
806
/// Writes an [ImportPiece] for an import or export directive.
808
807
void writeImport (NamespaceDirective directive, Token keyword,
809
808
{Token ? deferredKeyword, Token ? asKeyword, SimpleIdentifier ? prefix}) {
810
809
pieces.withMetadata (directive.metadata, () {
811
- pieces.token (keyword);
812
- pieces.space ();
813
- pieces.visit (directive.uri);
810
+ if (directive.configurations.isEmpty && asKeyword == null ) {
811
+ // If there are no configurations or prefix (the common case), just
812
+ // write the import directly inline.
813
+ pieces.token (keyword);
814
+ pieces.space ();
815
+ pieces.visit (directive.uri);
816
+ } else {
817
+ // Otherwise, allow splitting between the configurations and prefix.
818
+ var sections = [
819
+ pieces.build (() {
820
+ pieces.token (keyword);
821
+ pieces.space ();
822
+ pieces.visit (directive.uri);
823
+ })
824
+ ];
814
825
815
- if (directive.configurations.isNotEmpty) {
816
- var configurations = < Piece > [];
817
826
for (var configuration in directive.configurations) {
818
- configurations .add (nodePiece (configuration));
827
+ sections .add (nodePiece (configuration));
819
828
}
820
829
821
- pieces.add (PostfixPiece (configurations));
822
- }
823
-
824
- if (asKeyword != null ) {
825
- pieces.add (PostfixPiece ([
826
- pieces.build (() {
830
+ if (asKeyword != null ) {
831
+ sections.add (pieces.build (() {
827
832
pieces.token (deferredKeyword, spaceAfter: true );
828
833
pieces.token (asKeyword);
829
834
pieces.space ();
830
835
pieces.visit (prefix! );
831
- })
832
- ]));
836
+ }));
837
+ }
838
+
839
+ pieces.add (InfixPiece (const [], sections));
833
840
}
834
841
835
842
if (directive.combinators.isNotEmpty) {
836
- var combinators = < ClausePiece > [];
843
+ var combinators = < Piece > [];
837
844
for (var combinatorNode in directive.combinators) {
838
- var combinatorKeyword = tokenPiece (combinatorNode.keyword);
839
-
840
845
switch (combinatorNode) {
841
846
case HideCombinator (hiddenNames: var names):
842
847
case ShowCombinator (shownNames: var names):
843
- var parts = < Piece > [];
844
- for (var name in names) {
845
- parts.add (tokenPiece (name.token, commaAfter: true ));
846
- }
847
-
848
- var combinator = ClausePiece (combinatorKeyword, parts);
849
- combinators.add (combinator);
848
+ combinators.add (InfixPiece (const [], [
849
+ tokenPiece (combinatorNode.keyword),
850
+ for (var name in names)
851
+ tokenPiece (name.token, commaAfter: true ),
852
+ ]));
850
853
default :
851
854
throw StateError ('Unknown combinator type $combinatorNode .' );
852
855
}
853
856
}
854
857
855
- pieces.add (ClausesPiece (combinators));
858
+ pieces.add (ClausePiece (combinators));
856
859
}
857
860
858
861
pieces.token (directive.semicolon);
@@ -1209,17 +1212,13 @@ mixin PieceFactory {
1209
1212
}
1210
1213
});
1211
1214
1212
- var clauses = < ClausePiece > [];
1215
+ var clauses = < Piece > [];
1213
1216
1214
1217
void typeClause (Token keyword, List <AstNode > types) {
1215
- var keywordPiece = tokenPiece (keyword);
1216
-
1217
- var typePieces = < Piece > [];
1218
- for (var type in types) {
1219
- typePieces.add (nodePiece (type, commaAfter: true ));
1220
- }
1221
-
1222
- clauses.add (ClausePiece (keywordPiece, typePieces));
1218
+ clauses.add (InfixPiece (const [], [
1219
+ tokenPiece (keyword),
1220
+ for (var type in types) nodePiece (type, commaAfter: true ),
1221
+ ]));
1223
1222
}
1224
1223
1225
1224
if (extendsClause != null ) {
@@ -1248,9 +1247,9 @@ mixin PieceFactory {
1248
1247
[if (nativeClause.name case var name? ) name]);
1249
1248
}
1250
1249
1251
- ClausesPiece ? clausesPiece;
1250
+ ClausePiece ? clausesPiece;
1252
1251
if (clauses.isNotEmpty) {
1253
- clausesPiece = ClausesPiece (clauses,
1252
+ clausesPiece = ClausePiece (clauses,
1254
1253
allowLeadingClause: extendsClause != null || onClause != null );
1255
1254
}
1256
1255
0 commit comments