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_builder; | |
6 | |
7 import 'nesting_level.dart'; | |
8 import 'whitespace.dart'; | |
9 | |
10 /// Keeps track of expression nesting while the source code is being visited | |
11 /// and the chunks are being built. | |
12 class NestingBuilder { | |
13 /// The expression nesting levels and block indentation levels. | |
14 /// | |
15 /// This is tracked as a stack of [_IndentLevel]s. Each element in the stack | |
16 /// represents a level of block indentation. It's stored as a stack because | |
17 /// expressions may contain blocks which in turn contain other expressions. | |
18 /// The nesting level of the inner expressions are unrelated to the | |
19 /// surrounding ones. For example: | |
20 /// | |
21 /// outer(invocation(() { | |
22 /// inner(lambda()); | |
23 /// })); | |
24 /// | |
25 /// When writing `inner(lambda())`, we need to track its nesting level. At | |
26 /// the same time, when the lambda is done, we need to return to the nesting | |
27 /// level of `outer(invocation(...`. | |
28 // TODO(rnystrom): I think this is no longer true now that blocks are handled | |
29 // as separate nested chunks. Once cascades use expression nesting, we may | |
30 // be able to just store a single nesting depth in NestingBuilder. | |
31 /// | |
32 /// Has an implicit entry for top-most expression nesting outside of any | |
33 /// block for things like wrapped directives. | |
34 final List<_IndentLevel> _stack = [new _IndentLevel(0)]; | |
35 | |
36 /// When not `null`, the nesting level of the current innermost block after | |
37 /// the next token is written. | |
38 /// | |
39 /// When the nesting level is increased, we don't want it to take effect until | |
40 /// after at least one token has been written. That ensures that comments | |
41 /// appearing before the first token are correctly indented. For example, a | |
42 /// binary operator expression increases the nesting before the first operand | |
43 /// to ensure any splits within the left operand are handled correctly. If we | |
44 /// changed the nesting level immediately, then code like: | |
45 /// | |
46 /// { | |
47 /// // comment | |
48 /// foo + bar; | |
49 /// } | |
50 /// | |
51 /// would incorrectly get indented because the line comment adds a split which | |
52 /// would take the nesting level of the binary operator into account even | |
53 /// though we haven't written any of its tokens yet. | |
54 NestingLevel _pendingNesting; | |
55 | |
56 /// The current number of characters of block indentation. | |
57 int get indentation => _stack.last.indent; | |
58 | |
59 /// The nesting depth of the current inner-most block. | |
60 NestingLevel get nesting => _stack.last.nesting; | |
61 | |
62 /// The nesting depth of the current inner-most block, including any pending | |
63 /// nesting. | |
64 NestingLevel get currentNesting => | |
65 _pendingNesting != null ? _pendingNesting : _stack.last.nesting; | |
66 | |
67 /// The top "nesting level" that represents no expression nesting for the | |
68 /// current block. | |
69 NestingLevel get blockNesting { | |
70 // Walk the nesting levels until we bottom out. | |
71 var result = nesting; | |
72 while (result.parent != null) { | |
73 result = result.parent; | |
74 } | |
75 return result; | |
76 } | |
77 | |
78 /// Creates a new indentation level [spaces] deeper than the current one. | |
79 /// | |
80 /// If omitted, [spaces] defaults to [Indent.block]. | |
81 void indent([int spaces]) { | |
82 if (spaces == null) spaces = Indent.block; | |
83 | |
84 assert(_pendingNesting == null); | |
85 | |
86 _stack.add(new _IndentLevel(_stack.last.indent + spaces)); | |
87 } | |
88 | |
89 /// Discards the most recent indentation level. | |
90 void unindent() { | |
91 assert(_pendingNesting == null); | |
92 _stack.removeLast(); | |
93 } | |
94 | |
95 /// Begins a new expression nesting level [indent] deeper than the current | |
96 /// one if it splits. | |
97 /// | |
98 /// If [indent] is omitted, defaults to [Indent.expression]. | |
99 void nest([int indent]) { | |
100 if (indent == null) indent = Indent.expression; | |
101 | |
102 if (_pendingNesting != null) { | |
103 _pendingNesting = _pendingNesting.nest(indent); | |
104 } else { | |
105 _pendingNesting = nesting.nest(indent); | |
106 } | |
107 } | |
108 | |
109 /// Discards the most recent level of expression nesting. | |
110 /// | |
111 /// Expressions that are more nested will get increased indentation when split | |
112 /// if the previous line has a lower level of nesting. | |
113 void unnest() { | |
114 // By the time the nesting is done, it should have emitted some text and | |
115 // not be pending anymore. | |
116 assert(_pendingNesting == null); | |
117 | |
118 _setNesting(nesting.parent); | |
119 } | |
120 | |
121 /// Applies any pending nesting now that we are ready for it to take effect. | |
122 void commitNesting() { | |
123 if (_pendingNesting == null) return; | |
124 | |
125 _setNesting(_pendingNesting); | |
126 _pendingNesting = null; | |
127 } | |
128 | |
129 /// Sets the nesting level of the innermost block to [level]. | |
130 void _setNesting(NestingLevel level) { | |
131 _stack.last.nesting = level; | |
132 } | |
133 } | |
134 | |
135 /// A level of block nesting. | |
136 /// | |
137 /// This represents indentation changes that typically occur at statement or | |
138 /// block boundaries. | |
139 class _IndentLevel { | |
140 /// The number of spaces of indentation at this level. | |
141 final int indent; | |
142 | |
143 /// The current expression nesting in this indentation level. | |
144 NestingLevel nesting = new NestingLevel(); | |
145 | |
146 _IndentLevel(this.indent); | |
147 } | |
OLD | NEW |