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; | 5 library dart_style.src.chunk; |
6 | 6 |
7 import 'debug.dart'; | 7 import 'debug.dart'; |
8 | 8 |
9 /// Tracks where a selection start or end point may appear in some piece of | 9 /// Tracks where a selection start or end point may appear in some piece of |
10 /// text. | 10 /// text. |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 bool operator ==(other) => identical(this, other); | 276 bool operator ==(other) => identical(this, other); |
277 } | 277 } |
278 | 278 |
279 /// Delimits a range of chunks that must end up on the same line to avoid an | 279 /// Delimits a range of chunks that must end up on the same line to avoid an |
280 /// additional cost. | 280 /// additional cost. |
281 /// | 281 /// |
282 /// These are used to encourage the line splitter to try to keep things | 282 /// These are used to encourage the line splitter to try to keep things |
283 /// together, like parameter lists and binary operator expressions. | 283 /// together, like parameter lists and binary operator expressions. |
284 class Span { | 284 class Span { |
285 /// Index of the first chunk contained in this span. | 285 /// Index of the first chunk contained in this span. |
286 final int start; | 286 int get start => _start; |
| 287 int _start; |
287 | 288 |
288 /// Index of the last chunk contained in this span. | 289 /// Index of the last chunk contained in this span. |
289 int get end => _end; | 290 int get end => _end; |
290 int _end; | 291 int _end; |
291 | 292 |
292 /// The cost applied when the span is split across multiple lines or `null` | 293 /// The cost applied when the span is split across multiple lines or `null` |
293 /// if the span is for a multisplit. | 294 /// if the span is for a multisplit. |
294 final int cost; | 295 final int cost; |
295 | 296 |
296 Span(this.start, this.cost); | 297 Span(this._start, this.cost); |
297 | 298 |
298 /// Marks this span as ending at [end]. | 299 /// Marks this span as ending at [end]. |
299 void close(int end) { | 300 void close(int end) { |
300 assert(_end == null); | 301 assert(_end == null); |
301 _end = end; | 302 _end = end; |
302 } | 303 } |
303 | 304 |
304 String toString() { | 305 String toString() { |
305 var result = "Span($start"; | 306 var result = "Span($start"; |
306 | 307 |
307 if (end != null) { | 308 if (end != null) { |
308 result += " - $end"; | 309 result += " - $end"; |
309 } else { | 310 } else { |
310 result += "..."; | 311 result += "..."; |
311 } | 312 } |
312 | 313 |
313 if (cost != null) result += " \$$cost"; | 314 if (cost != null) result += " \$$cost"; |
314 | 315 |
315 return result + ")"; | 316 return result + ")"; |
316 } | 317 } |
| 318 |
| 319 /// Shifts the indexes of the chunk down by [offset]. |
| 320 /// |
| 321 /// This is used when a prefix of the chunk list gets pulled off by the |
| 322 /// [LineWriter] after it gets formatted as a line. The remaining spans need |
| 323 /// to have their indices shifted to account for the removed chunks. |
| 324 /// |
| 325 /// Returns `true` if the span has shifted all the way off the front and |
| 326 /// should just be discarded. |
| 327 bool shift(int offset) { |
| 328 if (end != null && end < offset) return true; |
| 329 |
| 330 _start -= offset; |
| 331 if (_end != null) _end -= offset; |
| 332 |
| 333 return false; |
| 334 } |
317 } | 335 } |
318 | 336 |
319 /// A comment in the source, with a bit of information about the surrounding | 337 /// A comment in the source, with a bit of information about the surrounding |
320 /// whitespace. | 338 /// whitespace. |
321 class SourceComment extends Selection { | 339 class SourceComment extends Selection { |
322 /// The text of the comment, including `//`, `/*`, and `*/`. | 340 /// The text of the comment, including `//`, `/*`, and `*/`. |
323 final String text; | 341 final String text; |
324 | 342 |
325 /// The number of newlines between the comment or token preceding this comment | 343 /// The number of newlines between the comment or token preceding this comment |
326 /// and the beginning of this one. | 344 /// and the beginning of this one. |
327 /// | 345 /// |
328 /// Will be zero if the comment is a trailing one. | 346 /// Will be zero if the comment is a trailing one. |
329 final int linesBefore; | 347 final int linesBefore; |
330 | 348 |
331 /// Whether this comment is a line comment. | 349 /// Whether this comment is a line comment. |
332 final bool isLineComment; | 350 final bool isLineComment; |
333 | 351 |
334 /// Whether this comment starts at column one in the source. | 352 /// Whether this comment starts at column one in the source. |
335 /// | 353 /// |
336 /// Comments that start at the start of the line will not be indented in the | 354 /// Comments that start at the start of the line will not be indented in the |
337 /// output. This way, commented out chunks of code do not get erroneously | 355 /// output. This way, commented out chunks of code do not get erroneously |
338 /// re-indented. | 356 /// re-indented. |
339 final bool isStartOfLine; | 357 final bool isStartOfLine; |
340 | 358 |
341 SourceComment(this.text, this.linesBefore, | 359 SourceComment(this.text, this.linesBefore, |
342 {this.isLineComment, this.isStartOfLine}); | 360 {this.isLineComment, this.isStartOfLine}); |
343 } | 361 } |
OLD | NEW |