Skip to content

Commit 94d6bd8

Browse files
Format records and record type annotations. (#1158)
* Format records and record type annotations. In the process of working on this, I discovered that multi-line function types with trailing commas are pretty busted looking. (I guess no one puts trailing commas there.) I fixed those too, since they are structurally similar to record types in many ways. * Update lib/src/source_visitor.dart Co-authored-by: Nate Bosch <[email protected]> Co-authored-by: Nate Bosch <[email protected]>
1 parent 4506ac5 commit 94d6bd8

15 files changed

+823
-32
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# 2.2.5-dev
22

3+
* Format record expressions and record type annotations.
4+
* Better indentation of multiline function types inside type argument lists.
35
* Require `package:analyzer` `^5.1.0`.
46
* Format unnamed libraries.
57
* Require Dart 2.18.

lib/src/argument_list_visitor.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,13 +453,18 @@ class ArgumentSublist {
453453

454454
// Tell it to use the rule we've already created.
455455
visitor.beforeBlock(argumentBlock, blockRule, previousSplit);
456-
} else if (_allArguments.length > 1) {
456+
} else if (_allArguments.length > 1 ||
457+
_allArguments.first is RecordLiteral) {
457458
// Edge case: Only bump the nesting if there are multiple arguments. This
458459
// lets us avoid spurious indentation in cases like:
459460
//
460461
// function(function(() {
461462
// body;
462463
// }));
464+
//
465+
// Do bump the nesting if the single argument is a record because records
466+
// are formatted like regular values when they appear in argument lists
467+
// even though they internally get block-like formatting.
463468
visitor.builder.startBlockArgumentNesting();
464469
} else if (argument is! NamedExpression) {
465470
// Edge case: Likewise, don't force the argument to split if there is
@@ -478,7 +483,8 @@ class ArgumentSublist {
478483

479484
if (argumentBlock != null) {
480485
rule.enableSplitOnInnerRules();
481-
} else if (_allArguments.length > 1) {
486+
} else if (_allArguments.length > 1 ||
487+
_allArguments.first is RecordLiteral) {
482488
visitor.builder.endBlockArgumentNesting();
483489
} else if (argument is! NamedExpression) {
484490
rule.enableSplitOnInnerRules();

lib/src/ast_extensions.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ extension ExpressionExtensions on Expression {
100100
/// ]..addAll(numbers);
101101
bool get isCollectionLike {
102102
var expression = this;
103-
if (expression is ListLiteral) return false;
104-
if (expression is SetOrMapLiteral) return false;
103+
if (expression is ListLiteral) return true;
104+
if (expression is SetOrMapLiteral) return true;
105105

106106
// If the target is a call with a trailing comma in the argument list,
107107
// treat it like a collection literal.
@@ -119,7 +119,7 @@ extension ExpressionExtensions on Expression {
119119
// element
120120
// ])..cascade();
121121

122-
return arguments == null || !arguments.arguments.hasCommaAfter;
122+
return arguments != null && arguments.arguments.hasCommaAfter;
123123
}
124124

125125
/// Whether this is an argument in an argument list with a trailing comma.

lib/src/dart_formatter.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class DartFormatter {
9090
var featureSet = FeatureSet.fromEnableFlags2(
9191
sdkLanguageVersion: Version(2, 19, 0),
9292
flags: [
93+
'records',
9394
'unnamed-libraries',
9495
],
9596
);

lib/src/line_writer.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,11 @@ class LineWriter {
180180
void _writeChunksUnsplit(BlockChunk block) {
181181
for (var chunk in block.children) {
182182
if (chunk.spaceWhenUnsplit) _buffer.write(' ');
183-
_writeChunk(chunk);
184183

185184
// Recurse into the block.
186185
if (chunk is BlockChunk) _writeChunksUnsplit(chunk);
186+
187+
_writeChunk(chunk);
187188
}
188189
}
189190

0 commit comments

Comments
 (0)