Skip to content

Commit

Permalink
Apply review feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
munificent committed Oct 27, 2023
1 parent 6658e5a commit bca0281
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 72 deletions.
14 changes: 8 additions & 6 deletions lib/src/front_end/ast_node_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -461,18 +461,20 @@ class AstNodeVisitor extends ThrowingAstVisitor<void>
when node.rightParenthesis.precedingComments == null:
token(forLoopParts.leftSeparator);
token(forLoopParts.rightSeparator);

case ForPartsWithDeclarations():
partsList.addCommentsBefore(forLoopParts.variables.beginToken);
visit(forLoopParts.variables);
finishForParts(forLoopParts, partsList);

case ForPartsWithExpression(:var initialization?):
partsList.addCommentsBefore(initialization.beginToken);
visit(initialization);
finishForParts(forLoopParts, partsList);

case ForPartsWithExpression():
if (forLoopParts.initialization case var initializer?) {
partsList.addCommentsBefore(initializer.beginToken);
visit(initializer);
} else {
partsList.addCommentsBefore(forLoopParts.leftSeparator);
}
// No initializer part.
partsList.addCommentsBefore(forLoopParts.leftSeparator);
finishForParts(forLoopParts, partsList);

case ForPartsWithPattern():
Expand Down
2 changes: 1 addition & 1 deletion lib/src/front_end/piece_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ mixin PieceFactory implements CommentWriter {
isValueDelimited: rightHandSide.isDelimited));
}

/// Writes the condition and updaters part of for a [ForParts] after the
/// Writes the condition and updaters parts of a [ForParts] after the
/// subclass's initializer clause has been written.
void finishForParts(ForParts forLoopParts, DelimitedListBuilder partsList) {
token(forLoopParts.leftSeparator);
Expand Down
65 changes: 0 additions & 65 deletions lib/src/piece/for.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,68 +63,3 @@ class ForPiece extends Piece {
@override
String toString() => 'For';
}

class ForPartsPiece extends Piece {
final Piece? _initializer;
final Piece _leftSemicolon;
final Piece? _condition;
final Piece _rightSemicolon;
final Piece? _increment;

ForPartsPiece(this._initializer, this._leftSemicolon, this._condition,
this._rightSemicolon, this._increment);

/// If there is at least one else or else-if clause, then it always splits.
@override
List<State> get additionalStates => [if (_hasAnyClause) State.split];

/// Whether any of the clauses have been provided or its an empty loop like:
///
/// ```
/// for (;;) body;
/// ```
bool get _hasAnyClause =>
_initializer != null || _condition != null || _increment != null;

@override
void format(CodeWriter writer, State state) {
if (state == State.unsplit) writer.setAllowNewlines(false);

writer.splitIf(state == State.split, space: false, indent: Indent.block);

if (_initializer case var initializer?) {
writer.format(initializer);
}

writer.format(_leftSemicolon);
writer.splitIf(state == State.split,
space: _condition != null, indent: Indent.block);

if (_condition case var condition?) {
writer.format(condition);
}

writer.format(_rightSemicolon);

if (_increment case var increment?) {
writer.splitIf(state == State.split,
space: _increment != null, indent: Indent.block);

writer.format(increment);
}

writer.splitIf(state == State.split, space: false, indent: Indent.none);
}

@override
void forEachChild(void Function(Piece piece) callback) {
if (_initializer case var initializer?) callback(initializer);
callback(_leftSemicolon);
if (_condition case var condition?) callback(condition);
callback(_rightSemicolon);
if (_increment case var increment?) callback(increment);
}

@override
String toString() => 'ForParts';
}

0 comments on commit bca0281

Please sign in to comment.