Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: pkg/analyzer_experimental/lib/src/services/writer.dart

Issue 22542003: analyzer_exp fixes (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 library source_writer; 5 library source_writer;
6 6
7 7
8 class Line { 8 class Line {
9 9
10 final int lineLength; 10 final int lineLength;
11 final tokens = <LineToken>[]; 11 final tokens = <LineToken>[];
12 final bool useTabs; 12 final bool useTabs;
13 final int spacesPerIndent; 13 final int spacesPerIndent;
14 14
15 Line({indent: 0, this.lineLength: 80, this.useTabs: false, 15 Line({indent: 0, this.lineLength: 80, this.useTabs: false,
16 this.spacesPerIndent: 2}) { 16 this.spacesPerIndent: 2}) {
17 if (indent > 0) { 17 if (indent > 0) {
18 _indent(indent); 18 _indent(indent);
19 } 19 }
20 } 20 }
21 21
22 addSpace() { 22 void addSpace() {
pquitslund 2013/08/08 21:20:23 Again, why be explicit here? Like I said, I prefe
23 addSpaces(1); 23 addSpaces(1);
24 } 24 }
25 25
26 addSpaces(int n) { 26 void addSpaces(int n) {
pquitslund 2013/08/08 21:20:23 Ditto.
27 if (n > 0) { 27 if (n > 0) {
28 tokens.add(new SpaceToken(n)); 28 tokens.add(new SpaceToken(n));
29 } 29 }
30 } 30 }
31 31
32 addToken(LineToken token) { 32 void addToken(LineToken token) {
pquitslund 2013/08/08 21:20:23 Ditto.
33 tokens.add(token); 33 tokens.add(token);
34 } 34 }
35 35
36 bool isWhitespace() { 36 bool isWhitespace() => tokens.every((tok) => tok is SpaceToken);
pquitslund 2013/08/08 21:20:23 Sweet!
37 for (var tok in tokens) {
38 if (!(tok is SpaceToken)) {
39 return false;
40 }
41 }
42 return true;
43 }
44 37
45 _indent(int n) { 38 void _indent(int n) {
pquitslund 2013/08/08 21:20:23 Ditto. (And for the rest too.)
46 tokens.add(useTabs ? new TabToken(n) : new SpaceToken(n * spacesPerIndent)); 39 tokens.add(useTabs ? new TabToken(n) : new SpaceToken(n * spacesPerIndent));
47 } 40 }
48 41
49 String toString() { 42 String toString() {
50 var buffer = new StringBuffer(); 43 var buffer = new StringBuffer();
51 for (var tok in tokens) { 44 for (var tok in tokens) {
52 buffer.write(tok.toString()); 45 buffer.write(tok.toString());
53 } 46 }
54 return buffer.toString(); 47 return buffer.toString();
55 } 48 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 final StringBuffer buffer = new StringBuffer(); 82 final StringBuffer buffer = new StringBuffer();
90 Line currentLine; 83 Line currentLine;
91 84
92 final String lineSeparator; 85 final String lineSeparator;
93 int indentCount = 0; 86 int indentCount = 0;
94 87
95 SourceWriter({this.indentCount: 0, this.lineSeparator: '\n'}) { 88 SourceWriter({this.indentCount: 0, this.lineSeparator: '\n'}) {
96 currentLine = new Line(indent: indentCount); 89 currentLine = new Line(indent: indentCount);
97 } 90 }
98 91
99 indent() { 92 void indent() {
100 ++indentCount; 93 ++indentCount;
101 } 94 }
102 95
103 unindent() { 96 void unindent() {
104 --indentCount; 97 --indentCount;
105 } 98 }
106 99
107 print(x) { 100 void print(x) {
108 currentLine.addToken(new LineToken(x)); 101 currentLine.addToken(new LineToken(x));
109 } 102 }
110 103
111 newline() { 104 void newline() {
112 if (currentLine.isWhitespace()) { 105 if (currentLine.isWhitespace()) {
113 currentLine.tokens.clear(); 106 currentLine.tokens.clear();
114 } 107 }
115 currentLine.addToken(new NewlineToken(this.lineSeparator)); 108 currentLine.addToken(new NewlineToken(this.lineSeparator));
116 buffer.write(currentLine.toString()); 109 buffer.write(currentLine.toString());
117 currentLine = new Line(indent: indentCount); 110 currentLine = new Line(indent: indentCount);
118 } 111 }
119 112
120 newlines(int num) { 113 void newlines(int num) {
121 while (num-- > 0) { 114 while (num-- > 0) {
122 newline(); 115 newline();
123 } 116 }
124 } 117 }
125 118
126 space() { 119 void space() {
127 spaces(1); 120 spaces(1);
128 } 121 }
129 122
130 spaces(n) { 123 void spaces(n) {
131 currentLine.addSpaces(n); 124 currentLine.addSpaces(n);
132 } 125 }
133 126
134 println(String s) { 127 void println(String s) {
135 print(s); 128 print(s);
136 newline(); 129 newline();
137 } 130 }
138 131
139 String toString() { 132 String toString() {
140 var source = new StringBuffer(buffer.toString()); 133 var source = new StringBuffer(buffer.toString());
141 if (!currentLine.isWhitespace()) { 134 if (!currentLine.isWhitespace()) {
142 source.write(currentLine); 135 source.write(currentLine);
143 } 136 }
144 return source.toString(); 137 return source.toString();
145 } 138 }
146 139
147 } 140 }
148 141
149 142
150 const SPACE = ' '; 143 const SPACE = ' ';
151 final SPACES = [ 144 const SPACES = const [
152 '', 145 '',
153 ' ', 146 ' ',
154 ' ', 147 ' ',
155 ' ', 148 ' ',
156 ' ', 149 ' ',
157 ' ', 150 ' ',
158 ' ', 151 ' ',
159 ' ', 152 ' ',
160 ' ', 153 ' ',
161 ' ', 154 ' ',
162 ' ', 155 ' ',
163 ' ', 156 ' ',
164 ' ', 157 ' ',
165 ' ', 158 ' ',
166 ' ', 159 ' ',
167 ' ', 160 ' ',
168 ' ', 161 ' ',
169 ]; 162 ];
170 final TABS = [ 163 const TABS = const [
171 '', 164 '',
172 '\t', 165 '\t',
173 '\t\t', 166 '\t\t',
174 '\t\t\t', 167 '\t\t\t',
175 '\t\t\t\t', 168 '\t\t\t\t',
176 '\t\t\t\t\t', 169 '\t\t\t\t\t',
177 '\t\t\t\t\t\t', 170 '\t\t\t\t\t\t',
178 '\t\t\t\t\t\t\t', 171 '\t\t\t\t\t\t\t',
179 '\t\t\t\t\t\t\t\t', 172 '\t\t\t\t\t\t\t\t',
180 '\t\t\t\t\t\t\t\t\t', 173 '\t\t\t\t\t\t\t\t\t',
(...skipping 12 matching lines...) Expand all
193 186
194 String getTabs(int n) => n < TABS.length ? TABS[n] : repeat('\t', n); 187 String getTabs(int n) => n < TABS.length ? TABS[n] : repeat('\t', n);
195 188
196 String repeat(String ch, int times) { 189 String repeat(String ch, int times) {
197 var sb = new StringBuffer(); 190 var sb = new StringBuffer();
198 for (var i = 0; i < times; ++i) { 191 for (var i = 0; i < times; ++i) {
199 sb.write(ch); 192 sb.write(ch);
200 } 193 }
201 return sb.toString(); 194 return sb.toString();
202 } 195 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698