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. |
11 abstract class ArgumentRule extends Rule { | 11 abstract class ArgumentRule extends Rule { |
12 /// The rule used to split collections in the argument list, if any. | 12 /// The rule used to split collections in the argument list, if any. |
13 final Rule _collectionRule; | 13 Rule _collectionRule; |
14 | 14 |
15 /// If true, then inner rules that are written will force this rule to split. | 15 /// If true, then inner rules that are written will force this rule to split. |
16 /// | 16 /// |
17 /// Temporarily disabled while writing collectio arguments so that they can be | 17 /// Temporarily disabled while writing collectio arguments so that they can be |
18 /// multi-line without forcing the whole argument list to split. | 18 /// multi-line without forcing the whole argument list to split. |
19 bool _trackInnerRules = true; | 19 bool _trackInnerRules = true; |
20 | 20 |
21 /// Don't split when an inner collection rule splits. | 21 /// Don't split when an inner collection rule splits. |
22 bool get splitsOnInnerRules => _trackInnerRules; | 22 bool get splitsOnInnerRules => _trackInnerRules; |
23 | 23 |
24 /// Creates a new rule for a positional argument list. | 24 /// Creates a new rule for a positional argument list. |
25 /// | 25 /// |
26 /// If [_collectionRule] is given, it is the rule used to split the | 26 /// If [_collectionRule] is given, it is the rule used to split the |
27 /// collections in the list. | 27 /// collections in the list. |
28 ArgumentRule(this._collectionRule); | 28 ArgumentRule(this._collectionRule); |
29 | 29 |
30 Iterable<Rule> get constrainedRules { | 30 void addConstrainedRules(Set<Rule> rules) { |
31 var rules = super.constrainedRules; | 31 if (_collectionRule != null) rules.add(_collectionRule); |
32 if (_collectionRule != null) { | 32 } |
33 rules = rules.toList()..add(_collectionRule); | 33 |
| 34 void forgetUnusedRules() { |
| 35 super.forgetUnusedRules(); |
| 36 if (_collectionRule != null && _collectionRule.index == null) { |
| 37 _collectionRule = null; |
34 } | 38 } |
35 | |
36 return rules; | |
37 } | 39 } |
38 | 40 |
39 /// Called before a collection argument is written. | 41 /// Called before a collection argument is written. |
40 /// | 42 /// |
41 /// Disables tracking inner rules while a collection argument is written. | 43 /// Disables tracking inner rules while a collection argument is written. |
42 void beforeCollection() { | 44 void beforeCollection() { |
43 assert(_trackInnerRules == true); | 45 assert(_trackInnerRules == true); |
44 _trackInnerRules = false; | 46 _trackInnerRules = false; |
45 } | 47 } |
46 | 48 |
(...skipping 14 matching lines...) Expand all Loading... |
61 /// If there are named arguments following these positional ones, this will | 63 /// If there are named arguments following these positional ones, this will |
62 /// be their rule. | 64 /// be their rule. |
63 Rule _namedArgsRule; | 65 Rule _namedArgsRule; |
64 | 66 |
65 /// Creates a new rule for a positional argument list. | 67 /// Creates a new rule for a positional argument list. |
66 /// | 68 /// |
67 /// If [collectionRule] is given, it is the rule used to split the collection | 69 /// If [collectionRule] is given, it is the rule used to split the collection |
68 /// arguments in the list. | 70 /// arguments in the list. |
69 PositionalRule(Rule collectionRule) : super(collectionRule); | 71 PositionalRule(Rule collectionRule) : super(collectionRule); |
70 | 72 |
71 Iterable<Rule> get constrainedRules { | 73 void addConstrainedRules(Set<Rule> rules) { |
72 var rules = super.constrainedRules; | 74 if (_namedArgsRule != null) rules.add(_namedArgsRule); |
73 if (_namedArgsRule != null) { | 75 } |
74 rules = rules.toList()..add(_namedArgsRule); | 76 |
| 77 void forgetUnusedRules() { |
| 78 super.forgetUnusedRules(); |
| 79 if (_namedArgsRule != null && _namedArgsRule.index == null) { |
| 80 _namedArgsRule = null; |
75 } | 81 } |
76 | |
77 return rules; | |
78 } | 82 } |
79 | 83 |
80 /// Remembers [chunk] as containing the split that occurs right before an | 84 /// Remembers [chunk] as containing the split that occurs right before an |
81 /// argument in the list. | 85 /// argument in the list. |
82 void beforeArgument(Chunk chunk) { | 86 void beforeArgument(Chunk chunk) { |
83 _arguments.add(chunk); | 87 _arguments.add(chunk); |
84 } | 88 } |
85 | 89 |
86 /// Remembers that [rule] is the [NamedArgsRule] immediately following this | 90 /// Remembers that [rule] is the [NamedArgsRule] immediately following this |
87 /// positional argument list. | 91 /// positional argument list. |
(...skipping 27 matching lines...) Expand all Loading... |
115 | 119 |
116 /// Split rule for a call with a single positional argument (which may or may | 120 /// Split rule for a call with a single positional argument (which may or may |
117 /// not be a collection argument.) | 121 /// not be a collection argument.) |
118 class SinglePositionalRule extends PositionalRule { | 122 class SinglePositionalRule extends PositionalRule { |
119 int get numValues => 2; | 123 int get numValues => 2; |
120 | 124 |
121 /// If there is only a single non-collection argument, allow it to split | 125 /// If there is only a single non-collection argument, allow it to split |
122 /// internally without forcing a split before the argument. | 126 /// internally without forcing a split before the argument. |
123 final bool splitsOnInnerRules; | 127 final bool splitsOnInnerRules; |
124 | 128 |
125 bool hack = false; | |
126 | |
127 /// Creates a new rule for a positional argument list. | 129 /// Creates a new rule for a positional argument list. |
128 /// | 130 /// |
129 /// If [collectionRule] is given, it is the rule used to split the | 131 /// If [collectionRule] is given, it is the rule used to split the |
130 /// collections in the list. If [splitsOnInnerRules] is `true`, then we will | 132 /// collections in the list. If [splitsOnInnerRules] is `true`, then we will |
131 /// split before the argument if the argument itself contains a split. | 133 /// split before the argument if the argument itself contains a split. |
132 SinglePositionalRule(Rule collectionRule, {bool splitsOnInnerRules}) | 134 SinglePositionalRule(Rule collectionRule, {bool splitsOnInnerRules}) |
133 : super(collectionRule), | 135 : super(collectionRule), |
134 splitsOnInnerRules = | 136 splitsOnInnerRules = |
135 splitsOnInnerRules != null ? splitsOnInnerRules : false; | 137 splitsOnInnerRules != null ? splitsOnInnerRules : false; |
136 | 138 |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 // If we aren't splitting any args, we can split the collection. | 321 // If we aren't splitting any args, we can split the collection. |
320 if (value == 0) return null; | 322 if (value == 0) return null; |
321 | 323 |
322 // Split before all of the arguments, even the collections, so don't let | 324 // Split before all of the arguments, even the collections, so don't let |
323 // them split. | 325 // them split. |
324 return 0; | 326 return 0; |
325 } | 327 } |
326 | 328 |
327 String toString() => "Named${super.toString()}"; | 329 String toString() => "Named${super.toString()}"; |
328 } | 330 } |
OLD | NEW |