| 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 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 var unsplitRules = _ruleValues.clone(); | 188 var unsplitRules = _ruleValues.clone(); |
| 189 | 189 |
| 190 // Walk down the rules looking for unbound ones to try. | 190 // Walk down the rules looking for unbound ones to try. |
| 191 var triedRules = 0; | 191 var triedRules = 0; |
| 192 for (var rule in _splitter.rules) { | 192 for (var rule in _splitter.rules) { |
| 193 if (_liveRules.contains(rule)) { | 193 if (_liveRules.contains(rule)) { |
| 194 // We found one worth trying, so try all of its values. | 194 // We found one worth trying, so try all of its values. |
| 195 for (var value = 1; value < rule.numValues; value++) { | 195 for (var value = 1; value < rule.numValues; value++) { |
| 196 var boundRules = unsplitRules.clone(); | 196 var boundRules = unsplitRules.clone(); |
| 197 | 197 |
| 198 var mustSplitRules; | 198 List<Rule> mustSplitRules; |
| 199 var valid = boundRules.tryBind(_splitter.rules, rule, value, (rule) { | 199 var valid = boundRules.tryBind(_splitter.rules, rule, value, (rule) { |
| 200 if (mustSplitRules == null) mustSplitRules = []; | 200 if (mustSplitRules == null) mustSplitRules = []; |
| 201 mustSplitRules.add(rule); | 201 mustSplitRules.add(rule); |
| 202 }); | 202 }); |
| 203 | 203 |
| 204 // Make sure we don't violate the constraints of the bound rules. | 204 // Make sure we don't violate the constraints of the bound rules. |
| 205 if (!valid) continue; | 205 if (!valid) continue; |
| 206 | 206 |
| 207 var state = new SolveState(_splitter, boundRules); | 207 var state = new SolveState(_splitter, boundRules); |
| 208 | 208 |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 446 } | 446 } |
| 447 | 447 |
| 448 /// Lazily initializes the [_boundInUnboundLines], which is needed to compare | 448 /// Lazily initializes the [_boundInUnboundLines], which is needed to compare |
| 449 /// two states for overlap. | 449 /// two states for overlap. |
| 450 /// | 450 /// |
| 451 /// We do this lazily because the calculation is a bit slow, and is only | 451 /// We do this lazily because the calculation is a bit slow, and is only |
| 452 /// needed when we have two states with the same score. | 452 /// needed when we have two states with the same score. |
| 453 void _ensureBoundRulesInUnboundLines() { | 453 void _ensureBoundRulesInUnboundLines() { |
| 454 if (_boundRulesInUnboundLines != null) return; | 454 if (_boundRulesInUnboundLines != null) return; |
| 455 | 455 |
| 456 _boundRulesInUnboundLines = new Set(); | 456 _boundRulesInUnboundLines = new Set<Rule>(); |
| 457 | 457 |
| 458 var boundInLine = new Set(); | 458 var boundInLine = new Set<Rule>(); |
| 459 var hasUnbound = false; | 459 var hasUnbound = false; |
| 460 | 460 |
| 461 for (var i = 0; i < _splitter.chunks.length - 1; i++) { | 461 for (var i = 0; i < _splitter.chunks.length - 1; i++) { |
| 462 if (splits.shouldSplitAt(i)) { | 462 if (splits.shouldSplitAt(i)) { |
| 463 if (hasUnbound) _boundRulesInUnboundLines.addAll(boundInLine); | 463 if (hasUnbound) _boundRulesInUnboundLines.addAll(boundInLine); |
| 464 | 464 |
| 465 boundInLine.clear(); | 465 boundInLine.clear(); |
| 466 hasUnbound = false; | 466 hasUnbound = false; |
| 467 } | 467 } |
| 468 | 468 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 521 void _ensureUnboundConstraints() { | 521 void _ensureUnboundConstraints() { |
| 522 if (_unboundConstraints != null) return; | 522 if (_unboundConstraints != null) return; |
| 523 | 523 |
| 524 // _ensureConstraints should be called first which initializes these. | 524 // _ensureConstraints should be called first which initializes these. |
| 525 assert(_boundRules != null); | 525 assert(_boundRules != null); |
| 526 assert(_unboundRules != null); | 526 assert(_unboundRules != null); |
| 527 | 527 |
| 528 _unboundConstraints = {}; | 528 _unboundConstraints = {}; |
| 529 | 529 |
| 530 for (var unbound in _unboundRules) { | 530 for (var unbound in _unboundRules) { |
| 531 var disallowedValues; | 531 Set<int> disallowedValues; |
| 532 | 532 |
| 533 for (var bound in unbound.constrainedRules) { | 533 for (var bound in unbound.constrainedRules) { |
| 534 if (!_boundRules.contains(bound)) continue; | 534 if (!_boundRules.contains(bound)) continue; |
| 535 | 535 |
| 536 var boundValue = _ruleValues.getValue(bound); | 536 var boundValue = _ruleValues.getValue(bound); |
| 537 | 537 |
| 538 for (var value = 0; value < unbound.numValues; value++) { | 538 for (var value = 0; value < unbound.numValues; value++) { |
| 539 var constraint = unbound.constrain(value, bound); | 539 var constraint = unbound.constrain(value, bound); |
| 540 | 540 |
| 541 // If the unbound rule doesn't place any constraint on this bound | 541 // If the unbound rule doesn't place any constraint on this bound |
| 542 // rule, we're fine. | 542 // rule, we're fine. |
| 543 if (constraint == null) continue; | 543 if (constraint == null) continue; |
| 544 | 544 |
| 545 // If the bound rule's value already meets the constraint it applies, | 545 // If the bound rule's value already meets the constraint it applies, |
| 546 // we don't need to track it. This way, two states that have the | 546 // we don't need to track it. This way, two states that have the |
| 547 // same bound value, one of which has a satisfied constraint, are | 547 // same bound value, one of which has a satisfied constraint, are |
| 548 // still allowed to overlap. | 548 // still allowed to overlap. |
| 549 if (constraint == boundValue) continue; | 549 if (constraint == boundValue) continue; |
| 550 if (constraint == Rule.mustSplit && boundValue != Rule.unsplit) { | 550 if (constraint == Rule.mustSplit && boundValue != Rule.unsplit) { |
| 551 continue; | 551 continue; |
| 552 } | 552 } |
| 553 | 553 |
| 554 if (disallowedValues == null) { | 554 if (disallowedValues == null) { |
| 555 disallowedValues = new Set(); | 555 disallowedValues = new Set<int>(); |
| 556 _unboundConstraints[unbound] = disallowedValues; | 556 _unboundConstraints[unbound] = disallowedValues; |
| 557 } | 557 } |
| 558 | 558 |
| 559 disallowedValues.add(value); | 559 disallowedValues.add(value); |
| 560 } | 560 } |
| 561 } | 561 } |
| 562 } | 562 } |
| 563 } | 563 } |
| 564 | 564 |
| 565 String toString() { | 565 String toString() { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 585 | 585 |
| 586 buffer.write(" \$${splits.cost}"); | 586 buffer.write(" \$${splits.cost}"); |
| 587 | 587 |
| 588 if (overflowChars > 0) buffer.write(" (${overflowChars} over)"); | 588 if (overflowChars > 0) buffer.write(" (${overflowChars} over)"); |
| 589 if (!_isComplete) buffer.write(" (incomplete)"); | 589 if (!_isComplete) buffer.write(" (incomplete)"); |
| 590 if (splits == null) buffer.write(" invalid"); | 590 if (splits == null) buffer.write(" invalid"); |
| 591 | 591 |
| 592 return buffer.toString(); | 592 return buffer.toString(); |
| 593 } | 593 } |
| 594 } | 594 } |
| OLD | NEW |