Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(437)

Side by Side Diff: lib/src/chunk.dart

Issue 1492683002: Change the way hard splits are handled. (Closed) Base URL: https://github.com/dart-lang/dart_style.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/src/argument_list_visitor.dart ('k') | lib/src/chunk_builder.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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; 5 library dart_style.src.chunk;
6 6
7 import 'fast_hash.dart'; 7 import 'fast_hash.dart';
8 import 'nesting_level.dart'; 8 import 'nesting_level.dart';
9 import 'rule/rule.dart'; 9 import 'rule/rule.dart';
10 10
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 /// called, no more text should be added to the chunk since it would appear 107 /// called, no more text should be added to the chunk since it would appear
108 /// *before* the split. 108 /// *before* the split.
109 bool get canAddText => _rule == null; 109 bool get canAddText => _rule == null;
110 110
111 /// The [Rule] that controls when a split should occur after this chunk. 111 /// The [Rule] that controls when a split should occur after this chunk.
112 /// 112 ///
113 /// Multiple splits may share a [Rule]. 113 /// Multiple splits may share a [Rule].
114 Rule get rule => _rule; 114 Rule get rule => _rule;
115 Rule _rule; 115 Rule _rule;
116 116
117 /// Whether this chunk is always followed by a newline or whether the line
118 /// splitter may choose to keep the next chunk on the same line.
119 bool get isHardSplit => _rule is HardSplitRule;
120
121 /// Whether or not an extra blank line should be output after this chunk if 117 /// Whether or not an extra blank line should be output after this chunk if
122 /// it's split. 118 /// it's split.
123 /// 119 ///
124 /// Internally, this can be either `true`, `false`, or `null`. The latter is 120 /// Internally, this can be either `true`, `false`, or `null`. The latter is
125 /// an indeterminate state that lets later modifications to the split decide 121 /// an indeterminate state that lets later modifications to the split decide
126 /// whether it should be double or not. 122 /// whether it should be double or not.
127 /// 123 ///
128 /// However, this getter does not expose that. It will return `false` if the 124 /// However, this getter does not expose that. It will return `false` if the
129 /// chunk is still indeterminate. 125 /// chunk is still indeterminate.
130 bool get isDouble => _isDouble != null ? _isDouble : false; 126 bool get isDouble => _isDouble != null ? _isDouble : false;
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 void allowText() { 186 void allowText() {
191 _rule = null; 187 _rule = null;
192 } 188 }
193 189
194 /// Append [text] to the end of the split's text. 190 /// Append [text] to the end of the split's text.
195 void appendText(String text) { 191 void appendText(String text) {
196 assert(canAddText); 192 assert(canAddText);
197 _text += text; 193 _text += text;
198 } 194 }
199 195
200 /// Forces this soft split to become a hard split.
201 ///
202 /// This is called on the soft splits owned by a rule that decides to harden
203 /// when it finds out another hard split occurs within its chunks.
204 void harden() {
205 _rule = new HardSplitRule();
206 spans.clear();
207 }
208
209 /// Finishes off this chunk with the given [rule] and split information. 196 /// Finishes off this chunk with the given [rule] and split information.
210 /// 197 ///
211 /// This may be called multiple times on the same split since the splits 198 /// This may be called multiple times on the same split since the splits
212 /// produced by walking the source and the splits coming from comments and 199 /// produced by walking the source and the splits coming from comments and
213 /// preserved whitespace often overlap. When that happens, this has logic to 200 /// preserved whitespace often overlap. When that happens, this has logic to
214 /// combine that information into a single split. 201 /// combine that information into a single split.
215 void applySplit(Rule rule, int indent, NestingLevel nesting, 202 void applySplit(Rule rule, int indent, NestingLevel nesting,
216 {bool flushLeft, bool isDouble, bool space}) { 203 {bool flushLeft, bool isDouble, bool space}) {
217 if (flushLeft == null) flushLeft = false; 204 if (flushLeft == null) flushLeft = false;
218 if (space == null) space = false; 205 if (space == null) space = false;
219 if (isHardSplit || rule is HardSplitRule) { 206 if (rule.isHardened) {
220 // A hard split always wins. 207 // A hard split always wins.
221 _rule = rule; 208 _rule = rule;
222 } else if (_rule == null) { 209 } else if (_rule == null) {
223 // If the chunk hasn't been initialized yet, just inherit the rule. 210 // If the chunk hasn't been initialized yet, just inherit the rule.
224 _rule = rule; 211 _rule = rule;
225 } 212 }
226 213
227 // Last split settings win. 214 // Last split settings win.
228 _flushLeft = flushLeft; 215 _flushLeft = flushLeft;
229 _nesting = nesting; 216 _nesting = nesting;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 251
265 if (text.isNotEmpty) parts.add(text); 252 if (text.isNotEmpty) parts.add(text);
266 253
267 if (_indent != null) parts.add("indent:$_indent"); 254 if (_indent != null) parts.add("indent:$_indent");
268 if (spaceWhenUnsplit == true) parts.add("space"); 255 if (spaceWhenUnsplit == true) parts.add("space");
269 if (_isDouble == true) parts.add("double"); 256 if (_isDouble == true) parts.add("double");
270 if (_flushLeft == true) parts.add("flush"); 257 if (_flushLeft == true) parts.add("flush");
271 258
272 if (_rule == null) { 259 if (_rule == null) {
273 parts.add("(no split)"); 260 parts.add("(no split)");
274 } else if (isHardSplit) {
275 parts.add("hard");
276 } else { 261 } else {
277 parts.add(rule.toString()); 262 parts.add(rule.toString());
263 if (rule.isHardened) parts.add("(hard)");
278 264
279 if (_rule.constrainedRules.isNotEmpty) { 265 if (_rule.constrainedRules.isNotEmpty) {
280 parts.add("-> ${_rule.constrainedRules.join(' ')}"); 266 parts.add("-> ${_rule.constrainedRules.join(' ')}");
281 } 267 }
282 } 268 }
283 269
284 return parts.join(" "); 270 return parts.join(" ");
285 } 271 }
286 } 272 }
287 273
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 /// output. This way, commented out chunks of code do not get erroneously 389 /// output. This way, commented out chunks of code do not get erroneously
404 /// re-indented. 390 /// re-indented.
405 final bool flushLeft; 391 final bool flushLeft;
406 392
407 /// Whether this comment is an inline block comment. 393 /// Whether this comment is an inline block comment.
408 bool get isInline => linesBefore == 0 && !isLineComment; 394 bool get isInline => linesBefore == 0 && !isLineComment;
409 395
410 SourceComment(this.text, this.linesBefore, 396 SourceComment(this.text, this.linesBefore,
411 {this.isLineComment, this.flushLeft}); 397 {this.isLineComment, this.flushLeft});
412 } 398 }
OLDNEW
« no previous file with comments | « lib/src/argument_list_visitor.dart ('k') | lib/src/chunk_builder.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698