OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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.chunk_builder; | 5 library dart_style.src.chunk_builder; |
6 | 6 |
7 import 'chunk.dart'; | 7 import 'chunk.dart'; |
8 import 'dart_formatter.dart'; | 8 import 'dart_formatter.dart'; |
9 import 'debug.dart' as debug; | 9 import 'debug.dart' as debug; |
10 import 'line_splitter.dart'; | 10 import 'line_splitter.dart'; |
(...skipping 29 matching lines...) Expand all Loading... |
40 /// most recent split, even if the visitor "creates" the split before changing | 40 /// most recent split, even if the visitor "creates" the split before changing |
41 /// indentation or nesting. | 41 /// indentation or nesting. |
42 Whitespace _pendingWhitespace = Whitespace.none; | 42 Whitespace _pendingWhitespace = Whitespace.none; |
43 | 43 |
44 /// The nested stack of rules that are currently in use. | 44 /// The nested stack of rules that are currently in use. |
45 /// | 45 /// |
46 /// New chunks are implicitly split by the innermost rule when the chunk is | 46 /// New chunks are implicitly split by the innermost rule when the chunk is |
47 /// ended. | 47 /// ended. |
48 final _rules = <Rule>[]; | 48 final _rules = <Rule>[]; |
49 | 49 |
| 50 /// The set of rules known to contain hard splits that will in turn force |
| 51 /// these rules to harden. |
| 52 /// |
| 53 /// This is accumulated lazily while chunks are being built. Then, once they |
| 54 /// are all done, the rules are all hardened. We do this later because some |
| 55 /// rules may not have all of their constraints fully wired up until after |
| 56 /// the hard split appears. For example, a hard split in a positional |
| 57 /// argument list needs to force the named arguments to split too, but we |
| 58 /// don't create that rule until after the positional arguments are done. |
| 59 final _rulesToHarden = new Set<Rule>(); |
| 60 |
50 /// The list of rules that are waiting until the next whitespace has been | 61 /// The list of rules that are waiting until the next whitespace has been |
51 /// written before they start. | 62 /// written before they start. |
52 final _lazyRules = <Rule>[]; | 63 final _lazyRules = <Rule>[]; |
53 | 64 |
54 /// The indexes of the chunks owned by each rule (except for hard splits). | 65 /// The indexes of the chunks owned by each rule (except for hard splits). |
55 final _ruleChunks = <Rule, List<int>>{}; | 66 final _ruleChunks = <Rule, List<int>>{}; |
56 | 67 |
57 /// The nested stack of spans that are currently being written. | 68 /// The nested stack of spans that are currently being written. |
58 final _openSpans = <OpenSpan>[]; | 69 final _openSpans = <OpenSpan>[]; |
59 | 70 |
(...skipping 666 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
726 | 737 |
727 return true; | 738 return true; |
728 } | 739 } |
729 | 740 |
730 /// Pre-processes the chunks after they are done being written by the visitor | 741 /// Pre-processes the chunks after they are done being written by the visitor |
731 /// but before they are run through the line splitter. | 742 /// but before they are run through the line splitter. |
732 /// | 743 /// |
733 /// Marks ranges of chunks that can be line split independently to keep the | 744 /// Marks ranges of chunks that can be line split independently to keep the |
734 /// batches we send to [LineSplitter] small. | 745 /// batches we send to [LineSplitter] small. |
735 void _divideChunks() { | 746 void _divideChunks() { |
| 747 // Harden all of the rules that we know get forced by containing hard |
| 748 // splits, along with all of the other rules they constrain. |
| 749 for (var rule in _rulesToHarden) { |
| 750 _hardenRule(rule); |
| 751 } |
| 752 |
736 // For each independent set of chunks, see if there are any rules in them | 753 // For each independent set of chunks, see if there are any rules in them |
737 // that we want to preemptively harden. This is basically to send smaller | 754 // that we want to preemptively harden. This is basically to send smaller |
738 // batches of chunks to LineSplitter in cases where the code is deeply | 755 // batches of chunks to LineSplitter in cases where the code is deeply |
739 // nested or complex. | 756 // nested or complex. |
740 var start = 0; | 757 var start = 0; |
741 for (var i = 0; i < _chunks.length; i++) { | 758 for (var i = 0; i < _chunks.length; i++) { |
742 if (_canDivideAt(i)) { | 759 if (_canDivideAt(i)) { |
743 _preemptRules(start, i); | 760 _preemptRules(start, i); |
744 start = i; | 761 start = i; |
745 } | 762 } |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
848 /// Hardens the active rules when a hard split occurs within them. | 865 /// Hardens the active rules when a hard split occurs within them. |
849 void _handleHardSplit() { | 866 void _handleHardSplit() { |
850 if (_rules.isEmpty) return; | 867 if (_rules.isEmpty) return; |
851 | 868 |
852 // If the current rule doesn't care, it will "eat" the hard split and no | 869 // If the current rule doesn't care, it will "eat" the hard split and no |
853 // others will care either. | 870 // others will care either. |
854 if (!_rules.last.splitsOnInnerRules) return; | 871 if (!_rules.last.splitsOnInnerRules) return; |
855 | 872 |
856 // Start with the innermost rule. This will traverse the other rules it | 873 // Start with the innermost rule. This will traverse the other rules it |
857 // constrains. | 874 // constrains. |
858 _hardenRule(_rules.last); | 875 _rulesToHarden.add(_rules.last); |
859 } | 876 } |
860 | 877 |
861 /// Replaces [rule] with a hard split. | 878 /// Replaces [rule] with a hard split. |
862 /// | 879 /// |
863 /// This also applies all of the implications of that change: | 880 /// This also applies all of the implications of that change: |
864 /// | 881 /// |
865 /// * Existing chunks using that rule are hardened. | 882 /// * Existing chunks using that rule are hardened. |
866 /// * Later chunks using that rule will use a hard split instead. | 883 /// * Later chunks using that rule will use a hard split instead. |
867 /// * Any other rules that are constrained by this one are also hardened. | 884 /// * Any other rules that are constrained by this one are also hardened. |
868 void _hardenRule(Rule rule) { | 885 void _hardenRule(Rule rule) { |
(...skipping 23 matching lines...) Expand all Loading... |
892 if (rule.constrain(rule.fullySplitValue, other) == | 909 if (rule.constrain(rule.fullySplitValue, other) == |
893 other.fullySplitValue) { | 910 other.fullySplitValue) { |
894 harden(other); | 911 harden(other); |
895 } | 912 } |
896 } | 913 } |
897 } | 914 } |
898 | 915 |
899 harden(rule); | 916 harden(rule); |
900 } | 917 } |
901 } | 918 } |
OLD | NEW |