OLD | NEW |
| (Empty) |
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. | |
27 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. | |
40 int get totalUsedIndent => _totalUsedIndent; | |
41 int _totalUsedIndent; | |
42 | |
43 bool get isNested => _parent != null; | |
44 | |
45 NestingLevel() : indent = 0; | |
46 | |
47 NestingLevel._(this._parent, this.indent); | |
48 | |
49 /// Creates a new deeper level of nesting indented [spaces] more characters | |
50 /// that the outer level. | |
51 NestingLevel nest(int spaces) => new NestingLevel._(this, spaces); | |
52 | |
53 /// Clears the previously calculated total indent of this nesting level. | |
54 void clearTotalUsedIndent() { | |
55 _totalUsedIndent = null; | |
56 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. | |
61 void refreshTotalUsedIndent(Set<NestingLevel> usedNesting) { | |
62 if (_totalUsedIndent != null) return; | |
63 | |
64 _totalUsedIndent = 0; | |
65 | |
66 if (_parent != null) { | |
67 _parent.refreshTotalUsedIndent(usedNesting); | |
68 _totalUsedIndent += _parent.totalUsedIndent; | |
69 } | |
70 | |
71 if (usedNesting.contains(this)) _totalUsedIndent += indent; | |
72 } | |
73 | |
74 String toString() { | |
75 if (_parent == null) return indent.toString(); | |
76 return "$parent:$indent"; | |
77 } | |
78 } | |
OLD | NEW |