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 /// Internal debugging utilities. | 5 /// Internal debugging utilities. |
6 library dart_style.src.debug; | 6 library dart_style.src.debug; |
7 | 7 |
8 import 'dart:math' as math; | 8 import 'dart:math' as math; |
9 | 9 |
10 import 'chunk.dart'; | 10 import 'chunk.dart'; |
11 import 'line_prefix.dart'; | 11 import 'rule/rule.dart'; |
12 import 'line_splitter.dart'; | 12 import 'rule_set.dart'; |
13 | 13 |
14 /// Set this to `true` to turn on diagnostic output while building chunks. | 14 /// Set this to `true` to turn on diagnostic output while building chunks. |
15 bool traceChunkBuilder = false; | 15 bool traceChunkBuilder = false; |
16 | 16 |
17 /// Set this to `true` to turn on diagnostic output while writing lines. | 17 /// Set this to `true` to turn on diagnostic output while writing lines. |
18 bool traceLineWriter = false; | 18 bool traceLineWriter = false; |
19 | 19 |
20 /// Set this to `true` to turn on diagnostic output while line splitting. | 20 /// Set this to `true` to turn on diagnostic output while line splitting. |
21 bool traceSplitter = false; | 21 bool traceSplitter = false; |
22 | 22 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 spans.addAll(chunk.spans); | 77 spans.addAll(chunk.spans); |
78 | 78 |
79 addSpans(chunk.blockChunks); | 79 addSpans(chunk.blockChunks); |
80 } | 80 } |
81 } | 81 } |
82 | 82 |
83 addSpans(chunks); | 83 addSpans(chunks); |
84 | 84 |
85 spans = spans.toList(); | 85 spans = spans.toList(); |
86 | 86 |
| 87 var rules = chunks |
| 88 .map((chunk) => chunk.rule) |
| 89 .where((rule) => rule != null && rule is! HardSplitRule) |
| 90 .toSet(); |
| 91 |
87 var rows = []; | 92 var rows = []; |
88 | 93 |
89 addChunk(chunk, prefix, index) { | 94 addChunk(chunk, prefix, index) { |
90 var row = []; | 95 var row = []; |
91 row.add("$prefix$index:"); | 96 row.add("$prefix$index:"); |
92 | 97 |
93 if (chunk.text.length > 70) { | 98 if (chunk.text.length > 70) { |
94 row.add(chunk.text.substring(0, 70)); | 99 row.add(chunk.text.substring(0, 70)); |
95 } else { | 100 } else { |
96 row.add(chunk.text); | 101 row.add(chunk.text); |
97 } | 102 } |
98 | 103 |
99 var spanBars = ""; | 104 var spanBars = ""; |
100 for (var span in spans) { | 105 for (var span in spans) { |
101 spanBars += chunk.spans.contains(span) ? "|" : " "; | 106 spanBars += chunk.spans.contains(span) ? "|" : " "; |
102 } | 107 } |
103 row.add(spanBars); | 108 row.add(spanBars); |
104 | 109 |
105 writeIf(predicate, String callback()) { | 110 writeIf(predicate, String callback()) { |
106 if (predicate) { | 111 if (predicate) { |
107 row.add(callback()); | 112 row.add(callback()); |
108 } else { | 113 } else { |
109 row.add(""); | 114 row.add(""); |
110 } | 115 } |
111 } | 116 } |
112 | 117 |
113 if (chunk.rule != null) { | 118 if (chunk.rule != null) { |
114 row.add(chunk.isHardSplit ? "" : chunk.rule.toString()); | 119 row.add(chunk.isHardSplit ? "" : chunk.rule.toString()); |
115 | 120 |
116 writeIf(chunk.rule.outerRules.isNotEmpty, | 121 var outerRules = chunk.rule.outerRules.toSet().intersection(rules); |
117 () => "-> ${chunk.rule.outerRules.join(" ")}"); | 122 writeIf(outerRules.isNotEmpty, () => "-> ${outerRules.join(" ")}"); |
118 } else { | 123 } else { |
119 row.add("(no rule)"); | 124 row.add("(no rule)"); |
120 | 125 |
121 // Outer rules. | 126 // Outer rules. |
122 row.add(""); | 127 row.add(""); |
123 } | 128 } |
124 | 129 |
125 writeIf(chunk.indent != null && chunk.indent != 0, | 130 writeIf(chunk.indent != null && chunk.indent != 0, |
126 () => "indent ${chunk.indent}"); | 131 () => "indent ${chunk.indent}"); |
127 | 132 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 buffer.writeln(); | 169 buffer.writeln(); |
165 } | 170 } |
166 | 171 |
167 print(buffer.toString()); | 172 print(buffer.toString()); |
168 } | 173 } |
169 | 174 |
170 /// Convert the line to a [String] representation. | 175 /// Convert the line to a [String] representation. |
171 /// | 176 /// |
172 /// It will determine how best to split it into multiple lines of output and | 177 /// It will determine how best to split it into multiple lines of output and |
173 /// return a single string that may contain one or more newline characters. | 178 /// return a single string that may contain one or more newline characters. |
174 void dumpLines(List<Chunk> chunks, LinePrefix prefix, SplitSet splits) { | 179 void dumpLines(List<Chunk> chunks, int firstLineIndent, SplitSet splits) { |
175 var buffer = new StringBuffer(); | 180 var buffer = new StringBuffer(); |
176 | 181 |
177 // TODO(rnystrom): Handle block chunks here. | 182 writeIndent(indent) => buffer.write(gray("| " * (indent ~/ 2))); |
178 | 183 |
179 writeIndent(indent) => buffer.write(gray("| " * (indent ~/ 2))); | 184 writeChunksUnsplit(List<Chunk> chunks) { |
180 writeIndent(prefix.column); | 185 for (var chunk in chunks) { |
| 186 buffer.write(chunk.text); |
| 187 if (chunk.spaceWhenUnsplit) buffer.write(" "); |
181 | 188 |
182 for (var i = prefix.length; i < chunks.length - 1; i++) { | 189 // Recurse into the block. |
| 190 writeChunksUnsplit(chunk.blockChunks); |
| 191 } |
| 192 } |
| 193 |
| 194 writeIndent(firstLineIndent); |
| 195 |
| 196 for (var i = 0; i < chunks.length - 1; i++) { |
183 var chunk = chunks[i]; | 197 var chunk = chunks[i]; |
184 buffer.write(chunk.text); | 198 buffer.write(chunk.text); |
185 | 199 |
186 if (splits.shouldSplitAt(i)) { | 200 if (splits.shouldSplitAt(i)) { |
187 for (var j = 0; j < (chunk.isDouble ? 2 : 1); j++) { | 201 for (var j = 0; j < (chunk.isDouble ? 2 : 1); j++) { |
188 buffer.writeln(); | 202 buffer.writeln(); |
189 writeIndent(splits.getColumn(i)); | 203 writeIndent(splits.getColumn(i)); |
190 } | 204 } |
191 } else { | 205 } else { |
| 206 writeChunksUnsplit(chunk.blockChunks); |
| 207 |
192 if (chunk.spaceWhenUnsplit) buffer.write(" "); | 208 if (chunk.spaceWhenUnsplit) buffer.write(" "); |
193 } | 209 } |
194 } | 210 } |
195 | 211 |
196 buffer.write(chunks.last.text); | 212 buffer.write(chunks.last.text); |
197 log(buffer); | 213 log(buffer); |
198 } | 214 } |
199 | 215 |
200 String _color(String ansiEscape) => useAnsiColors ? ansiEscape : ""; | 216 String _color(String ansiEscape) => useAnsiColors ? ansiEscape : ""; |
OLD | NEW |