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

When the RHS of => is a function call, prefer to split at the =>. #1523

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion lib/src/front_end/ast_node_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,9 @@ class AstNodeVisitor extends ThrowingAstVisitor<void> with PieceFactory {
nodePiece(node.expression, context: NodeContext.assignment);

pieces.add(AssignPiece(operatorPiece, expression,
canBlockSplitRight: node.expression.canBlockSplit));
canBlockSplitRight: node.expression.canBlockSplit,
avoidBlockSplitRight:
node.expression.blockFormatType == BlockFormat.invocation));
pieces.token(node.semicolon);
}

Expand Down
101 changes: 52 additions & 49 deletions lib/src/piece/assign.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class AssignPiece extends Piece {
static const State _blockSplitLeft = State(1);

/// Allow the right-hand side to block split.
static const State _blockSplitRight = State(2);
static const State _blockSplitRight = State(2, cost: 0);

/// Split at the operator.
static const State _atOperator = State(3);
Expand Down Expand Up @@ -109,13 +109,34 @@ class AssignPiece extends Piece {
/// ];
final bool _canBlockSplitRight;

/// If `true` then prefer to split at the operator instead of block splitting
/// the right side.
///
/// This is `true` for `=>` functions whose body is a function call. This
/// keeps the called function next to its arguments instead having the
/// function name stick to the `=>` while the arguments split. In other words,
/// prefer:
///
/// someMethod() =>
/// someFunction(argument, another);
///
/// Over:
///
/// someMethod() => someFunction(
/// argument,
/// another,
/// );
final bool _avoidBlockSplitRight;

AssignPiece(this._operator, this._right,
{Piece? left,
bool canBlockSplitLeft = false,
bool canBlockSplitRight = false})
bool canBlockSplitRight = false,
bool avoidBlockSplitRight = false})
: _left = left,
_canBlockSplitLeft = canBlockSplitLeft,
_canBlockSplitRight = canBlockSplitRight;
_canBlockSplitRight = canBlockSplitRight,
_avoidBlockSplitRight = avoidBlockSplitRight;

@override
List<State> get additionalStates => [
Expand All @@ -126,6 +147,16 @@ class AssignPiece extends Piece {
_atOperator,
];

@override
int stateCost(State state) {
// Allow block splitting the right side, but increase the cost so that we
// prefer splitting at the operator and not splitting in the right piece if
// possible.
if (state == _blockSplitRight && _avoidBlockSplitRight) return 1;

return super.stateCost(state);
}

@override
void applyConstraints(State state, Constrain constrain) {
// Force the left side to block split when in that state.
Expand All @@ -140,63 +171,35 @@ class AssignPiece extends Piece {
}

@override
bool allowNewlineInChild(State state, Piece child) {
if (state == State.unsplit) {
if (child == _left) return false;

// Always allow block-splitting the right side if it supports it.
if (child == _right) return _canBlockSplitRight;
}

return true;
}
bool allowNewlineInChild(State state, Piece child) => state != State.unsplit;

@override
void format(CodeWriter writer, State state) {
switch (state) {
case State.unsplit:
_writeLeft(writer, allowNewlines: false);
_writeOperator(writer);
// Always allow block-splitting the right side if it supports it.
_writeRight(writer, allowNewlines: _canBlockSplitRight);

case _atOperator:
// When splitting at the operator, both operands may split or not and
// will be indented if they do.
writer.pushIndent(Indent.expression);
_writeLeft(writer);
_writeOperator(writer, split: state == _atOperator);
_writeRight(writer);
writer.popIndent();

case _blockSplitLeft:
_writeLeft(writer);
_writeOperator(writer);
_writeRight(writer, indent: !_canBlockSplitRight);

case _blockSplitRight:
_writeLeft(writer);
_writeOperator(writer, split: state == _atOperator);
_writeRight(writer);
}
}
// When splitting at the operator, both operands may split or not and
// will be indented if they do.
if (state == _atOperator) writer.pushIndent(Indent.expression);

void _writeLeft(CodeWriter writer, {bool allowNewlines = true}) {
if (_left case var left?) writer.format(left);
}

void _writeOperator(CodeWriter writer, {bool split = false}) {
writer.pushIndent(Indent.expression);
writer.format(_operator);
writer.popIndent();
writer.splitIf(split);
}
writer.splitIf(state == _atOperator);

void _writeRight(CodeWriter writer,
{bool indent = false, bool allowNewlines = true}) {
if (indent) writer.pushIndent(Indent.expression);
// If the left side block splits and the right doesn't, then indent the
// right side if it splits as in:
//
// var [
// a,
// b,
// ] = long +
// expression;
var indentRight = state == _blockSplitLeft && !_canBlockSplitRight;
if (indentRight) writer.pushIndent(Indent.expression);
writer.format(_right);
if (indent) writer.popIndent();
if (indentRight) writer.popIndent();

if (state == _atOperator) writer.popIndent();
}

@override
Expand Down
11 changes: 6 additions & 5 deletions test/tall/function/default_value.unit
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ f([
f([callback(SomeType parameter) = const CallableClass(someLongConstantArgument, anotherConstantArgument)]) {}
<<<
f([
callback(SomeType parameter) =
const CallableClass(
someLongConstantArgument,
anotherConstantArgument,
),
callback(
SomeType parameter,
) = const CallableClass(
someLongConstantArgument,
anotherConstantArgument,
),
munificent marked this conversation as resolved.
Show resolved Hide resolved
]) {}
8 changes: 3 additions & 5 deletions test/tall/function/expression_body.unit
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,11 @@ function() => (
longElement,
longElement,
);
>>> Prefer block-like splitting for function calls.
>>> Avoid block-like splitting for function calls.
function() => another(argument, argument);
<<<
function() => another(
argument,
argument,
);
function() =>
another(argument, argument);
>>> Use block-like splitting for switch expressions.
function() => switch (value) { 1 => 'one', 2 => 'two' };
<<<
Expand Down
6 changes: 2 additions & 4 deletions test/tall/regression/0100/0108.unit
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,8 @@ class ElementBinder {
eventName,
(_) => zone.run(
() => bindAssignableProps.forEach(
(propAndExp) => propAndExp[1].assign(
scope.context,
jsNode[propAndExp[0]],
),
(propAndExp) =>
propAndExp[1].assign(scope.context, jsNode[propAndExp[0]]),
),
),
),
Expand Down
8 changes: 4 additions & 4 deletions test/tall/regression/0200/0217.stmt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Annotation _getAnnotation(AnnotatedNode node, String name) => nodeMetadata
.firstWhere((annotation) => _isAnnotationType(annotation, name),
orElse: () => null);
<<<
Annotation _getAnnotation(AnnotatedNode node, String name) => nodeMetadata
.firstWhere(
Annotation _getAnnotation(AnnotatedNode node, String name) =>
nodeMetadata.firstWhere(
(annotation) => _isAnnotationType(annotation, name),
orElse: () => null,
);
Expand All @@ -13,8 +13,8 @@ Annotation _getAnnotation(AnnotatedNode node, String name) => nodeMetadata
.firstWhere((annotation) => _isAnnotationType(annotation, name),
orElse: () => null);
<<<
Annotation _getAnnotation(AnnotatedNode node, String name) => nodeMetadata
.firstWhere(
Annotation _getAnnotation(AnnotatedNode node, String name) =>
nodeMetadata.firstWhere(
(annotation) => _isAnnotationType(annotation, name),
orElse: () => null,
);
Loading