| OLD | NEW |
| 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 | 8 |
| 9 class Line { | 9 class Line { |
| 10 | 10 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 void addSpaces(int n, {breakWeight: DEFAULT_SPACE_WEIGHT}) { | 28 void addSpaces(int n, {breakWeight: DEFAULT_SPACE_WEIGHT}) { |
| 29 tokens.add(new SpaceToken(n, breakWeight: breakWeight)); | 29 tokens.add(new SpaceToken(n, breakWeight: breakWeight)); |
| 30 } | 30 } |
| 31 | 31 |
| 32 void addToken(LineToken token) { | 32 void addToken(LineToken token) { |
| 33 tokens.add(token); | 33 tokens.add(token); |
| 34 } | 34 } |
| 35 | 35 |
| 36 bool isEmpty() => tokens.isEmpty; | 36 bool isEmpty() => tokens.isEmpty; |
| 37 | 37 |
| 38 bool isWhitespace() => tokens.every((tok) => tok is SpaceToken); | 38 bool isWhitespace() => tokens.every( |
| 39 (tok) => tok is SpaceToken || tok is TabToken); |
| 39 | 40 |
| 40 void indent(int n) { | 41 void indent(int n) { |
| 41 tokens.insert(0, | 42 tokens.insert(0, |
| 42 useTabs ? new TabToken(n) : new SpaceToken(n * spacesPerIndent)); | 43 useTabs ? new TabToken(n) : new SpaceToken(n * spacesPerIndent)); |
| 43 } | 44 } |
| 44 | 45 |
| 45 String toString() => printer.printLine(this); | 46 String toString() => printer.printLine(this); |
| 46 | 47 |
| 47 } | 48 } |
| 48 | 49 |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 } | 292 } |
| 292 | 293 |
| 293 | 294 |
| 294 class SourceWriter { | 295 class SourceWriter { |
| 295 | 296 |
| 296 final StringBuffer buffer = new StringBuffer(); | 297 final StringBuffer buffer = new StringBuffer(); |
| 297 Line currentLine; | 298 Line currentLine; |
| 298 | 299 |
| 299 final String lineSeparator; | 300 final String lineSeparator; |
| 300 int indentCount = 0; | 301 int indentCount = 0; |
| 302 final int spacesPerIndent; |
| 303 final bool useTabs; |
| 301 | 304 |
| 302 LinePrinter linePrinter; | 305 LinePrinter linePrinter; |
| 303 LineToken _lastToken; | 306 LineToken _lastToken; |
| 304 | 307 |
| 305 SourceWriter({this.indentCount: 0, this.lineSeparator: NEW_LINE, | 308 SourceWriter({this.indentCount: 0, this.lineSeparator: NEW_LINE, |
| 306 bool useTabs: false, int spacesPerIndent: 2, int maxLineLength: 80}) { | 309 this.useTabs: false, this.spacesPerIndent: 2, int maxLineLength: 80}) { |
| 307 if (maxLineLength > 0) { | 310 if (maxLineLength > 0) { |
| 308 linePrinter = new SimpleLineBreaker(maxLineLength, (n) => | 311 linePrinter = new SimpleLineBreaker(maxLineLength, (n) => |
| 309 getIndentString(n, useTabs: useTabs, spacesPerIndent: spacesPerIndent)
); | 312 getIndentString(n, useTabs: useTabs, spacesPerIndent: spacesPerIndent)
); |
| 310 } else { | 313 } else { |
| 311 linePrinter = new SimpleLinePrinter(); | 314 linePrinter = new SimpleLinePrinter(); |
| 312 } | 315 } |
| 313 currentLine = newLine(); | 316 currentLine = newLine(); |
| 314 } | 317 } |
| 315 | 318 |
| 316 LineToken get lastToken => _lastToken; | 319 LineToken get lastToken => _lastToken; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 } | 364 } |
| 362 | 365 |
| 363 void unindent() { | 366 void unindent() { |
| 364 --indentCount; | 367 --indentCount; |
| 365 // Rather than fiddle with deletions/insertions just start fresh | 368 // Rather than fiddle with deletions/insertions just start fresh |
| 366 if (currentLine.isWhitespace()) { | 369 if (currentLine.isWhitespace()) { |
| 367 currentLine = newLine(); | 370 currentLine = newLine(); |
| 368 } | 371 } |
| 369 } | 372 } |
| 370 | 373 |
| 371 Line newLine() => new Line(indentLevel: indentCount, printer: linePrinter); | 374 Line newLine() => new Line(indentLevel: indentCount, useTabs: useTabs, |
| 375 spacesPerIndent: spacesPerIndent, printer: linePrinter); |
| 372 | 376 |
| 373 String toString() { | 377 String toString() { |
| 374 var source = new StringBuffer(buffer.toString()); | 378 var source = new StringBuffer(buffer.toString()); |
| 375 if (!currentLine.isWhitespace()) { | 379 if (!currentLine.isWhitespace()) { |
| 376 source.write(currentLine); | 380 source.write(currentLine); |
| 377 } | 381 } |
| 378 return source.toString(); | 382 return source.toString(); |
| 379 } | 383 } |
| 380 | 384 |
| 381 } | 385 } |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 | 432 |
| 429 String getTabs(int n) => n < TABS.length ? TABS[n] : repeat('\t', n); | 433 String getTabs(int n) => n < TABS.length ? TABS[n] : repeat('\t', n); |
| 430 | 434 |
| 431 String repeat(String ch, int times) { | 435 String repeat(String ch, int times) { |
| 432 var sb = new StringBuffer(); | 436 var sb = new StringBuffer(); |
| 433 for (var i = 0; i < times; ++i) { | 437 for (var i = 0; i < times; ++i) { |
| 434 sb.write(ch); | 438 sb.write(ch); |
| 435 } | 439 } |
| 436 return sb.toString(); | 440 return sb.toString(); |
| 437 } | 441 } |
| OLD | NEW |