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 |
| 10 /// text. |
| 11 abstract class Selection { |
| 12 /// The chunk of text. |
| 13 String get text; |
| 14 |
| 15 /// The offset from the beginning of [text] where the selection starts, or |
| 16 /// `null` if the selection does not start within this chunk. |
| 17 int get selectionStart => _selectionStart; |
| 18 int _selectionStart; |
| 19 |
| 20 /// The offset from the beginning of [text] where the selection ends, or |
| 21 /// `null` if the selection does not start within this chunk. |
| 22 int get selectionEnd => _selectionEnd; |
| 23 int _selectionEnd; |
| 24 |
| 25 /// Sets [selectionStart] to be [start] characters into [text]. |
| 26 void startSelection(int start) { |
| 27 _selectionStart = start; |
| 28 } |
| 29 |
| 30 /// Sets [selectionStart] to be [fromEnd] characters from the end of [text]. |
| 31 void startSelectionFromEnd(int fromEnd) { |
| 32 _selectionStart = text.length - fromEnd; |
| 33 } |
| 34 |
| 35 /// Sets [selectionEnd] to be [end] characters into [text]. |
| 36 void endSelection(int end) { |
| 37 _selectionEnd = end; |
| 38 } |
| 39 |
| 40 /// Sets [selectionEnd] to be [fromEnd] characters from the end of [text]. |
| 41 void endSelectionFromEnd(int fromEnd) { |
| 42 _selectionEnd = text.length - fromEnd; |
| 43 } |
| 44 } |
| 45 |
9 /// A chunk of non-breaking output text terminated by a hard or soft newline. | 46 /// A chunk of non-breaking output text terminated by a hard or soft newline. |
10 /// | 47 /// |
11 /// Chunks are created by [LineWriter] and fed into [LineSplitter]. Each | 48 /// Chunks are created by [LineWriter] and fed into [LineSplitter]. Each |
12 /// contains some text, along with the data needed to tell how the next line | 49 /// contains some text, along with the data needed to tell how the next line |
13 /// should be formatted and how desireable it is to split after the chunk. | 50 /// should be formatted and how desireable it is to split after the chunk. |
14 /// | 51 /// |
15 /// Line splitting after chunks comes in a few different forms. | 52 /// Line splitting after chunks comes in a few different forms. |
16 /// | 53 /// |
17 /// * A "hard" split is a mandatory newline. The formatted output will contain | 54 /// * A "hard" split is a mandatory newline. The formatted output will contain |
18 /// at least one newline after the chunk's text. | 55 /// at least one newline after the chunk's text. |
19 /// * A "soft" split is a discretionary newline. If a line doesn't fit within | 56 /// * A "soft" split is a discretionary newline. If a line doesn't fit within |
20 /// the page width, one or more soft splits may be turned into newlines to | 57 /// the page width, one or more soft splits may be turned into newlines to |
21 /// wrap the line to fit within the bounds. If a soft split is not turned | 58 /// wrap the line to fit within the bounds. If a soft split is not turned |
22 /// into a newline, it may instead appear as a space or zero-length string | 59 /// into a newline, it may instead appear as a space or zero-length string |
23 /// in the output, depending on [spaceWhenUnsplit]. | 60 /// in the output, depending on [spaceWhenUnsplit]. |
24 /// * A "double" split expands to two newlines. In other words, it leaves a | 61 /// * A "double" split expands to two newlines. In other words, it leaves a |
25 /// blank line in the output. Hard or soft splits may be doubled. This is | 62 /// blank line in the output. Hard or soft splits may be doubled. This is |
26 /// determined by [isDouble]. | 63 /// determined by [isDouble]. |
27 /// | 64 /// |
28 /// A split controls the leading spacing of the subsequent line, both | 65 /// A split controls the leading spacing of the subsequent line, both |
29 /// block-based [indent] and expression-wrapping-based [nesting]. | 66 /// block-based [indent] and expression-wrapping-based [nesting]. |
30 class Chunk { | 67 class Chunk extends Selection { |
31 /// The literal text output for the chunk. | 68 /// The literal text output for the chunk. |
32 String get text => _text; | 69 String get text => _text; |
33 String _text; | 70 String _text; |
34 | 71 |
35 /// The indentation level of the line following this chunk. | 72 /// The indentation level of the line following this chunk. |
36 /// | 73 /// |
37 /// Note that this is not a relative indentation *offset*. It's the full | 74 /// Note that this is not a relative indentation *offset*. It's the full |
38 /// indentation. When a chunk is newly created from text, this is `null` to | 75 /// indentation. When a chunk is newly created from text, this is `null` to |
39 /// indicate that the chunk has no splitting information yet. | 76 /// indicate that the chunk has no splitting information yet. |
40 int get indent => _indent; | 77 int get indent => _indent; |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 result += " - $end"; | 309 result += " - $end"; |
273 } else { | 310 } else { |
274 result += "..."; | 311 result += "..."; |
275 } | 312 } |
276 | 313 |
277 if (cost != null) result += " \$$cost"; | 314 if (cost != null) result += " \$$cost"; |
278 | 315 |
279 return result + ")"; | 316 return result + ")"; |
280 } | 317 } |
281 } | 318 } |
| 319 |
| 320 /// A comment in the source, with a bit of information about the surrounding |
| 321 /// whitespace. |
| 322 class SourceComment extends Selection { |
| 323 /// The text of the comment, including `//`, `/*`, and `*/`. |
| 324 final String text; |
| 325 |
| 326 /// The number of newlines between the comment or token preceding this comment |
| 327 /// and the beginning of this one. |
| 328 /// |
| 329 /// Will be zero if the comment is a trailing one. |
| 330 final int linesBefore; |
| 331 |
| 332 /// Whether this comment is a line comment. |
| 333 final bool isLineComment; |
| 334 |
| 335 /// Whether this comment starts at column one in the source. |
| 336 /// |
| 337 /// Comments that start at the start of the line will not be indented in the |
| 338 /// output. This way, commented out chunks of code do not get erroneously |
| 339 /// re-indented. |
| 340 final bool isStartOfLine; |
| 341 |
| 342 SourceComment(this.text, this.linesBefore, |
| 343 {this.isLineComment, this.isStartOfLine}); |
| 344 } |
OLD | NEW |