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.line_splitting.solve_state; | 5 library dart_style.src.line_splitting.solve_state; |
6 | 6 |
7 import '../debug.dart' as debug; | 7 import '../debug.dart' as debug; |
8 import '../rule/rule.dart'; | 8 import '../rule/rule.dart'; |
9 import '../whitespace.dart'; | 9 import '../whitespace.dart'; |
10 import 'line_splitter.dart'; | 10 import 'line_splitter.dart'; |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 /// states may diverge by binding unbound rules or not. | 106 /// states may diverge by binding unbound rules or not. |
107 Set<Rule> _boundRulesInUnboundLines; | 107 Set<Rule> _boundRulesInUnboundLines; |
108 | 108 |
109 SolveState(this._splitter, this._ruleValues) { | 109 SolveState(this._splitter, this._ruleValues) { |
110 _calculateSplits(); | 110 _calculateSplits(); |
111 _calculateCost(); | 111 _calculateCost(); |
112 } | 112 } |
113 | 113 |
114 /// Gets the value to use for [rule], either the bound value or | 114 /// Gets the value to use for [rule], either the bound value or |
115 /// [Rule.unsplit] if it isn't bound. | 115 /// [Rule.unsplit] if it isn't bound. |
116 int getValue(Rule rule) { | 116 int getValue(Rule rule) => _ruleValues.getValue(rule); |
117 if (rule is HardSplitRule) return Rule.unsplit; | |
118 | |
119 return _ruleValues.getValue(rule); | |
120 } | |
121 | 117 |
122 /// Returns `true` if this state is a better solution to use as the final | 118 /// Returns `true` if this state is a better solution to use as the final |
123 /// result than [other]. | 119 /// result than [other]. |
124 bool isBetterThan(SolveState other) { | 120 bool isBetterThan(SolveState other) { |
125 // If this state contains an unbound rule that we know can't be left | 121 // If this state contains an unbound rule that we know can't be left |
126 // unsplit, we can't pick this as a solution. | 122 // unsplit, we can't pick this as a solution. |
127 if (!_isComplete) return false; | 123 if (!_isComplete) return false; |
128 | 124 |
129 // Anything is better than nothing. | 125 // Anything is better than nothing. |
130 if (other == null) return true; | 126 if (other == null) return true; |
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
430 | 426 |
431 _splits.setCost(cost); | 427 _splits.setCost(cost); |
432 } | 428 } |
433 | 429 |
434 /// Adds [rule] and all of the rules it constrains to the set of [_liveRules]. | 430 /// Adds [rule] and all of the rules it constrains to the set of [_liveRules]. |
435 /// | 431 /// |
436 /// Only does this if [rule] is a valid soft rule. Returns `true` if any new | 432 /// Only does this if [rule] is a valid soft rule. Returns `true` if any new |
437 /// live rules were added. | 433 /// live rules were added. |
438 bool _addLiveRules(Rule rule) { | 434 bool _addLiveRules(Rule rule) { |
439 if (rule == null) return false; | 435 if (rule == null) return false; |
440 if (rule is HardSplitRule) return false; | |
441 | 436 |
442 var added = false; | 437 var added = false; |
443 for (var constrained in rule.allConstrainedRules) { | 438 for (var constrained in rule.allConstrainedRules) { |
444 if (_ruleValues.contains(constrained)) continue; | 439 if (_ruleValues.contains(constrained)) continue; |
445 | 440 |
446 _liveRules.add(constrained); | 441 _liveRules.add(constrained); |
447 added = true; | 442 added = true; |
448 } | 443 } |
449 | 444 |
450 return added; | 445 return added; |
(...skipping 14 matching lines...) Expand all Loading... |
465 | 460 |
466 for (var i = 0; i < _splitter.chunks.length - 1; i++) { | 461 for (var i = 0; i < _splitter.chunks.length - 1; i++) { |
467 if (splits.shouldSplitAt(i)) { | 462 if (splits.shouldSplitAt(i)) { |
468 if (hasUnbound) _boundRulesInUnboundLines.addAll(boundInLine); | 463 if (hasUnbound) _boundRulesInUnboundLines.addAll(boundInLine); |
469 | 464 |
470 boundInLine.clear(); | 465 boundInLine.clear(); |
471 hasUnbound = false; | 466 hasUnbound = false; |
472 } | 467 } |
473 | 468 |
474 var rule = _splitter.chunks[i].rule; | 469 var rule = _splitter.chunks[i].rule; |
475 if (rule != null && rule is! HardSplitRule) { | 470 if (rule != null) { |
476 if (_ruleValues.contains(rule)) { | 471 if (_ruleValues.contains(rule)) { |
477 boundInLine.add(rule); | 472 boundInLine.add(rule); |
478 } else { | 473 } else { |
479 hasUnbound = true; | 474 hasUnbound = true; |
480 } | 475 } |
481 } | 476 } |
482 } | 477 } |
483 | 478 |
484 if (hasUnbound) _boundRulesInUnboundLines.addAll(boundInLine); | 479 if (hasUnbound) _boundRulesInUnboundLines.addAll(boundInLine); |
485 } | 480 } |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
590 | 585 |
591 buffer.write(" \$${splits.cost}"); | 586 buffer.write(" \$${splits.cost}"); |
592 | 587 |
593 if (overflowChars > 0) buffer.write(" (${overflowChars} over)"); | 588 if (overflowChars > 0) buffer.write(" (${overflowChars} over)"); |
594 if (!_isComplete) buffer.write(" (incomplete)"); | 589 if (!_isComplete) buffer.write(" (incomplete)"); |
595 if (splits == null) buffer.write(" invalid"); | 590 if (splits == null) buffer.write(" invalid"); |
596 | 591 |
597 return buffer.toString(); | 592 return buffer.toString(); |
598 } | 593 } |
599 } | 594 } |
OLD | NEW |