@@ -9,9 +9,6 @@ abstract class ArgumentRule extends Rule {
9
9
/// The chunks prior to each positional argument.
10
10
final List <Chunk ?> _arguments = [];
11
11
12
- /// The rule used to split collections in the argument list, if any.
13
- Rule ? _collectionRule;
14
-
15
12
/// The number of leading collection arguments.
16
13
///
17
14
/// This and [_trailingCollections] cannot both be positive. If every
@@ -34,20 +31,7 @@ abstract class ArgumentRule extends Rule {
34
31
@override
35
32
bool get splitsOnInnerRules => _trackInnerRules;
36
33
37
- ArgumentRule (this ._collectionRule, this ._leadingCollections,
38
- this ._trailingCollections);
39
-
40
- @override
41
- void addConstrainedRules (Set <Rule > rules) {
42
- super .addConstrainedRules (rules);
43
- if (_collectionRule != null ) rules.add (_collectionRule! );
44
- }
45
-
46
- @override
47
- void forgetUnusedRules () {
48
- super .forgetUnusedRules ();
49
- if (_collectionRule? .index == null ) _collectionRule = null ;
50
- }
34
+ ArgumentRule ._(this ._leadingCollections, this ._trailingCollections);
51
35
52
36
/// Remembers [chunk] as containing the split that occurs right before an
53
37
/// argument in the list.
@@ -86,17 +70,47 @@ abstract class ArgumentRule extends Rule {
86
70
/// splits before all of the non-collection arguments, but does not split
87
71
/// before the collections, so that they can split internally.
88
72
class PositionalRule extends ArgumentRule {
89
- /// If there are named arguments following these positional ones, this will
90
- /// be their rule.
91
- Rule ? _namedArgsRule;
92
-
93
73
/// Creates a new rule for a positional argument list.
94
74
///
95
- /// If [_collectionRule] is given, it is the rule used to split the collection
96
- /// arguments in the list.
97
- PositionalRule (
98
- Rule ? collectionRule, int leadingCollections, int trailingCollections)
99
- : super (collectionRule, leadingCollections, trailingCollections);
75
+ /// [argumentCount] is the number of arguments that will be added to the rule
76
+ /// by later calls to [beforeArgument()] .
77
+ ///
78
+ /// If [collectionRule] is given, it is the rule used to split the collection
79
+ /// arguments in the list. It must be provided if [leadingCollections] or
80
+ /// [trailingCollections] is non-zero.
81
+ PositionalRule (Rule ? collectionRule,
82
+ {required int argumentCount,
83
+ int leadingCollections = 0 ,
84
+ int trailingCollections = 0 })
85
+ : super ._(leadingCollections, trailingCollections) {
86
+ if (collectionRule != null ) {
87
+ // Don't split inside collections if there are leading collections and
88
+ // we split before the first argument.
89
+ if (leadingCollections > 0 ) {
90
+ addConstraint (1 , collectionRule, Rule .unsplit);
91
+ }
92
+
93
+ // If we're only splitting before the non-collection arguments, the
94
+ // intent is to split inside the collections, so force that here.
95
+ if (leadingCollections > 0 || trailingCollections > 0 ) {
96
+ addConstraint (argumentCount + 1 , collectionRule, 1 );
97
+ }
98
+
99
+ // Split before a single argument. If it's in the middle of the collection
100
+ // arguments, don't allow them to split.
101
+ for (var argument = 0 ; argument < leadingCollections; argument++ ) {
102
+ var value = argumentCount - argument + 1 ;
103
+ addConstraint (value, collectionRule, Rule .unsplit);
104
+ }
105
+
106
+ for (var argument = argumentCount - trailingCollections;
107
+ argument < argumentCount;
108
+ argument++ ) {
109
+ var value = argumentCount - argument + 1 ;
110
+ addConstraint (value, collectionRule, Rule .unsplit);
111
+ }
112
+ }
113
+ }
100
114
101
115
@override
102
116
int get numValues {
@@ -118,18 +132,6 @@ class PositionalRule extends ArgumentRule {
118
132
return result;
119
133
}
120
134
121
- @override
122
- void addConstrainedRules (Set <Rule > rules) {
123
- super .addConstrainedRules (rules);
124
- if (_namedArgsRule != null ) rules.add (_namedArgsRule! );
125
- }
126
-
127
- @override
128
- void forgetUnusedRules () {
129
- super .forgetUnusedRules ();
130
- if (_namedArgsRule? .index == null ) _namedArgsRule = null ;
131
- }
132
-
133
135
@override
134
136
bool isSplitAtValue (int value, Chunk chunk) {
135
137
// Split only before the first argument. Keep the entire argument list
@@ -162,78 +164,19 @@ class PositionalRule extends ArgumentRule {
162
164
return true ;
163
165
}
164
166
165
- /// Remembers that [rule] is the [Rule] immediately following this positional
166
- /// positional argument list.
167
+ /// Builds any constraints from this positional argument rule onto the [rule]
168
+ /// used for the subsequent named arguments in the same argument list.
167
169
///
168
- /// This is normally a [NamedRule] but [PositionalRule] is also used for the
169
- /// property accesses at the beginning of a call chain, in which case this
170
+ /// The [rule] is normally a [NamedRule] but [PositionalRule] is also used for
171
+ /// the property accesses at the beginning of a call chain, in which case this
170
172
/// is just a [SimpleRule] .
171
- void setNamedArgsRule (Rule rule) {
172
- _namedArgsRule = rule;
173
- }
174
-
175
- /// Constrains the named argument list to at least move to the next line if
176
- /// there are any splits in the positional arguments. Prevents things like:
177
- ///
178
- /// function(
179
- /// argument,
180
- /// argument, named: argument);
181
- @override
182
- int ? constrain (int value, Rule other) {
183
- var constrained = super .constrain (value, other);
184
- if (constrained != null ) return constrained;
173
+ void addNamedArgsConstraints (Rule rule) {
174
+ // If the positional args are one-per-line, the named args are too.
175
+ constrainWhenFullySplit (rule);
185
176
186
- // Handle the relationship between the positional and named args.
187
- if (other == _namedArgsRule) {
188
- // If the positional args are one-per-line, the named args are too.
189
- if (value == fullySplitValue) return _namedArgsRule! .fullySplitValue;
190
-
191
- // Otherwise, if there is any split in the positional arguments, don't
192
- // allow the named arguments on the same line as them.
193
- if (value != 0 ) return - 1 ;
194
- }
195
-
196
- // Decide how to constrain the collection rule.
197
- if (other != _collectionRule) return null ;
198
-
199
- // If all of the collections are in the named arguments, [_collectionRule]
200
- // will not be null, but we don't have to handle it.
201
- if (_leadingCollections == 0 && _trailingCollections == 0 ) return null ;
202
-
203
- // If we aren't splitting any args, we can split the collection.
204
- if (value == Rule .unsplit) return null ;
205
-
206
- // Split only before the first argument.
207
- if (value == 1 ) {
208
- if (_leadingCollections > 0 ) {
209
- // We are splitting before a collection, so don't let it split
210
- // internally.
211
- return Rule .unsplit;
212
- } else {
213
- // The split is outside of the collections so they can split or not.
214
- return null ;
215
- }
216
- }
217
-
218
- // Split before a single argument. If it's in the middle of the collection
219
- // arguments, don't allow them to split.
220
- if (value <= _arguments.length) {
221
- var argument = _arguments.length - value + 1 ;
222
- if (argument < _leadingCollections ||
223
- argument >= _arguments.length - _trailingCollections) {
224
- return Rule .unsplit;
225
- }
226
-
227
- return null ;
228
- }
229
-
230
- // Only split before the non-collection arguments. This case only comes into
231
- // play when we do want to split the collection, so force that here.
232
- if (value == _arguments.length + 1 ) return 1 ;
233
-
234
- // Split before all of the arguments, even the collections. We'll allow
235
- // them to split but indent their bodies if they do.
236
- return null ;
177
+ // Otherwise, if there is any split in the positional arguments, don't
178
+ // allow the named arguments on the same line as them.
179
+ addRangeConstraint (1 , fullySplitValue, rule, Rule .mustSplit);
237
180
}
238
181
239
182
@override
@@ -249,9 +192,23 @@ class NamedRule extends ArgumentRule {
249
192
@override
250
193
int get numValues => 3 ;
251
194
195
+ /// Creates a new rule for a named argument list.
196
+ ///
197
+ /// [argumentCount] is the number of arguments that will be added to the rule
198
+ /// by later calls to [beforeArgument()] .
199
+ ///
200
+ /// If [collectionRule] is given, it is the rule used to split the collection
201
+ /// arguments in the list. It must be provided if [leadingCollections] or
202
+ /// [trailingCollections] is non-zero.
252
203
NamedRule (
253
204
Rule ? collectionRule, int leadingCollections, int trailingCollections)
254
- : super (collectionRule, leadingCollections, trailingCollections);
205
+ : super ._(leadingCollections, trailingCollections) {
206
+ if (leadingCollections > 0 || trailingCollections > 0 ) {
207
+ // Split only before the first argument. Don't allow the collections to
208
+ // split.
209
+ addConstraint (1 , collectionRule! , Rule .unsplit);
210
+ }
211
+ }
255
212
256
213
@override
257
214
bool isSplitAtValue (int value, Chunk chunk) {
@@ -262,30 +219,6 @@ class NamedRule extends ArgumentRule {
262
219
return true ;
263
220
}
264
221
265
- @override
266
- int ? constrain (int value, Rule other) {
267
- var constrained = super .constrain (value, other);
268
- if (constrained != null ) return constrained;
269
-
270
- // Decide how to constrain the collection rule.
271
- if (other != _collectionRule) return null ;
272
-
273
- // If all of the collections are in the named arguments, [_collectionRule]
274
- // will not be null, but we don't have to handle it.
275
- if (_leadingCollections == 0 && _trailingCollections == 0 ) return null ;
276
-
277
- // If we aren't splitting any args, we can split the collection.
278
- if (value == Rule .unsplit) return null ;
279
-
280
- // Split only before the first argument. Don't allow the collections to
281
- // split.
282
- if (value == 1 ) return Rule .unsplit;
283
-
284
- // Split before all of the arguments, even the collections. We'll allow
285
- // them to split but indent their bodies if they do.
286
- return null ;
287
- }
288
-
289
222
@override
290
223
String toString () => 'Named${super .toString ()}' ;
291
224
}
0 commit comments