Coverage report for lib/src/nesting_level.dart

Line coverage: 16 / 19 (84.2%)

All files > lib/src/nesting_level.dart

1
// Copyright (c) 2015, 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.nesting_level;
6
7
import 'fast_hash.dart';
8
9
/// A single level of expression nesting.
10
///
11
/// When a line is split in the middle of an expression, this tracks the
12
/// context of where in the expression that split occurs. It ensures that the
13
/// [LineSplitter] obeys the expression nesting when deciding what column to
14
/// start lines at when split inside an expression.
15
///
16
/// Each instance of this represents a single level of expression nesting. If we
17
/// split at to chunks with different levels of nesting, the splitter ensures
18
/// they each get assigned to different columns.
19
///
20
/// In addition, each level has an indent. This is the number of spaces it is
21
/// indented relative to the outer expression. It's almost always
22
/// [Indent.expression], but cascades are special magic snowflakes and use
23
/// [Indent.cascade].
24
class NestingLevel extends FastHash {
25
  /// The nesting level surrounding this one, or `null` if this is represents
26
  /// top level code in a block.
276
  NestingLevel get parent => _parent;
28
  NestingLevel _parent;
29
30
  /// The number of characters that this nesting level is indented relative to
31
  /// the containing level.
32
  ///
33
  /// Normally, this is [Indent.expression], but cascades use [Indent.cascade].
34
  final int indent;
35
36
  /// The total number of characters of indentation from this level and all of
37
  /// its parents, after determining which nesting levels are actually used.
38
  ///
39
  /// This is only valid during line splitting.
404
  int get totalUsedIndent => _totalUsedIndent;
41
  int _totalUsedIndent;
42
434
  bool get isNested => _parent != null;
44
453
  NestingLevel() : indent = 0;
46
473
  NestingLevel._(this._parent, this.indent);
48
49
  /// Creates a new deeper level of nesting indented [spaces] more characters
50
  /// that the outer level.
516
  NestingLevel nest(int spaces) => new NestingLevel._(this, spaces);
52
53
  /// Clears the previously calculated total indent of this nesting level.
542
  void clearTotalUsedIndent() {
552
    _totalUsedIndent = null;
566
    if (_parent != null) _parent.clearTotalUsedIndent();
57
  }
58
59
  /// Calculates the total amount of indentation from this nesting level and
60
  /// all of its parents assuming only [usedNesting] levels are in use.
612
  void refreshTotalUsedIndent(Set<NestingLevel> usedNesting) {
622
    if (_totalUsedIndent != null) return;
63
642
    _totalUsedIndent = 0;
65
662
    if (_parent != null) {
674
      _parent.refreshTotalUsedIndent(usedNesting);
688
      _totalUsedIndent += _parent.totalUsedIndent;
69
    }
70
718
    if (usedNesting.contains(this)) _totalUsedIndent += indent;
72
  }
73
740
  String toString() {
750
    if (_parent == null) return indent.toString();
760
    return "$parent:$indent";
77
  }
78
}