| 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
|
| 3 | | // BSD-style license that can be found in the LICENSE file.
|
| 4 | |
|
| 5 | | library dart_style.src.chunk_builder;
|
| 6 | |
|
| 7 | | import 'chunk.dart';
|
| 8 | | import 'dart_formatter.dart';
|
| 9 | | import 'debug.dart' as debug;
|
| 10 | | import 'line_writer.dart';
|
| 11 | | import 'nesting_builder.dart';
|
| 12 | | import 'nesting_level.dart';
|
| 13 | | import 'rule/rule.dart';
|
| 14 | | import 'source_code.dart';
|
| 15 | | import 'style_fix.dart';
|
| 16 | | import 'whitespace.dart';
|
| 17 | |
|
| 18 | | /// Matches if the last character of a string is an identifier character.
|
| 19 | | final _trailingIdentifierChar = new RegExp(r"[a-zA-Z0-9_]$");
|
| 20 | |
|
| 21 | | /// Matches a JavaDoc-style doc comment that starts with "/**" and ends with
|
| 22 | | /// "*/" or "**/".
|
| 23 | | final _javaDocComment = new RegExp(r"^/\*\*([^*/][\s\S]*?)\*?\*/$");
|
| 24 | |
|
| 25 | | /// Matches the leading "*" in a line in the middle of a JavaDoc-style comment.
|
| 26 | | var _javaDocLine = new RegExp(r"^\s*\*(.*)");
|
| 27 | |
|
| 28 | | /// Takes the incremental serialized output of [SourceVisitor]--the source text
|
| 29 | | /// along with any comments and preserved whitespace--and produces a coherent
|
| 30 | | /// tree of [Chunk]s which can then be split into physical lines.
|
| 31 | | ///
|
| 32 | | /// Keeps track of leading indentation, expression nesting, and all of the hairy
|
| 33 | | /// code required to seamlessly integrate existing comments into the pure
|
| 34 | | /// output produced by [SourceVisitor].
|
| 35 | | class ChunkBuilder {
|
| 36 | | final DartFormatter _formatter;
|
| 37 | |
|
| 38 | | /// The builder for the code surrounding the block that this writer is for, or
|
| 39 | | /// `null` if this is writing the top-level code.
|
| 40 | | final ChunkBuilder _parent;
|
| 41 | |
|
| 42 | | final SourceCode _source;
|
| 43 | |
|
| 44 | | final List<Chunk> _chunks;
|
| 45 | |
|
| 46 | | /// The whitespace that should be written to [_chunks] before the next
|
| 47 | | /// non-whitespace token.
|
| 48 | | ///
|
| 49 | | /// This ensures that changes to indentation and nesting also apply to the
|
| 50 | | /// most recent split, even if the visitor "creates" the split before changing
|
| 51 | | /// indentation or nesting.
|
| 52 | | Whitespace _pendingWhitespace = Whitespace.none;
|
| 53 | |
|
| 54 | | /// The nested stack of rules that are currently in use.
|
| 55 | | ///
|
| 56 | | /// New chunks are implicitly split by the innermost rule when the chunk is
|
| 57 | | /// ended.
|
| 58 | | final _rules = <Rule>[];
|
| 59 | |
|
| 60 | | /// The set of rules known to contain hard splits that will in turn force
|
| 61 | | /// these rules to harden.
|
| 62 | | ///
|
| 63 | | /// This is accumulated lazily while chunks are being built. Then, once they
|
| 64 | | /// are all done, the rules are all hardened. We do this later because some
|
| 65 | | /// rules may not have all of their constraints fully wired up until after
|
| 66 | | /// the hard split appears. For example, a hard split in a positional
|
| 67 | | /// argument list needs to force the named arguments to split too, but we
|
| 68 | | /// don't create that rule until after the positional arguments are done.
|
| 69 | | final _hardSplitRules = new Set<Rule>();
|
| 70 | |
|
| 71 | | /// The list of rules that are waiting until the next whitespace has been
|
| 72 | | /// written before they start.
|
| 73 | | final _lazyRules = <Rule>[];
|
| 74 | |
|
| 75 | | /// The nested stack of spans that are currently being written.
|
| 76 | | final _openSpans = <OpenSpan>[];
|
| 77 | |
|
| 78 | | /// The current state.
|
| 79 | | final _nesting = new NestingBuilder();
|
| 80 | |
|
| 81 | | /// The stack of nesting levels where block arguments may start.
|
| 82 | | ///
|
| 83 | | /// A block argument's contents will nest at the last level in this stack.
|
| 84 | | final _blockArgumentNesting = <NestingLevel>[];
|
| 85 | |
|
| 86 | | /// The index of the "current" chunk being written.
|
| 87 | | ///
|
| 88 | | /// If the last chunk is still being appended to, this is its index.
|
| 89 | | /// Otherwise, it is the index of the next chunk which will be created.
|
| 90 | 3 | int get _currentChunkIndex {
|
| 91 | 6 | if (_chunks.isEmpty) return 0;
|
| 92 | 18 | if (_chunks.last.canAddText) return _chunks.length - 1;
|
| 93 | 6 | return _chunks.length;
|
| 94 | | }
|
| 95 | |
|
| 96 | | /// Whether or not there was a leading comment that was flush left before any
|
| 97 | | /// other content was written.
|
| 98 | | ///
|
| 99 | | /// This is used when writing child blocks to make the parent chunk have the
|
| 100 | | /// right flush left value when a comment appears immediately inside the
|
| 101 | | /// block.
|
| 102 | | bool _firstFlushLeft = false;
|
| 103 | |
|
| 104 | | /// The number of calls to [preventSplit()] that have not been ended by a
|
| 105 | | /// call to [endPreventSplit()].
|
| 106 | | ///
|
| 107 | | /// Splitting is completely disabled inside string interpolation. We do want
|
| 108 | | /// to fix the whitespace inside interpolation, though, so we still format
|
| 109 | | /// them. This tracks whether we're inside an interpolation. We can't use a
|
| 110 | | /// simple bool because interpolation can nest.
|
| 111 | | ///
|
| 112 | | /// When this is non-zero, splits are ignored.
|
| 113 | | int _preventSplitNesting = 0;
|
| 114 | |
|
| 115 | | /// Whether there is pending whitespace that depends on the number of
|
| 116 | | /// newlines in the source.
|
| 117 | | ///
|
| 118 | | /// This is used to avoid calculating the newlines between tokens unless
|
| 119 | | /// actually needed since doing so is slow when done between every single
|
| 120 | | /// token pair.
|
| 121 | 3 | bool get needsToPreserveNewlines =>
|
| 122 | 6 | _pendingWhitespace == Whitespace.oneOrTwoNewlines ||
|
| 123 | 6 | _pendingWhitespace == Whitespace.splitOrTwoNewlines ||
|
| 124 | 6 | _pendingWhitespace == Whitespace.splitOrNewline;
|
| 125 | |
|
| 126 | | /// The number of characters of code that can fit in a single line.
|
| 127 | 0 | int get pageWidth => _formatter.pageWidth;
|
| 128 | |
|
| 129 | | /// The current innermost rule.
|
| 130 | 6 | Rule get rule => _rules.last;
|
| 131 | |
|
| 132 | 3 | ChunkBuilder(this._formatter, this._source)
|
| 133 | | : _parent = null,
|
| 134 | 3 | _chunks = [] {
|
| 135 | 9 | indent(_formatter.indent);
|
| 136 | 3 | startBlockArgumentNesting();
|
| 137 | | }
|
| 138 | |
|
| 139 | 2 | ChunkBuilder._(this._parent, this._formatter, this._source, this._chunks) {
|
| 140 | 2 | startBlockArgumentNesting();
|
| 141 | | }
|
| 142 | |
|
| 143 | | /// Writes [string], the text for a single token, to the output.
|
| 144 | | ///
|
| 145 | | /// By default, this also implicitly adds one level of nesting if we aren't
|
| 146 | | /// currently nested at all. We do this here so that if a comment appears
|
| 147 | | /// after any token within a statement or top-level form and that comment
|
| 148 | | /// leads to splitting, we correctly nest. Even pathological cases like:
|
| 149 | | ///
|
| 150 | | ///
|
| 151 | | /// import // comment
|
| 152 | | /// "this_gets_nested.dart";
|
| 153 | | ///
|
| 154 | | /// If we didn't do this here, we'd have to call [nestExpression] after the
|
| 155 | | /// first token of practically every grammar production.
|
| 156 | 3 | void write(String string) {
|
| 157 | 3 | _emitPendingWhitespace();
|
| 158 | 3 | _writeText(string);
|
| 159 | |
|
| 160 | 9 | _lazyRules.forEach(_activateRule);
|
| 161 | 6 | _lazyRules.clear();
|
| 162 | |
|
| 163 | 6 | _nesting.commitNesting();
|
| 164 | | }
|
| 165 | |
|
| 166 | | /// Writes a [WhitespaceChunk] of [type].
|
| 167 | 3 | void writeWhitespace(Whitespace type) {
|
| 168 | 3 | _pendingWhitespace = type;
|
| 169 | | }
|
| 170 | |
|
| 171 | | /// Write a split owned by the current innermost rule.
|
| 172 | | ///
|
| 173 | | /// If [flushLeft] is `true`, then forces the next line to start at column
|
| 174 | | /// one regardless of any indentation or nesting.
|
| 175 | | ///
|
| 176 | | /// If [isDouble] is passed, forces the split to either be a single or double
|
| 177 | | /// newline. Otherwise, leaves it indeterminate.
|
| 178 | | ///
|
| 179 | | /// If [nest] is `false`, ignores any current expression nesting. Otherwise,
|
| 180 | | /// uses the current nesting level. If unsplit, it expands to a space if
|
| 181 | | /// [space] is `true`.
|
| 182 | 3 | Chunk split({bool flushLeft, bool isDouble, bool nest, bool space}) {
|
| 183 | | space ??= false;
|
| 184 | |
|
| 185 | | // If we are not allowed to split at all, don't. Returning null for the
|
| 186 | | // chunk is safe since the rule that uses the chunk will itself get
|
| 187 | | // discarded because no chunk references it.
|
| 188 | 6 | if (_preventSplitNesting > 0) {
|
| 189 | 0 | if (space) _pendingWhitespace = Whitespace.space;
|
| 190 | | return null;
|
| 191 | | }
|
| 192 | |
|
| 193 | 9 | return _writeSplit(_rules.last,
|
| 194 | | flushLeft: flushLeft, isDouble: isDouble, nest: nest, space: space);
|
| 195 | | }
|
| 196 | |
|
| 197 | | /// Outputs the series of [comments] and associated whitespace that appear
|
| 198 | | /// before [token] (which is not written by this).
|
| 199 | | ///
|
| 200 | | /// The list contains each comment as it appeared in the source between the
|
| 201 | | /// last token written and the next one that's about to be written.
|
| 202 | | ///
|
| 203 | | /// [linesBeforeToken] is the number of lines between the last comment (or
|
| 204 | | /// previous token if there are no comments) and the next token.
|
| 205 | 2 | void writeComments(
|
| 206 | | List<SourceComment> comments, int linesBeforeToken, String token) {
|
| 207 | | // Edge case: if we require a blank line, but there exists one between
|
| 208 | | // some of the comments, or after the last one, then we don't need to
|
| 209 | | // enforce one before the first comment. Example:
|
| 210 | | //
|
| 211 | | // library foo;
|
| 212 | | // // comment
|
| 213 | | //
|
| 214 | | // class Bar {}
|
| 215 | | //
|
| 216 | | // Normally, a blank line is required after `library`, but since there is
|
| 217 | | // one after the comment, we don't need one before it. This is mainly so
|
| 218 | | // that commented out directives stick with their preceding group.
|
| 219 | 4 | if (_pendingWhitespace == Whitespace.twoNewlines &&
|
| 220 | 6 | comments.first.linesBefore < 2) {
|
| 221 | 2 | if (linesBeforeToken > 1) {
|
| 222 | 1 | _pendingWhitespace = Whitespace.newline;
|
| 223 | | } else {
|
| 224 | 5 | for (var i = 1; i < comments.length; i++) {
|
| 225 | 3 | if (comments[i].linesBefore > 1) {
|
| 226 | 0 | _pendingWhitespace = Whitespace.newline;
|
| 227 | | break;
|
| 228 | | }
|
| 229 | | }
|
| 230 | | }
|
| 231 | | }
|
| 232 | |
|
| 233 | | // Edge case: if the previous output was also from a call to
|
| 234 | | // [writeComments()] which ended with a line comment, force a newline.
|
| 235 | | // Normally, comments are strictly interleaved with tokens and you never
|
| 236 | | // get two sequences of comments in a row. However, when applying a fix
|
| 237 | | // that removes a token (like `new`), it's possible to get two sets of
|
| 238 | | // comments in a row, as in:
|
| 239 | | //
|
| 240 | | // // a
|
| 241 | | // new // b
|
| 242 | | // Foo();
|
| 243 | | //
|
| 244 | | // When that happens, we need to make sure the preserve the split at the
|
| 245 | | // end of the first sequence of comments if there is one.
|
| 246 | 2 | if (_pendingWhitespace == null) {
|
| 247 | 2 | comments.first.linesBefore = 1;
|
| 248 | 1 | _pendingWhitespace = Whitespace.none;
|
| 249 | | }
|
| 250 | |
|
| 251 | | // Edge case: if the comments are completely inline (i.e. just a series of
|
| 252 | | // block comments with no newlines before, after, or between them), then
|
| 253 | | // they will eat any pending newlines. Make sure that doesn't happen by
|
| 254 | | // putting the pending whitespace before the first comment and moving them
|
| 255 | | // to their own line. Turns this:
|
| 256 | | //
|
| 257 | | // library foo; /* a */ /* b */ import 'a.dart';
|
| 258 | | //
|
| 259 | | // into:
|
| 260 | | //
|
| 261 | | // library foo;
|
| 262 | | //
|
| 263 | | // /* a */ /* b */
|
| 264 | | // import 'a.dart';
|
| 265 | 2 | if (linesBeforeToken == 0 &&
|
| 266 | 6 | comments.every((comment) => comment.isInline) &&
|
| 267 | 6 | _pendingWhitespace.minimumLines > 0) {
|
| 268 | 4 | comments.first.linesBefore = _pendingWhitespace.minimumLines;
|
| 269 | | linesBeforeToken = 1;
|
| 270 | | }
|
| 271 | |
|
| 272 | | // Write each comment and the whitespace between them.
|
| 273 | 6 | for (var i = 0; i < comments.length; i++) {
|
| 274 | 2 | var comment = comments[i];
|
| 275 | |
|
| 276 | 4 | preserveNewlines(comment.linesBefore);
|
| 277 | |
|
| 278 | | // Don't emit a space because we'll handle it below. If we emit it here,
|
| 279 | | // we may get a trailing space if the comment needs a line before it.
|
| 280 | 4 | if (_pendingWhitespace == Whitespace.space) {
|
| 281 | 2 | _pendingWhitespace = Whitespace.none;
|
| 282 | | }
|
| 283 | 2 | _emitPendingWhitespace();
|
| 284 | |
|
| 285 | 4 | if (comment.linesBefore == 0) {
|
| 286 | | // If we're sitting on a split, move the comment before it to adhere it
|
| 287 | | // to the preceding text.
|
| 288 | 4 | if (_shouldMoveCommentBeforeSplit(comment.text)) {
|
| 289 | 6 | _chunks.last.allowText();
|
| 290 | | }
|
| 291 | |
|
| 292 | | // The comment follows other text, so we need to decide if it gets a
|
| 293 | | // space before it or not.
|
| 294 | 4 | if (_needsSpaceBeforeComment(comment)) _writeText(" ");
|
| 295 | | } else {
|
| 296 | | // The comment starts a line, so make sure it stays on its own line.
|
| 297 | 2 | _writeHardSplit(
|
| 298 | 2 | flushLeft: comment.flushLeft,
|
| 299 | 4 | isDouble: comment.linesBefore > 1,
|
| 300 | | nest: true);
|
| 301 | | }
|
| 302 | |
|
| 303 | 2 | _writeCommentText(comment);
|
| 304 | |
|
| 305 | 2 | if (comment.selectionStart != null) {
|
| 306 | 5 | startSelectionFromEnd(comment.text.length - comment.selectionStart);
|
| 307 | | }
|
| 308 | |
|
| 309 | 2 | if (comment.selectionEnd != null) {
|
| 310 | 5 | endSelectionFromEnd(comment.text.length - comment.selectionEnd);
|
| 311 | | }
|
| 312 | |
|
| 313 | | // Make sure there is at least one newline after a line comment and allow
|
| 314 | | // one or two after a block comment that has nothing after it.
|
| 315 | | var linesAfter;
|
| 316 | 6 | if (i < comments.length - 1) {
|
| 317 | 6 | linesAfter = comments[i + 1].linesBefore;
|
| 318 | | } else {
|
| 319 | | linesAfter = linesBeforeToken;
|
| 320 | |
|
| 321 | | // Always force a newline after multi-line block comments. Prevents
|
| 322 | | // mistakes like:
|
| 323 | | //
|
| 324 | | // /**
|
| 325 | | // * Some doc comment.
|
| 326 | | // */ someFunction() { ... }
|
| 327 | 8 | if (linesAfter == 0 && comments.last.text.contains("\n")) {
|
| 328 | | linesAfter = 1;
|
| 329 | | }
|
| 330 | | }
|
| 331 | |
|
| 332 | 6 | if (linesAfter > 0) _writeHardSplit(isDouble: linesAfter > 1, nest: true);
|
| 333 | | }
|
| 334 | |
|
| 335 | | // If the comment has text following it (aside from a grouping character),
|
| 336 | | // it needs a trailing space.
|
| 337 | 2 | if (_needsSpaceAfterLastComment(comments, token)) {
|
| 338 | 2 | _pendingWhitespace = Whitespace.space;
|
| 339 | | }
|
| 340 | |
|
| 341 | 2 | preserveNewlines(linesBeforeToken);
|
| 342 | | }
|
| 343 | |
|
| 344 | | /// Writes the text of [comment].
|
| 345 | | ///
|
| 346 | | /// If it's a JavaDoc comment that should be fixed to use `///`, fixes it.
|
| 347 | 2 | void _writeCommentText(SourceComment comment) {
|
| 348 | 6 | if (!_formatter.fixes.contains(StyleFix.docComments)) {
|
| 349 | 4 | _writeText(comment.text);
|
| 350 | | return;
|
| 351 | | }
|
| 352 | |
|
| 353 | | // See if it's a JavaDoc comment.
|
| 354 | 3 | var match = _javaDocComment.firstMatch(comment.text);
|
| 355 | | if (match == null) {
|
| 356 | 2 | _writeText(comment.text);
|
| 357 | | return;
|
| 358 | | }
|
| 359 | |
|
| 360 | | // Remove a leading "*" from the middle lines.
|
| 361 | 3 | var lines = match.group(1).split("\n").toList();
|
| 362 | 4 | for (var i = 1; i < lines.length - 1; i++) {
|
| 363 | 1 | var line = lines[i];
|
| 364 | 2 | var match = _javaDocLine.firstMatch(line);
|
| 365 | | if (match != null) {
|
| 366 | 1 | line = match.group(1);
|
| 367 | | } else {
|
| 368 | | // Note that this may remove deliberate leading whitespace. In tests on
|
| 369 | | // a large corpus, though, I couldn't find examples of that.
|
| 370 | 1 | line = line.trimLeft();
|
| 371 | | }
|
| 372 | 1 | lines[i] = line;
|
| 373 | | }
|
| 374 | |
|
| 375 | | // Trim the first and last lines if empty.
|
| 376 | 4 | if (lines.first.trim().isEmpty) lines.removeAt(0);
|
| 377 | 5 | if (lines.isNotEmpty && lines.last.trim().isEmpty) lines.removeLast();
|
| 378 | |
|
| 379 | | // Don't completely eliminate an empty block comment.
|
| 380 | 2 | if (lines.isEmpty) lines.add("");
|
| 381 | |
|
| 382 | 2 | for (var line in lines) {
|
| 383 | 3 | if (line.isNotEmpty && !line.startsWith(" ")) line = " $line";
|
| 384 | 3 | _writeText("///${line.trimRight()}");
|
| 385 | 1 | _pendingWhitespace = Whitespace.newline;
|
| 386 | 1 | _emitPendingWhitespace();
|
| 387 | | }
|
| 388 | | }
|
| 389 | |
|
| 390 | | /// If the current pending whitespace allows some source discretion, pins
|
| 391 | | /// that down given that the source contains [numLines] newlines at that
|
| 392 | | /// point.
|
| 393 | 2 | void preserveNewlines(int numLines) {
|
| 394 | | // If we didn't know how many newlines the user authored between the last
|
| 395 | | // token and this one, now we do.
|
| 396 | 2 | switch (_pendingWhitespace) {
|
| 397 | 2 | case Whitespace.splitOrNewline:
|
| 398 | 1 | if (numLines > 0) {
|
| 399 | 1 | _pendingWhitespace = Whitespace.nestedNewline;
|
| 400 | | } else {
|
| 401 | 1 | _pendingWhitespace = Whitespace.none;
|
| 402 | 1 | split(space: true);
|
| 403 | | }
|
| 404 | | break;
|
| 405 | |
|
| 406 | 2 | case Whitespace.splitOrTwoNewlines:
|
| 407 | 1 | if (numLines > 1) {
|
| 408 | 1 | _pendingWhitespace = Whitespace.twoNewlines;
|
| 409 | | } else {
|
| 410 | 1 | _pendingWhitespace = Whitespace.none;
|
| 411 | 1 | split(space: true);
|
| 412 | | }
|
| 413 | | break;
|
| 414 | |
|
| 415 | 2 | case Whitespace.oneOrTwoNewlines:
|
| 416 | 2 | if (numLines > 1) {
|
| 417 | 1 | _pendingWhitespace = Whitespace.twoNewlines;
|
| 418 | | } else {
|
| 419 | 2 | _pendingWhitespace = Whitespace.newline;
|
| 420 | | }
|
| 421 | | break;
|
| 422 | | }
|
| 423 | | }
|
| 424 | |
|
| 425 | | /// Creates a new indentation level [spaces] deeper than the current one.
|
| 426 | | ///
|
| 427 | | /// If omitted, [spaces] defaults to [Indent.block].
|
| 428 | 3 | void indent([int spaces]) {
|
| 429 | 6 | _nesting.indent(spaces);
|
| 430 | | }
|
| 431 | |
|
| 432 | | /// Discards the most recent indentation level.
|
| 433 | 2 | void unindent() {
|
| 434 | 4 | _nesting.unindent();
|
| 435 | | }
|
| 436 | |
|
| 437 | | /// Starts a new span with [cost].
|
| 438 | | ///
|
| 439 | | /// Each call to this needs a later matching call to [endSpan].
|
| 440 | 3 | void startSpan([int cost = Cost.normal]) {
|
| 441 | 12 | _openSpans.add(new OpenSpan(_currentChunkIndex, cost));
|
| 442 | | }
|
| 443 | |
|
| 444 | | /// Ends the innermost span.
|
| 445 | 3 | void endSpan() {
|
| 446 | 6 | var openSpan = _openSpans.removeLast();
|
| 447 | |
|
| 448 | | // A span that just covers a single chunk can't be split anyway.
|
| 449 | 3 | var end = _currentChunkIndex;
|
| 450 | 6 | if (openSpan.start == end) return;
|
| 451 | |
|
| 452 | | // Add the span to every chunk that can split it.
|
| 453 | 6 | var span = new Span(openSpan.cost);
|
| 454 | 9 | for (var i = openSpan.start; i < end; i++) {
|
| 455 | 6 | var chunk = _chunks[i];
|
| 456 | 12 | if (!chunk.rule.isHardened) chunk.spans.add(span);
|
| 457 | | }
|
| 458 | | }
|
| 459 | |
|
| 460 | | /// Starts a new [Rule].
|
| 461 | | ///
|
| 462 | | /// If omitted, defaults to a new [Rule].
|
| 463 | 3 | void startRule([Rule rule]) {
|
| 464 | 2 | if (rule == null) rule = new Rule();
|
| 465 | |
|
| 466 | | // If there are any pending lazy rules, start them now so that the proper
|
| 467 | | // stack ordering of rules is maintained.
|
| 468 | 9 | _lazyRules.forEach(_activateRule);
|
| 469 | 6 | _lazyRules.clear();
|
| 470 | |
|
| 471 | 3 | _activateRule(rule);
|
| 472 | | }
|
| 473 | |
|
| 474 | 3 | void _activateRule(Rule rule) {
|
| 475 | | // See if any of the rules that contain this one care if it splits.
|
| 476 | 8 | _rules.forEach((outer) {
|
| 477 | 2 | if (!outer.splitsOnInnerRules) return;
|
| 478 | 2 | rule.imply(outer);
|
| 479 | | });
|
| 480 | 6 | _rules.add(rule);
|
| 481 | | }
|
| 482 | |
|
| 483 | | /// Starts a new [Rule] that comes into play *after* the next whitespace
|
| 484 | | /// (including comments) is written.
|
| 485 | | ///
|
| 486 | | /// This is used for operators who want to start a rule before the first
|
| 487 | | /// operand but not get forced to split if a comment appears before the
|
| 488 | | /// entire expression.
|
| 489 | | ///
|
| 490 | | /// If [rule] is omitted, defaults to a new [Rule].
|
| 491 | 3 | void startLazyRule([Rule rule]) {
|
| 492 | 2 | if (rule == null) rule = new Rule();
|
| 493 | |
|
| 494 | 6 | _lazyRules.add(rule);
|
| 495 | | }
|
| 496 | |
|
| 497 | | /// Ends the innermost rule.
|
| 498 | 3 | void endRule() {
|
| 499 | 6 | if (_lazyRules.isNotEmpty) {
|
| 500 | 2 | _lazyRules.removeLast();
|
| 501 | | } else {
|
| 502 | 6 | _rules.removeLast();
|
| 503 | | }
|
| 504 | | }
|
| 505 | |
|
| 506 | | /// Pre-emptively forces all of the current rules to become hard splits.
|
| 507 | | ///
|
| 508 | | /// This is called by [SourceVisitor] when it can determine that a rule will
|
| 509 | | /// will always be split. Turning it (and the surrounding rules) into hard
|
| 510 | | /// splits lets the writer break the output into smaller pieces for the line
|
| 511 | | /// splitter, which helps performance and avoids failing on very large input.
|
| 512 | | ///
|
| 513 | | /// In particular, it's easy for the visitor to know that collections with a
|
| 514 | | /// large number of items must split. Doing that early avoids crashing the
|
| 515 | | /// splitter when it tries to recurse on huge collection literals.
|
| 516 | 4 | void forceRules() => _handleHardSplit();
|
| 517 | |
|
| 518 | | /// Begins a new expression nesting level [indent] spaces deeper than the
|
| 519 | | /// current one if it splits.
|
| 520 | | ///
|
| 521 | | /// If [indent] is omitted, defaults to [Indent.expression]. If [now] is
|
| 522 | | /// `true`, commits the nesting change immediately instead of waiting until
|
| 523 | | /// after the next chunk of text is written.
|
| 524 | 3 | void nestExpression({int indent, bool now}) {
|
| 525 | | if (now == null) now = false;
|
| 526 | |
|
| 527 | 6 | _nesting.nest(indent);
|
| 528 | 4 | if (now) _nesting.commitNesting();
|
| 529 | | }
|
| 530 | |
|
| 531 | | /// Discards the most recent level of expression nesting.
|
| 532 | | ///
|
| 533 | | /// Expressions that are more nested will get increased indentation when split
|
| 534 | | /// if the previous line has a lower level of nesting.
|
| 535 | | ///
|
| 536 | | /// If [now] is `false`, does not commit the nesting change until after the
|
| 537 | | /// next chunk of text is written.
|
| 538 | 3 | void unnest({bool now}) {
|
| 539 | | if (now == null) now = true;
|
| 540 | |
|
| 541 | 6 | _nesting.unnest();
|
| 542 | 6 | if (now) _nesting.commitNesting();
|
| 543 | | }
|
| 544 | |
|
| 545 | | /// Marks the selection starting point as occurring [fromEnd] characters to
|
| 546 | | /// the left of the end of what's currently been written.
|
| 547 | | ///
|
| 548 | | /// It counts backwards from the end because this is called *after* the chunk
|
| 549 | | /// of text containing the selection has been output.
|
| 550 | 1 | void startSelectionFromEnd(int fromEnd) {
|
| 551 | 2 | assert(_chunks.isNotEmpty);
|
| 552 | 3 | _chunks.last.startSelectionFromEnd(fromEnd);
|
| 553 | | }
|
| 554 | |
|
| 555 | | /// Marks the selection ending point as occurring [fromEnd] characters to the
|
| 556 | | /// left of the end of what's currently been written.
|
| 557 | | ///
|
| 558 | | /// It counts backwards from the end because this is called *after* the chunk
|
| 559 | | /// of text containing the selection has been output.
|
| 560 | 1 | void endSelectionFromEnd(int fromEnd) {
|
| 561 | 2 | assert(_chunks.isNotEmpty);
|
| 562 | 3 | _chunks.last.endSelectionFromEnd(fromEnd);
|
| 563 | | }
|
| 564 | |
|
| 565 | | /// Captures the current nesting level as marking where subsequent block
|
| 566 | | /// arguments should start.
|
| 567 | 3 | void startBlockArgumentNesting() {
|
| 568 | 12 | _blockArgumentNesting.add(_nesting.currentNesting);
|
| 569 | | }
|
| 570 | |
|
| 571 | | /// Releases the last nesting level captured by [startBlockArgumentNesting].
|
| 572 | 3 | void endBlockArgumentNesting() {
|
| 573 | 6 | _blockArgumentNesting.removeLast();
|
| 574 | | }
|
| 575 | |
|
| 576 | | /// Starts a new block as a child of the current chunk.
|
| 577 | | ///
|
| 578 | | /// Nested blocks are handled using their own independent [LineWriter].
|
| 579 | 2 | ChunkBuilder startBlock(Chunk argumentChunk) {
|
| 580 | 4 | var chunk = _chunks.last;
|
| 581 | 2 | chunk.makeBlock(argumentChunk);
|
| 582 | |
|
| 583 | | var builder =
|
| 584 | 10 | new ChunkBuilder._(this, _formatter, _source, chunk.block.chunks);
|
| 585 | |
|
| 586 | | // A block always starts off indented one level.
|
| 587 | 2 | builder.indent();
|
| 588 | |
|
| 589 | | return builder;
|
| 590 | | }
|
| 591 | |
|
| 592 | | /// Ends this [ChunkBuilder], which must have been created by [startBlock()].
|
| 593 | | ///
|
| 594 | | /// Forces the chunk that owns the block to split if it can tell that the
|
| 595 | | /// block contents will always split. It does that by looking for hard splits
|
| 596 | | /// in the block. If [ignoredSplit] is given, that rule will be ignored
|
| 597 | | /// when determining if a block contains a hard split. If [forceSplit] is
|
| 598 | | /// `true`, the block is considered to always split.
|
| 599 | | ///
|
| 600 | | /// Returns the previous writer for the surrounding block.
|
| 601 | 2 | ChunkBuilder endBlock(Rule ignoredSplit, {bool forceSplit}) {
|
| 602 | 2 | _divideChunks();
|
| 603 | |
|
| 604 | | // If we don't already know if the block is going to split, see if it
|
| 605 | | // contains any hard splits or is longer than a page.
|
| 606 | | if (!forceSplit) {
|
| 607 | | var length = 0;
|
| 608 | 4 | for (var chunk in _chunks) {
|
| 609 | 8 | length += chunk.length + chunk.unsplitBlockLength;
|
| 610 | 6 | if (length > _formatter.pageWidth) {
|
| 611 | | forceSplit = true;
|
| 612 | | break;
|
| 613 | | }
|
| 614 | |
|
| 615 | 2 | if (chunk.rule != null &&
|
| 616 | 4 | chunk.rule.isHardened &&
|
| 617 | 4 | chunk.rule != ignoredSplit) {
|
| 618 | | forceSplit = true;
|
| 619 | | break;
|
| 620 | | }
|
| 621 | | }
|
| 622 | | }
|
| 623 | |
|
| 624 | 4 | _parent._endChildBlock(
|
| 625 | 2 | firstFlushLeft: _firstFlushLeft, forceSplit: forceSplit);
|
| 626 | |
|
| 627 | 2 | return _parent;
|
| 628 | | }
|
| 629 | |
|
| 630 | | /// Finishes off the last chunk in a child block of this parent.
|
| 631 | 2 | void _endChildBlock({bool firstFlushLeft, bool forceSplit}) {
|
| 632 | | // If there is a hard newline within the block, force the surrounding rule
|
| 633 | | // for it so that we apply that constraint.
|
| 634 | 2 | if (forceSplit) forceRules();
|
| 635 | |
|
| 636 | | // Write the split for the block contents themselves.
|
| 637 | 4 | var chunk = _chunks.last;
|
| 638 | 12 | chunk.applySplit(rule, _nesting.indentation, _blockArgumentNesting.last,
|
| 639 | | flushLeft: firstFlushLeft);
|
| 640 | |
|
| 641 | 5 | if (chunk.rule.isHardened) _handleHardSplit();
|
| 642 | | }
|
| 643 | |
|
| 644 | | /// Finishes writing and returns a [SourceCode] containing the final output
|
| 645 | | /// and updated selection, if any.
|
| 646 | 3 | SourceCode end() {
|
| 647 | 3 | _writeHardSplit();
|
| 648 | 3 | _divideChunks();
|
| 649 | |
|
| 650 | | if (debug.traceChunkBuilder) {
|
| 651 | 0 | debug.log(debug.green("\nBuilt:"));
|
| 652 | 0 | debug.dumpChunks(0, _chunks);
|
| 653 | 0 | debug.log();
|
| 654 | | }
|
| 655 | |
|
| 656 | 9 | var writer = new LineWriter(_formatter, _chunks);
|
| 657 | 9 | var result = writer.writeLines(_formatter.indent,
|
| 658 | 6 | isCompilationUnit: _source.isCompilationUnit);
|
| 659 | |
|
| 660 | | var selectionStart;
|
| 661 | | var selectionLength;
|
| 662 | 6 | if (_source.selectionStart != null) {
|
| 663 | 1 | selectionStart = result.selectionStart;
|
| 664 | 1 | var selectionEnd = result.selectionEnd;
|
| 665 | |
|
| 666 | | // If we haven't hit the beginning and/or end of the selection yet, they
|
| 667 | | // must be at the very end of the code.
|
| 668 | 1 | if (selectionStart == null) selectionStart = writer.length;
|
| 669 | 1 | if (selectionEnd == null) selectionEnd = writer.length;
|
| 670 | |
|
| 671 | 1 | selectionLength = selectionEnd - selectionStart;
|
| 672 | | }
|
| 673 | |
|
| 674 | 6 | return new SourceCode(result.text,
|
| 675 | 6 | uri: _source.uri,
|
| 676 | 6 | isCompilationUnit: _source.isCompilationUnit,
|
| 677 | | selectionStart: selectionStart,
|
| 678 | | selectionLength: selectionLength);
|
| 679 | | }
|
| 680 | |
|
| 681 | 2 | void preventSplit() {
|
| 682 | 4 | _preventSplitNesting++;
|
| 683 | | }
|
| 684 | |
|
| 685 | 2 | void endPreventSplit() {
|
| 686 | 4 | _preventSplitNesting--;
|
| 687 | 4 | assert(_preventSplitNesting >= 0, "Mismatched calls.");
|
| 688 | | }
|
| 689 | |
|
| 690 | | /// Writes the current pending [Whitespace] to the output, if any.
|
| 691 | | ///
|
| 692 | | /// This should only be called after source lines have been preserved to turn
|
| 693 | | /// any ambiguous whitespace into a concrete choice.
|
| 694 | 3 | void _emitPendingWhitespace() {
|
| 695 | | // Output any pending whitespace first now that we know it won't be
|
| 696 | | // trailing.
|
| 697 | 3 | switch (_pendingWhitespace) {
|
| 698 | 3 | case Whitespace.space:
|
| 699 | 3 | _writeText(" ");
|
| 700 | | break;
|
| 701 | |
|
| 702 | 3 | case Whitespace.newline:
|
| 703 | 2 | _writeHardSplit();
|
| 704 | | break;
|
| 705 | |
|
| 706 | 3 | case Whitespace.nestedNewline:
|
| 707 | 1 | _writeHardSplit(nest: true);
|
| 708 | | break;
|
| 709 | |
|
| 710 | 3 | case Whitespace.newlineFlushLeft:
|
| 711 | 1 | _writeHardSplit(flushLeft: true, nest: true);
|
| 712 | | break;
|
| 713 | |
|
| 714 | 3 | case Whitespace.twoNewlines:
|
| 715 | 3 | _writeHardSplit(isDouble: true);
|
| 716 | | break;
|
| 717 | |
|
| 718 | 3 | case Whitespace.splitOrNewline:
|
| 719 | 3 | case Whitespace.splitOrTwoNewlines:
|
| 720 | 3 | case Whitespace.oneOrTwoNewlines:
|
| 721 | | // We should have pinned these down before getting here.
|
| 722 | 0 | assert(false);
|
| 723 | | break;
|
| 724 | | }
|
| 725 | |
|
| 726 | 3 | _pendingWhitespace = Whitespace.none;
|
| 727 | | }
|
| 728 | |
|
| 729 | | /// Returns `true` if the last chunk is a split that should be moved after the
|
| 730 | | /// comment that is about to be written.
|
| 731 | 2 | bool _shouldMoveCommentBeforeSplit(String comment) {
|
| 732 | | // Not if there is nothing before it.
|
| 733 | 4 | if (_chunks.isEmpty) return false;
|
| 734 | |
|
| 735 | | // Multi-line comments are always pushed to the next line.
|
| 736 | 2 | if (comment.contains("\n")) return false;
|
| 737 | |
|
| 738 | 6 | var text = _chunks.last.text;
|
| 739 | |
|
| 740 | | // A block comment following a comma probably refers to the following item.
|
| 741 | 4 | if (text.endsWith(",") && comment.startsWith("/*")) return false;
|
| 742 | |
|
| 743 | | // If the text before the split is an open grouping character, it looks
|
| 744 | | // better to keep it with the elements than with the bracket itself.
|
| 745 | 6 | return !text.endsWith("(") && !text.endsWith("[") && !text.endsWith("{");
|
| 746 | | }
|
| 747 | |
|
| 748 | | /// Returns `true` if [comment] appears to be a magic generic method comment.
|
| 749 | | ///
|
| 750 | | /// Those get spaced a little differently to look more like real syntax:
|
| 751 | | ///
|
| 752 | | /// int f/*<S, T>*/(int x) => 3;
|
| 753 | 2 | bool _isGenericMethodComment(SourceComment comment) {
|
| 754 | 8 | return comment.text.startsWith("/*<") || comment.text.startsWith("/*=");
|
| 755 | | }
|
| 756 | |
|
| 757 | | /// Returns `true` if a space should be output between the end of the current
|
| 758 | | /// output and the subsequent comment which is about to be written.
|
| 759 | | ///
|
| 760 | | /// This is only called if the comment is trailing text in the unformatted
|
| 761 | | /// source. In most cases, a space will be output to separate the comment
|
| 762 | | /// from what precedes it. This returns false if:
|
| 763 | | ///
|
| 764 | | /// * This comment does begin the line in the output even if it didn't in
|
| 765 | | /// the source.
|
| 766 | | /// * The comment is a block comment immediately following a grouping
|
| 767 | | /// character (`(`, `[`, or `{`). This is to allow `foo(/* comment */)`,
|
| 768 | | /// et. al.
|
| 769 | 2 | bool _needsSpaceBeforeComment(SourceComment comment) {
|
| 770 | | // Not at the start of the file.
|
| 771 | 4 | if (_chunks.isEmpty) return false;
|
| 772 | |
|
| 773 | | // Not at the start of a line.
|
| 774 | 6 | if (!_chunks.last.canAddText) return false;
|
| 775 | |
|
| 776 | 6 | var text = _chunks.last.text;
|
| 777 | 2 | if (text.endsWith("\n")) return false;
|
| 778 | |
|
| 779 | | // Always put a space before line comments.
|
| 780 | 2 | if (comment.isLineComment) return true;
|
| 781 | |
|
| 782 | | // Magic generic method comments like "Foo/*<T>*/" don't get spaces.
|
| 783 | 2 | if (_isGenericMethodComment(comment) &&
|
| 784 | 2 | _trailingIdentifierChar.hasMatch(text)) {
|
| 785 | | return false;
|
| 786 | | }
|
| 787 | |
|
| 788 | | // Block comments do not get a space if following a grouping character.
|
| 789 | 6 | return !text.endsWith("(") && !text.endsWith("[") && !text.endsWith("{");
|
| 790 | | }
|
| 791 | |
|
| 792 | | /// Returns `true` if a space should be output after the last comment which
|
| 793 | | /// was just written and the token that will be written.
|
| 794 | 2 | bool _needsSpaceAfterLastComment(List<SourceComment> comments, String token) {
|
| 795 | | // Not if there are no comments.
|
| 796 | 2 | if (comments.isEmpty) return false;
|
| 797 | |
|
| 798 | | // Not at the beginning of a line.
|
| 799 | 6 | if (!_chunks.last.canAddText) return false;
|
| 800 | |
|
| 801 | | // Magic generic method comments like "Foo/*<T>*/" don't get spaces.
|
| 802 | 5 | if (_isGenericMethodComment(comments.last) && token == "(") {
|
| 803 | | return false;
|
| 804 | | }
|
| 805 | |
|
| 806 | | // Otherwise, it gets a space if the following token is not a delimiter or
|
| 807 | | // the empty string, for EOF.
|
| 808 | 2 | return token != ")" &&
|
| 809 | 2 | token != "]" &&
|
| 810 | 2 | token != "}" &&
|
| 811 | 2 | token != "," &&
|
| 812 | 2 | token != ";" &&
|
| 813 | 2 | token != "";
|
| 814 | | }
|
| 815 | |
|
| 816 | | /// Appends a hard split with the current indentation and nesting (the latter
|
| 817 | | /// only if [nest] is `true`).
|
| 818 | | ///
|
| 819 | | /// If [double] is `true` or `false`, forces a single or double line to be
|
| 820 | | /// output. Otherwise, it is left indeterminate.
|
| 821 | | ///
|
| 822 | | /// If [flushLeft] is `true`, then the split will always cause the next line
|
| 823 | | /// to be at column zero. Otherwise, it uses the normal indentation and
|
| 824 | | /// nesting behavior.
|
| 825 | 3 | void _writeHardSplit({bool isDouble, bool flushLeft, bool nest: false}) {
|
| 826 | | // A hard split overrides any other whitespace.
|
| 827 | 3 | _pendingWhitespace = null;
|
| 828 | 6 | _writeSplit(new Rule.hard(),
|
| 829 | | flushLeft: flushLeft, isDouble: isDouble, nest: nest);
|
| 830 | | }
|
| 831 | |
|
| 832 | | /// Ends the current chunk (if any) with the given split information.
|
| 833 | | ///
|
| 834 | | /// Returns the chunk.
|
| 835 | 3 | Chunk _writeSplit(Rule rule,
|
| 836 | | {bool flushLeft, bool isDouble, bool nest, bool space}) {
|
| 837 | | nest ??= true;
|
| 838 | | space ??= false;
|
| 839 | |
|
| 840 | 6 | if (_chunks.isEmpty) {
|
| 841 | 1 | if (flushLeft != null) _firstFlushLeft = flushLeft;
|
| 842 | |
|
| 843 | | return null;
|
| 844 | | }
|
| 845 | |
|
| 846 | 15 | _chunks.last.applySplit(rule, _nesting.indentation,
|
| 847 | 9 | nest ? _nesting.nesting : new NestingLevel(),
|
| 848 | | flushLeft: flushLeft, isDouble: isDouble, space: space);
|
| 849 | |
|
| 850 | 15 | if (_chunks.last.rule.isHardened) _handleHardSplit();
|
| 851 | 6 | return _chunks.last;
|
| 852 | | }
|
| 853 | |
|
| 854 | | /// Writes [text] to either the current chunk or a new one if the current
|
| 855 | | /// chunk is complete.
|
| 856 | 3 | void _writeText(String text) {
|
| 857 | 15 | if (_chunks.isNotEmpty && _chunks.last.canAddText) {
|
| 858 | 9 | _chunks.last.appendText(text);
|
| 859 | | } else {
|
| 860 | 9 | _chunks.add(new Chunk(text));
|
| 861 | | }
|
| 862 | | }
|
| 863 | |
|
| 864 | | /// Returns true if we can divide the chunks at [index] and line split the
|
| 865 | | /// ones before and after that separately.
|
| 866 | 3 | bool _canDivideAt(int i) {
|
| 867 | | // Don't divide after the last chunk.
|
| 868 | 12 | if (i == _chunks.length - 1) return false;
|
| 869 | |
|
| 870 | 6 | var chunk = _chunks[i];
|
| 871 | 6 | if (!chunk.rule.isHardened) return false;
|
| 872 | 4 | if (chunk.nesting.isNested) return false;
|
| 873 | 2 | if (chunk.isBlock) return false;
|
| 874 | |
|
| 875 | | return true;
|
| 876 | | }
|
| 877 | |
|
| 878 | | /// Pre-processes the chunks after they are done being written by the visitor
|
| 879 | | /// but before they are run through the line splitter.
|
| 880 | | ///
|
| 881 | | /// Marks ranges of chunks that can be line split independently to keep the
|
| 882 | | /// batches we send to [LineSplitter] small.
|
| 883 | 3 | void _divideChunks() {
|
| 884 | | // Harden all of the rules that we know get forced by containing hard
|
| 885 | | // splits, along with all of the other rules they constrain.
|
| 886 | 3 | _hardenRules();
|
| 887 | |
|
| 888 | | // Now that we know where all of the divided chunk sections are, mark the
|
| 889 | | // chunks.
|
| 890 | 12 | for (var i = 0; i < _chunks.length; i++) {
|
| 891 | 12 | _chunks[i].markDivide(_canDivideAt(i));
|
| 892 | | }
|
| 893 | | }
|
| 894 | |
|
| 895 | | /// Hardens the active rules when a hard split occurs within them.
|
| 896 | 3 | void _handleHardSplit() {
|
| 897 | 6 | if (_rules.isEmpty) return;
|
| 898 | |
|
| 899 | | // If the current rule doesn't care, it will "eat" the hard split and no
|
| 900 | | // others will care either.
|
| 901 | 6 | if (!_rules.last.splitsOnInnerRules) return;
|
| 902 | |
|
| 903 | | // Start with the innermost rule. This will traverse the other rules it
|
| 904 | | // constrains.
|
| 905 | 8 | _hardSplitRules.add(_rules.last);
|
| 906 | | }
|
| 907 | |
|
| 908 | | /// Replaces all of the previously hardened rules with hard splits, along
|
| 909 | | /// with every rule that those constrain to also split.
|
| 910 | | ///
|
| 911 | | /// This should only be called after all chunks have been written.
|
| 912 | 3 | void _hardenRules() {
|
| 913 | 6 | if (_hardSplitRules.isEmpty) return;
|
| 914 | |
|
| 915 | 2 | walkConstraints(rule) {
|
| 916 | 2 | rule.harden();
|
| 917 | |
|
| 918 | | // Follow this rule's constraints, recursively.
|
| 919 | 2 | for (var other in rule.constrainedRules) {
|
| 920 | 2 | if (other == rule) continue;
|
| 921 | |
|
| 922 | 2 | if (!other.isHardened &&
|
| 923 | 6 | rule.constrain(rule.fullySplitValue, other) ==
|
| 924 | 2 | other.fullySplitValue) {
|
| 925 | 2 | walkConstraints(other);
|
| 926 | | }
|
| 927 | | }
|
| 928 | | }
|
| 929 | |
|
| 930 | 4 | for (var rule in _hardSplitRules) {
|
| 931 | 2 | walkConstraints(rule);
|
| 932 | | }
|
| 933 | |
|
| 934 | | // Discard spans in hardened chunks since we know for certain they will
|
| 935 | | // split anyway.
|
| 936 | 4 | for (var chunk in _chunks) {
|
| 937 | 6 | if (chunk.rule != null && chunk.rule.isHardened) {
|
| 938 | 4 | chunk.spans.clear();
|
| 939 | | }
|
| 940 | | }
|
| 941 | | }
|
| 942 | | }
|