Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify ChainPiece. #1595

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 10 additions & 18 deletions lib/src/piece/chain.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ final class ChainPiece extends Piece {
writer.format(_target);

for (var i = 0; i < _calls.length; i++) {
_formatCall(writer, state, i, allowNewlines: false);
writer.format(_calls[i]._call);
}

case _splitAfterProperties:
Expand All @@ -177,7 +177,10 @@ final class ChainPiece extends Piece {

for (var i = 0; i < _calls.length; i++) {
writer.splitIf(i >= _leadingProperties, space: false);
_formatCall(writer, state, i, allowNewlines: i >= _leadingProperties);

// Every non-property call except the last will be on its own line.
writer.format(_calls[i]._call,
separate: i >= _leadingProperties && i < _calls.length - 1);
}

writer.popIndent();
Expand All @@ -186,7 +189,7 @@ final class ChainPiece extends Piece {
writer.format(_target);

for (var i = 0; i < _calls.length; i++) {
_formatCall(writer, state, i, allowNewlines: i == _blockCallIndex);
writer.format(_calls[i]._call);
}

case State.split:
Expand All @@ -195,27 +198,16 @@ final class ChainPiece extends Piece {

for (var i = 0; i < _calls.length; i++) {
writer.newline();
_formatCall(writer, state, i);

// The chain is fully split so every call except for the last is on
// its own line.
writer.format(_calls[i]._call, separate: i < _calls.length - 1);
}

writer.popIndent();
}
}

void _formatCall(CodeWriter writer, State state, int i,
{bool allowNewlines = true}) {
// If the chain is fully split, then every call except for the last will
// be on its own line. If the chain is split after properties, then
// every non-property call except the last will be on its own line.
var separate = switch (state) {
_splitAfterProperties => i >= _leadingProperties && i < _calls.length - 1,
State.split => i < _calls.length - 1,
_ => false,
};

writer.format(_calls[i]._call, separate: separate);
}

@override
void forEachChild(void Function(Piece piece) callback) {
callback(_target);
Expand Down