@@ -12,7 +12,6 @@ import '../piece/control_flow.dart';
12
12
import '../piece/for.dart' ;
13
13
import '../piece/if_case.dart' ;
14
14
import '../piece/infix.dart' ;
15
- import '../piece/leading_comment.dart' ;
16
15
import '../piece/list.dart' ;
17
16
import '../piece/piece.dart' ;
18
17
import '../piece/sequence.dart' ;
@@ -285,15 +284,6 @@ mixin PieceFactory {
285
284
return builder.build ();
286
285
}
287
286
288
- /// If [leadingComments] is not empty, returns [piece] wrapped in a
289
- /// [LeadingCommentPiece] that prepends them.
290
- ///
291
- /// Otherwise, just returns [piece] .
292
- Piece prependLeadingComments (List <Piece > leadingComments, Piece piece) {
293
- if (leadingComments.isEmpty) return piece;
294
- return LeadingCommentPiece (leadingComments, piece);
295
- }
296
-
297
287
/// Writes the leading keyword and parenthesized expression at the beginning
298
288
/// of an `if` , `while` , or `switch` expression or statement.
299
289
void writeControlFlowStart (Token keyword, Token leftParenthesis,
@@ -500,21 +490,18 @@ mixin PieceFactory {
500
490
501
491
// Hoist any leading comments so they don't force the for-in clauses
502
492
// to split.
503
- var leadingComments = const < Piece > [];
504
- if (metadata.isEmpty) {
505
- leadingComments = pieces.takeCommentsBefore (keyword);
506
- }
507
-
508
- // Use a nested piece so that the metadata precedes the keyword and
509
- // not the `(`.
510
- var forInPiece =
511
- pieces.build (metadata: metadata, inlineMetadata: true , () {
512
- pieces.token (keyword);
513
- pieces.space ();
514
- _writeForIn (pattern, forEachParts.inKeyword, forEachParts.iterable);
493
+ pieces.hoistLeadingComments (
494
+ metadata.firstOrNull? .beginToken ?? keyword, () {
495
+ // Use a nested piece so that the metadata precedes the keyword and
496
+ // not the `(`.
497
+ return pieces.build (metadata: metadata, inlineMetadata: true , () {
498
+ pieces.token (keyword);
499
+ pieces.space ();
500
+ _writeForIn (
501
+ pattern, forEachParts.inKeyword, forEachParts.iterable);
502
+ });
515
503
});
516
504
517
- pieces.add (prependLeadingComments (leadingComments, forInPiece));
518
505
pieces.token (rightParenthesis);
519
506
});
520
507
}
@@ -645,22 +632,21 @@ mixin PieceFactory {
645
632
// int f() {}
646
633
var firstToken =
647
634
modifiers.nonNulls.firstOrNull ?? returnType.firstNonCommentToken;
648
- var leadingComments = pieces.takeCommentsBefore (firstToken);
635
+ pieces.hoistLeadingComments (firstToken, () {
636
+ var returnTypePiece = pieces.build (() {
637
+ for (var keyword in modifiers) {
638
+ pieces.modifier (keyword);
639
+ }
649
640
650
- var returnTypePiece = pieces.build (() {
651
- for (var keyword in modifiers) {
652
- pieces.modifier (keyword);
653
- }
641
+ pieces.visit (returnType);
642
+ });
654
643
655
- pieces.visit (returnType);
656
- });
644
+ var signature = pieces.build (() {
645
+ writeFunction ();
646
+ });
657
647
658
- var signature = pieces.build (() {
659
- writeFunction ();
648
+ return VariablePiece (returnTypePiece, [signature], hasType: true );
660
649
});
661
-
662
- pieces.add (prependLeadingComments (leadingComments,
663
- VariablePiece (returnTypePiece, [signature], hasType: true )));
664
650
}
665
651
666
652
/// If [parameter] has a [defaultValue] then writes a piece for the parameter
@@ -941,10 +927,6 @@ mixin PieceFactory {
941
927
/// separate tokens, as in `foo is! Bar` .
942
928
void writeInfix (AstNode left, Token operator , AstNode right,
943
929
{bool hanging = false , Token ? operator2}) {
944
- // Hoist any comments before the first operand so they don't force the
945
- // infix operator to split.
946
- var leadingComments = pieces.takeCommentsBefore (left.firstNonCommentToken);
947
-
948
930
var leftPiece = pieces.build (() {
949
931
pieces.visit (left);
950
932
if (hanging) {
@@ -964,8 +946,7 @@ mixin PieceFactory {
964
946
pieces.visit (right);
965
947
});
966
948
967
- pieces.add (prependLeadingComments (
968
- leadingComments, InfixPiece ([leftPiece, rightPiece])));
949
+ pieces.add (InfixPiece ([leftPiece, rightPiece]));
969
950
}
970
951
971
952
/// Writes a chained infix operation: a binary operator expression, or
@@ -986,10 +967,6 @@ mixin PieceFactory {
986
967
void writeInfixChain <T extends AstNode >(
987
968
T node, BinaryOperation Function (T node) destructure,
988
969
{int ? precedence, bool indent = true }) {
989
- // Hoist any comments before the first operand so they don't force the
990
- // infix operator to split.
991
- var leadingComments = pieces.takeCommentsBefore (node.firstNonCommentToken);
992
-
993
970
var operands = < Piece > [];
994
971
995
972
void traverse (AstNode e) {
@@ -1017,8 +994,7 @@ mixin PieceFactory {
1017
994
traverse (node);
1018
995
}));
1019
996
1020
- pieces.add (prependLeadingComments (
1021
- leadingComments, InfixPiece (operands, indent: indent)));
997
+ pieces.add (InfixPiece (operands, indent: indent));
1022
998
}
1023
999
1024
1000
/// Writes a [ListPiece] for the given bracket-delimited set of elements.
@@ -1421,17 +1397,14 @@ mixin PieceFactory {
1421
1397
void _writeForIn (AstNode leftHandSide, Token inKeyword, Expression sequence) {
1422
1398
// Hoist any leading comments so they don't force the for-in clauses to
1423
1399
// split.
1424
- var leadingComments =
1425
- pieces.takeCommentsBefore (leftHandSide.firstNonCommentToken);
1426
-
1427
- var leftPiece =
1428
- nodePiece (leftHandSide, context: NodeContext .forLoopVariable);
1429
- var sequencePiece = _createForInSequence (inKeyword, sequence);
1400
+ pieces.hoistLeadingComments (leftHandSide.firstNonCommentToken, () {
1401
+ var leftPiece =
1402
+ nodePiece (leftHandSide, context: NodeContext .forLoopVariable);
1403
+ var sequencePiece = _createForInSequence (inKeyword, sequence);
1430
1404
1431
- pieces.add (prependLeadingComments (
1432
- leadingComments,
1433
- ForInPiece (leftPiece, sequencePiece,
1434
- canBlockSplitSequence: sequence.canBlockSplit)));
1405
+ return ForInPiece (leftPiece, sequencePiece,
1406
+ canBlockSplitSequence: sequence.canBlockSplit);
1407
+ });
1435
1408
}
1436
1409
1437
1410
/// Writes the `<variable> in <expression>` part of a for-in loop when the
@@ -1444,28 +1417,24 @@ mixin PieceFactory {
1444
1417
DeclaredIdentifier identifier, Token inKeyword, Expression sequence) {
1445
1418
// Hoist any leading comments so they don't force the for-in clauses
1446
1419
// to split.
1447
- var leadingComments = const < Piece > [];
1448
- if (identifier.metadata.isEmpty) {
1449
- leadingComments = pieces
1450
- .takeCommentsBefore (identifier.firstTokenAfterCommentAndMetadata);
1451
- }
1452
-
1453
- // Use a nested piece so that the metadata precedes the keyword and
1454
- // not the `(`.
1455
- var forInPiece =
1456
- pieces.build (metadata: identifier.metadata, inlineMetadata: true , () {
1457
- var leftPiece = pieces.build (() {
1458
- writeParameter (
1459
- modifiers: [identifier.keyword], identifier.type, identifier.name);
1460
- });
1420
+ pieces.hoistLeadingComments (identifier.beginToken, () {
1421
+ // Use a nested piece so that the metadata precedes the keyword and
1422
+ // not the `(`.
1423
+ return pieces.build (metadata: identifier.metadata, inlineMetadata: true ,
1424
+ () {
1425
+ var leftPiece = pieces.build (() {
1426
+ writeParameter (
1427
+ modifiers: [identifier.keyword],
1428
+ identifier.type,
1429
+ identifier.name);
1430
+ });
1461
1431
1462
- var sequencePiece = _createForInSequence (inKeyword, sequence);
1432
+ var sequencePiece = _createForInSequence (inKeyword, sequence);
1463
1433
1464
- pieces.add (ForInPiece (leftPiece, sequencePiece,
1465
- canBlockSplitSequence: sequence.canBlockSplit));
1434
+ pieces.add (ForInPiece (leftPiece, sequencePiece,
1435
+ canBlockSplitSequence: sequence.canBlockSplit));
1436
+ });
1466
1437
});
1467
-
1468
- pieces.add (prependLeadingComments (leadingComments, forInPiece));
1469
1438
}
1470
1439
1471
1440
/// Creates a piece for the `in <sequence>` part of a for-in loop.
0 commit comments