OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library dart_style.src.rule.argument; | 5 library dart_style.src.rule.argument; |
6 | 6 |
7 import '../chunk.dart'; | 7 import '../chunk.dart'; |
8 import 'rule.dart'; | 8 import 'rule.dart'; |
9 | 9 |
10 /// Base class for a rule that handles argument or parameter lists. | 10 /// Base class for a rule that handles argument or parameter lists. |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 /// Creates a new rule for a positional argument list. | 117 /// Creates a new rule for a positional argument list. |
118 /// | 118 /// |
119 /// If [collectionRule] is given, it is the rule used to split the | 119 /// If [collectionRule] is given, it is the rule used to split the |
120 /// collections in the list. If [splitsOnInnerRules] is `true`, then we will | 120 /// collections in the list. If [splitsOnInnerRules] is `true`, then we will |
121 /// split before the argument if the argument itself contains a split. | 121 /// split before the argument if the argument itself contains a split. |
122 SinglePositionalRule(Rule collectionRule, {bool splitsOnInnerRules}) | 122 SinglePositionalRule(Rule collectionRule, {bool splitsOnInnerRules}) |
123 : super(collectionRule), | 123 : super(collectionRule), |
124 splitsOnInnerRules = | 124 splitsOnInnerRules = |
125 splitsOnInnerRules != null ? splitsOnInnerRules : false; | 125 splitsOnInnerRules != null ? splitsOnInnerRules : false; |
126 | 126 |
127 bool isSplit(int value, Chunk chunk) => value != Rule.unsplit; | |
128 | |
129 int constrain(int value, Rule other) { | 127 int constrain(int value, Rule other) { |
130 var constrained = super.constrain(value, other); | 128 var constrained = super.constrain(value, other); |
131 if (constrained != null) return constrained; | 129 if (constrained != null) return constrained; |
132 | 130 |
133 if (other != _collectionRule) return null; | 131 if (other != _collectionRule) return null; |
134 | 132 |
135 // If we aren't splitting any args, we can split the collection. | 133 // If we aren't splitting any args, we can split the collection. |
136 if (value == Rule.unsplit) return null; | 134 if (value == Rule.unsplit) return null; |
137 | 135 |
138 // We are splitting before a collection, so don't let it split internally. | 136 // We are splitting before a collection, so don't let it split internally. |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 | 185 |
188 return result; | 186 return result; |
189 } | 187 } |
190 | 188 |
191 MultiplePositionalRule( | 189 MultiplePositionalRule( |
192 Rule collectionRule, this._leadingCollections, this._trailingCollections) | 190 Rule collectionRule, this._leadingCollections, this._trailingCollections) |
193 : super(collectionRule); | 191 : super(collectionRule); |
194 | 192 |
195 String toString() => "*Pos${super.toString()}"; | 193 String toString() => "*Pos${super.toString()}"; |
196 | 194 |
197 bool isSplit(int value, Chunk chunk) { | 195 bool isSplitAtValue(int value, Chunk chunk) { |
198 // Don't split at all. | |
199 if (value == Rule.unsplit) return false; | |
200 | |
201 // Split only before the first argument. Keep the entire argument list | 196 // Split only before the first argument. Keep the entire argument list |
202 // together on the next line. | 197 // together on the next line. |
203 if (value == 1) return chunk == _arguments.first; | 198 if (value == 1) return chunk == _arguments.first; |
204 | 199 |
205 // Split before a single argument. Try later arguments before earlier ones | 200 // Split before a single argument. Try later arguments before earlier ones |
206 // to try to keep as much on the first line as possible. | 201 // to try to keep as much on the first line as possible. |
207 if (value <= _arguments.length) { | 202 if (value <= _arguments.length) { |
208 var argument = _arguments.length - value + 1; | 203 var argument = _arguments.length - value + 1; |
209 return chunk == _arguments[argument]; | 204 return chunk == _arguments[argument]; |
210 } | 205 } |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 int get numValues => 3; | 286 int get numValues => 3; |
292 | 287 |
293 NamedRule({bool containsCollections}) | 288 NamedRule({bool containsCollections}) |
294 : _containsCollections = containsCollections ?? false; | 289 : _containsCollections = containsCollections ?? false; |
295 | 290 |
296 void beforeArguments(Chunk chunk) { | 291 void beforeArguments(Chunk chunk) { |
297 assert(_first == null); | 292 assert(_first == null); |
298 _first = chunk; | 293 _first = chunk; |
299 } | 294 } |
300 | 295 |
301 bool isSplit(int value, Chunk chunk) { | 296 bool isSplitAtValue(int value, Chunk chunk) { |
302 switch (value) { | 297 // Only split before the first argument. |
303 case Rule.unsplit: | 298 if (value == 1) return chunk == _first; |
304 return false; | |
305 case 1: | |
306 return chunk == _first; | |
307 case 2: | |
308 return true; | |
309 } | |
310 | 299 |
311 throw "unreachable"; | 300 // Split before every argument. |
| 301 return true; |
312 } | 302 } |
313 | 303 |
314 String toString() => "Named${super.toString()}"; | 304 String toString() => "Named${super.toString()}"; |
315 } | 305 } |
OLD | NEW |