@@ -12,7 +12,6 @@ import '../dart_formatter.dart';
12
12
import '../piece/adjacent.dart' ;
13
13
import '../piece/assign.dart' ;
14
14
import '../piece/block.dart' ;
15
- import '../piece/chain.dart' ;
16
15
import '../piece/constructor.dart' ;
17
16
import '../piece/for.dart' ;
18
17
import '../piece/if.dart' ;
@@ -22,6 +21,7 @@ import '../piece/piece.dart';
22
21
import '../piece/variable.dart' ;
23
22
import '../source_code.dart' ;
24
23
import 'adjacent_builder.dart' ;
24
+ import 'chain_builder.dart' ;
25
25
import 'comment_writer.dart' ;
26
26
import 'delimited_list_builder.dart' ;
27
27
import 'piece_factory.dart' ;
@@ -412,18 +412,9 @@ class AstNodeVisitor extends ThrowingAstVisitor<Piece> with PieceFactory {
412
412
413
413
@override
414
414
Piece visitConstructorName (ConstructorName node) {
415
- // If there is an import prefix and/or constructor name, then allow
416
- // splitting before the `.`. This doesn't look good, but is consistent with
417
- // constructor calls that don't have `new` or `const`. We allow splitting
418
- // in the latter because there is no way to distinguish syntactically
419
- // between a named constructor call and any other kind of method call or
420
- // property access.
421
- var operations = < Piece > [];
422
-
423
415
var builder = AdjacentBuilder (this );
424
416
if (node.type.importPrefix case var importPrefix? ) {
425
417
builder.token (importPrefix.name);
426
- operations.add (builder.build ());
427
418
builder.token (importPrefix.period);
428
419
}
429
420
@@ -435,16 +426,13 @@ class AstNodeVisitor extends ThrowingAstVisitor<Piece> with PieceFactory {
435
426
436
427
// If this is a named constructor, the name.
437
428
if (node.name != null ) {
438
- operations.add (builder.build ());
439
429
builder.token (node.period);
440
430
builder.visit (node.name);
441
431
}
442
432
443
433
// If there was a prefix or constructor name, then make a splittable piece.
444
434
// Otherwise, the current piece is a simple identifier for the name.
445
- operations.add (builder.build ());
446
- if (operations.length == 1 ) return operations.first;
447
- return ChainPiece (operations);
435
+ return builder.build ();
448
436
}
449
437
450
438
@override
@@ -1027,35 +1015,19 @@ class AstNodeVisitor extends ThrowingAstVisitor<Piece> with PieceFactory {
1027
1015
1028
1016
@override
1029
1017
Piece visitIndexExpression (IndexExpression node) {
1030
- // TODO(tall): Allow splitting before and/or after the `[` when method
1031
- // chain formatting is fully implemented. For now, we just output the code
1032
- // so that tests of other language features that contain index expressions
1033
- // can run.
1034
- return buildPiece ((b) {
1035
- b.visit (node.target);
1036
- b.token (node.leftBracket);
1037
- b.visit (node.index);
1038
- b.token (node.rightBracket);
1039
- });
1018
+ Piece ? targetPiece;
1019
+ if (node.target case var target? ) targetPiece = nodePiece (target);
1020
+ return createIndexExpression (targetPiece, node);
1040
1021
}
1041
1022
1042
1023
@override
1043
1024
Piece visitInstanceCreationExpression (InstanceCreationExpression node) {
1044
1025
var builder = AdjacentBuilder (this );
1045
1026
builder.token (node.keyword, spaceAfter: true );
1046
1027
1047
- // If there is an import prefix and/or constructor name, then allow
1048
- // splitting before the `.`. This doesn't look good, but is consistent with
1049
- // constructor calls that don't have `new` or `const`. We allow splitting
1050
- // in the latter because there is no way to distinguish syntactically
1051
- // between a named constructor call and any other kind of method call or
1052
- // property access.
1053
- var operations = < Piece > [];
1054
-
1055
1028
var constructor = node.constructorName;
1056
1029
if (constructor.type.importPrefix case var importPrefix? ) {
1057
1030
builder.token (importPrefix.name);
1058
- operations.add (builder.build ());
1059
1031
builder.token (importPrefix.period);
1060
1032
}
1061
1033
@@ -1066,19 +1038,13 @@ class AstNodeVisitor extends ThrowingAstVisitor<Piece> with PieceFactory {
1066
1038
1067
1039
// If this is a named constructor call, the name.
1068
1040
if (constructor.name case var name? ) {
1069
- operations.add (builder.build ());
1070
1041
builder.token (constructor.period);
1071
1042
builder.visit (name);
1072
1043
}
1073
1044
1074
1045
builder.visit (node.argumentList);
1075
- operations.add (builder.build ());
1076
1046
1077
- if (operations.length > 1 ) {
1078
- return ChainPiece (operations);
1079
- } else {
1080
- return operations.first;
1081
- }
1047
+ return builder.build ();
1082
1048
}
1083
1049
1084
1050
@override
@@ -1195,15 +1161,23 @@ class AstNodeVisitor extends ThrowingAstVisitor<Piece> with PieceFactory {
1195
1161
1196
1162
@override
1197
1163
Piece visitMethodInvocation (MethodInvocation node) {
1198
- return buildPiece ((b) {
1199
- // TODO(tall): Support splitting at `.` or `?.`. Right now we just format
1200
- // it inline so that we can use method calls in other tests.
1201
- b.visit (node.target);
1202
- b.token (node.operator );
1203
- b.visit (node.methodName);
1204
- b.visit (node.typeArguments);
1205
- b.visit (node.argumentList);
1206
- });
1164
+ // If there's no target, this is a "bare" function call like "foo(1, 2)",
1165
+ // or a section in a cascade.
1166
+ //
1167
+ // If it looks like a constructor or static call, we want to keep the
1168
+ // target and method together instead of including the method in the
1169
+ // subsequent method chain.
1170
+ if (node.target == null || node.looksLikeStaticCall) {
1171
+ return buildPiece ((b) {
1172
+ b.visit (node.target);
1173
+ b.token (node.operator );
1174
+ b.visit (node.methodName);
1175
+ b.visit (node.typeArguments);
1176
+ b.visit (node.argumentList);
1177
+ });
1178
+ }
1179
+
1180
+ return ChainBuilder (this , node).build ();
1207
1181
}
1208
1182
1209
1183
@override
@@ -1354,14 +1328,7 @@ class AstNodeVisitor extends ThrowingAstVisitor<Piece> with PieceFactory {
1354
1328
1355
1329
@override
1356
1330
Piece visitPrefixedIdentifier (PrefixedIdentifier node) {
1357
- // TODO(tall): Allow splitting before the `.` when method chain formatting
1358
- // is fully implemented. For now, we just output the code so that tests
1359
- // of other language features that contain prefixed identifiers can run.
1360
- return buildPiece ((b) {
1361
- b.visit (node.prefix);
1362
- b.token (node.period);
1363
- b.visit (node.identifier);
1364
- });
1331
+ return ChainBuilder (this , node).build ();
1365
1332
}
1366
1333
1367
1334
@override
@@ -1382,14 +1349,7 @@ class AstNodeVisitor extends ThrowingAstVisitor<Piece> with PieceFactory {
1382
1349
1383
1350
@override
1384
1351
Piece visitPropertyAccess (PropertyAccess node) {
1385
- // TODO(tall): Allow splitting before the `.` when method chain formatting
1386
- // is fully implemented. For now, we just output the code so that tests
1387
- // of other language features that contain property accesses can run.
1388
- return buildPiece ((b) {
1389
- b.visit (node.target);
1390
- b.token (node.operator );
1391
- b.visit (node.propertyName);
1392
- });
1352
+ return ChainBuilder (this , node).build ();
1393
1353
}
1394
1354
1395
1355
@override
0 commit comments