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 part of dart2js.util; | 5 part of dart2js.util; |
6 | 6 |
7 /// Indentation utility class. Should be used as a mixin in most cases. | 7 /// Indentation utility class. Should be used as a mixin in most cases. |
8 class Indentation { | 8 class Indentation { |
9 /// The current indentation string. | 9 /// The current indentation string. |
10 String get indentation { | 10 String get indentation { |
11 // Lazily add new indentation strings as required. | 11 // Lazily add new indentation strings as required. |
12 for (int i = _indentList.length; i <= _indentLevel; i++) { | 12 for (int i = _indentList.length; i <= _indentLevel; i++) { |
13 _indentList.add(_indentList[i - 1] + indentationUnit); | 13 _indentList.add(_indentList[i - 1] + indentationUnit); |
14 } | 14 } |
15 return _indentList[_indentLevel]; | 15 return _indentList[_indentLevel]; |
16 } | 16 } |
17 | 17 |
18 /// The current indentation level. | 18 /// The current indentation level. |
19 int _indentLevel = 0; | 19 int _indentLevel = 0; |
20 | 20 |
21 /// A cache of all indentation strings used so far. | 21 /// A cache of all indentation strings used so far. |
22 /// Always at least of length 1. | 22 /// Always at least of length 1. |
23 List<String> _indentList = <String>[""]; | 23 List<String> _indentList = <String>[""]; |
24 | 24 |
25 /// The indentation unit, defaulting to two spaces. May be overwritten. | 25 /// The indentation unit, defaulting to two spaces. May be overwritten. |
26 String _indentationUnit = " "; | 26 String _indentationUnit = " "; |
27 String get indentationUnit => _indentationUnit; | 27 String get indentationUnit => _indentationUnit; |
28 set indentationUnit(String value) { | 28 set indentationUnit(String value) { |
29 if (value != _indentationUnit) { | 29 if (value != _indentationUnit) { |
30 _indentationUnit = value; | 30 _indentationUnit = value; |
31 _indentList = <String>[""]; | 31 _indentList = <String>[""]; |
32 } | 32 } |
33 } | 33 } |
34 | 34 |
35 /// Increases the current level of indentation. | 35 /// Increases the current level of indentation. |
36 void indentMore() { | 36 void indentMore() { |
37 _indentLevel++; | 37 _indentLevel++; |
38 } | 38 } |
39 | 39 |
40 /// Decreases the current level of indentation. | 40 /// Decreases the current level of indentation. |
41 void indentLess() { | 41 void indentLess() { |
42 _indentLevel--; | 42 _indentLevel--; |
43 } | 43 } |
44 | 44 |
45 /// Calls [f] with one more indentation level, restoring indentation context | 45 /// Calls [f] with one more indentation level, restoring indentation context |
46 /// upon return of [f] and returning its result. | 46 /// upon return of [f] and returning its result. |
47 indentBlock(Function f) { | 47 indentBlock(Function f) { |
48 indentMore(); | 48 indentMore(); |
49 var result = f(); | 49 var result = f(); |
50 indentLess(); | 50 indentLess(); |
51 return result; | 51 return result; |
52 } | 52 } |
53 } | 53 } |
54 | 54 |
55 abstract class Tagging<N> implements Indentation { | 55 abstract class Tagging<N> implements Indentation { |
56 | |
57 StringBuffer sb = new StringBuffer(); | 56 StringBuffer sb = new StringBuffer(); |
58 Link<String> tagStack = const Link<String>(); | 57 Link<String> tagStack = const Link<String>(); |
59 | 58 |
60 void pushTag(String tag) { | 59 void pushTag(String tag) { |
61 tagStack = tagStack.prepend(tag); | 60 tagStack = tagStack.prepend(tag); |
62 indentMore(); | 61 indentMore(); |
63 } | 62 } |
64 | 63 |
65 String popTag() { | 64 String popTag() { |
66 assert(!tagStack.isEmpty); | 65 assert(!tagStack.isEmpty); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 }); | 136 }); |
138 } | 137 } |
139 | 138 |
140 void addCurrentIndent() { | 139 void addCurrentIndent() { |
141 sb.write(indentation); | 140 sb.write(indentation); |
142 } | 141 } |
143 | 142 |
144 /// Converts a parameter value into a string. | 143 /// Converts a parameter value into a string. |
145 String valueToString(var value) => value; | 144 String valueToString(var value) => value; |
146 } | 145 } |
OLD | NEW |