| 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.rule; | 5 library dart_style.src.rule.rule; |
| 6 | 6 |
| 7 import '../chunk.dart'; | 7 import '../chunk.dart'; |
| 8 import '../fast_hash.dart'; | 8 import '../fast_hash.dart'; |
| 9 | 9 |
| 10 /// A constraint that determines the different ways a related set of chunks may | 10 /// A constraint that determines the different ways a related set of chunks may |
| 11 /// be split. | 11 /// be split. |
| 12 abstract class Rule extends FastHash { | 12 abstract class Rule extends FastHash { |
| 13 /// The number of different states this rule can be in. | 13 /// The number of different states this rule can be in. |
| 14 /// | 14 /// |
| 15 /// Each state determines which set of chunks using this rule are split and | 15 /// Each state determines which set of chunks using this rule are split and |
| 16 /// which aren't. Values range from zero to one minus this. Value zero | 16 /// which aren't. Values range from zero to one minus this. Value zero |
| 17 /// always means "no chunks are split" and increasing values by convention | 17 /// always means "no chunks are split" and increasing values by convention |
| 18 /// mean increasingly undesirable splits. | 18 /// mean increasingly undesirable splits. |
| 19 int get numValues; | 19 int get numValues; |
| 20 | 20 |
| 21 /// The rule value that forces this rule into its maximally split state. | 21 /// The rule value that forces this rule into its maximally split state. |
| 22 /// | 22 /// |
| 23 /// By convention, this is the highest of the range of allowed values. | 23 /// By convention, this is the highest of the range of allowed values. |
| 24 int get fullySplitValue => numValues - 1; | 24 int get fullySplitValue => numValues - 1; |
| 25 | 25 |
| 26 int get cost => Cost.normal; | 26 int get cost => Cost.normal; |
| 27 | 27 |
| 28 /// The span of [Chunk]s that were written while this rule was still in | 28 /// During line splitting [LineSplitter] sets this to the index of this |
| 29 /// effect. | 29 /// rule in its list of rules. |
| 30 /// | 30 int index; |
| 31 /// This is used to tell which rules should be pre-emptively split if their | |
| 32 /// contents are too long. This may be a wider range than the set of chunks | |
| 33 /// enclosed by chunks whose rule is this one. A rule may still be on the | |
| 34 /// list of open rules for a while after its last chunk is written. | |
| 35 // TODO(rnystrom): These are only used by preemption which is kind of hacky. | |
| 36 // Remove this if preemption is redone. | |
| 37 int start; | |
| 38 int end; | |
| 39 | 31 |
| 40 /// The other [Rule]s that "surround" this one (and care about that fact). | 32 /// The other [Rule]s that "surround" this one (and care about that fact). |
| 41 /// | 33 /// |
| 42 /// In many cases, if a split occurs inside an expression, surrounding rules | 34 /// In many cases, if a split occurs inside an expression, surrounding rules |
| 43 /// also want to split too. For example, a split in the middle of an argument | 35 /// also want to split too. For example, a split in the middle of an argument |
| 44 /// forces the entire argument list to also split. | 36 /// forces the entire argument list to also split. |
| 45 /// | 37 /// |
| 46 /// This tracks those relationships. If this rule splits, (sets its value to | 38 /// This tracks those relationships. If this rule splits, (sets its value to |
| 47 /// [fullySplitValue]) then all of the outer rules will also be set to their | 39 /// [fullySplitValue]) then all of the outer rules will also be set to their |
| 48 /// fully split value. | 40 /// fully split value. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 79 /// null to not constrain other. | 71 /// null to not constrain other. |
| 80 int constrain(int value, Rule other) { | 72 int constrain(int value, Rule other) { |
| 81 // By default, any implied rule will be fully split if this one is fully | 73 // By default, any implied rule will be fully split if this one is fully |
| 82 // split. | 74 // split. |
| 83 if (value == 0) return null; | 75 if (value == 0) return null; |
| 84 if (_outerRules.contains(other)) return other.fullySplitValue; | 76 if (_outerRules.contains(other)) return other.fullySplitValue; |
| 85 | 77 |
| 86 return null; | 78 return null; |
| 87 } | 79 } |
| 88 | 80 |
| 89 /// Like [constrain], but in the other direction. | |
| 90 /// | |
| 91 /// If [other] has [otherValue], returns the constrained value this rule may | |
| 92 /// have, or `null` if any value is allowed. | |
| 93 int reverseConstrain(int otherValue, Rule other) { | |
| 94 // If [other] did not fully split, then we can't split either if us | |
| 95 // splitting implies that it should have. | |
| 96 if (otherValue == other.fullySplitValue) return null; | |
| 97 if (_outerRules.contains(other)) return 0; | |
| 98 | |
| 99 return null; | |
| 100 } | |
| 101 | |
| 102 String toString() => "$id"; | 81 String toString() => "$id"; |
| 103 } | 82 } |
| 104 | 83 |
| 105 /// A rule that always splits a chunk. | 84 /// A rule that always splits a chunk. |
| 106 class HardSplitRule extends Rule { | 85 class HardSplitRule extends Rule { |
| 107 int get numValues => 1; | 86 int get numValues => 1; |
| 108 | 87 |
| 109 /// It's always going to be applied, so there's no point in penalizing it. | 88 /// It's always going to be applied, so there's no point in penalizing it. |
| 110 /// | 89 /// |
| 111 /// Also, this avoids doubled counting in literal blocks where there is both | 90 /// Also, this avoids doubled counting in literal blocks where there is both |
| (...skipping 20 matching lines...) Expand all Loading... |
| 132 | 111 |
| 133 SimpleRule({int cost, bool splitsOnInnerRules}) | 112 SimpleRule({int cost, bool splitsOnInnerRules}) |
| 134 : cost = cost != null ? cost : Cost.normal, | 113 : cost = cost != null ? cost : Cost.normal, |
| 135 splitsOnInnerRules = | 114 splitsOnInnerRules = |
| 136 splitsOnInnerRules != null ? splitsOnInnerRules : true; | 115 splitsOnInnerRules != null ? splitsOnInnerRules : true; |
| 137 | 116 |
| 138 bool isSplit(int value, Chunk chunk) => value == 1; | 117 bool isSplit(int value, Chunk chunk) => value == 1; |
| 139 | 118 |
| 140 String toString() => "Simple${super.toString()}"; | 119 String toString() => "Simple${super.toString()}"; |
| 141 } | 120 } |
| OLD | NEW |