| 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 |
| 11 final tokens = <LineToken>[]; | 11 final List<LineToken> tokens = <LineToken>[]; |
| 12 final bool useTabs; | 12 final bool useTabs; |
| 13 final int spacesPerIndent; | 13 final int spacesPerIndent; |
| 14 final int indent; | 14 final int indentLevel; |
| 15 final LinePrinter printer; | 15 final LinePrinter printer; |
| 16 | 16 |
| 17 Line({this.indent: 0, this.useTabs: false, this.spacesPerIndent: 2, | 17 Line({this.indentLevel: 0, this.useTabs: false, this.spacesPerIndent: 2, |
| 18 this.printer: const SimpleLinePrinter()}) { | 18 this.printer: const SimpleLinePrinter()}) { |
| 19 if (indent > 0) { | 19 if (indentLevel > 0) { |
| 20 _indent(indent); | 20 indent(indentLevel); |
| 21 } | 21 } |
| 22 } | 22 } |
| 23 | 23 |
| 24 void addSpace() { | 24 void addSpace() { |
| 25 addSpaces(1); | 25 addSpaces(1); |
| 26 } | 26 } |
| 27 | 27 |
| 28 void addSpaces(int n, {breakWeight: DEFAULT_SPACE_WEIGHT}) { | 28 void addSpaces(int n, {breakWeight: DEFAULT_SPACE_WEIGHT}) { |
| 29 if (n > 0) { | 29 if (n > 0) { |
| 30 tokens.add(new SpaceToken(n, breakWeight: breakWeight)); | 30 tokens.add(new SpaceToken(n, breakWeight: breakWeight)); |
| 31 } | 31 } |
| 32 } | 32 } |
| 33 | 33 |
| 34 void addToken(LineToken token) { | 34 void addToken(LineToken token) { |
| 35 tokens.add(token); | 35 tokens.add(token); |
| 36 } | 36 } |
| 37 | 37 |
| 38 bool isEmpty() => tokens.isEmpty; |
| 39 |
| 38 bool isWhitespace() => tokens.every((tok) => tok is SpaceToken); | 40 bool isWhitespace() => tokens.every((tok) => tok is SpaceToken); |
| 39 | 41 |
| 40 void _indent(int n) { | 42 void indent(int n) { |
| 41 tokens.add(useTabs ? new TabToken(n) : new SpaceToken(n * spacesPerIndent)); | 43 tokens.insert(0, |
| 44 useTabs ? new TabToken(n) : new SpaceToken(n * spacesPerIndent)); |
| 42 } | 45 } |
| 43 | 46 |
| 44 String toString() => printer.printLine(this); | 47 String toString() => printer.printLine(this); |
| 45 | 48 |
| 46 } | 49 } |
| 47 | 50 |
| 48 | 51 |
| 49 /// Base class for line printers | 52 /// Base class for line printers |
| 50 abstract class LinePrinter { | 53 abstract class LinePrinter { |
| 51 | 54 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 72 if (indenter == null) { | 75 if (indenter == null) { |
| 73 indenter = NO_OP_INDENTER; | 76 indenter = NO_OP_INDENTER; |
| 74 } | 77 } |
| 75 } | 78 } |
| 76 | 79 |
| 77 String printLine(Line line) { | 80 String printLine(Line line) { |
| 78 var buf = new StringBuffer(); | 81 var buf = new StringBuffer(); |
| 79 var chunks = breakLine(line); | 82 var chunks = breakLine(line); |
| 80 for (var i = 0; i < chunks.length; ++i) { | 83 for (var i = 0; i < chunks.length; ++i) { |
| 81 if (i > 0) { | 84 if (i > 0) { |
| 82 buf.write(indent(chunks[i], line.indent)); | 85 buf.write(indent(chunks[i], line.indentLevel)); |
| 83 } else { | 86 } else { |
| 84 buf.write(chunks[i]); | 87 buf.write(chunks[i]); |
| 85 } | 88 } |
| 86 } | 89 } |
| 87 return buf.toString(); | 90 return buf.toString(); |
| 88 } | 91 } |
| 89 | 92 |
| 90 String indent(Chunk chunk, int level) => | 93 String indent(Chunk chunk, int level) => |
| 91 '\n' + indenter(level + 2) + chunk.toString(); | 94 '\n' + indenter(level + 2) + chunk.toString(); |
| 92 | 95 |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 LineToken _lastToken; | 305 LineToken _lastToken; |
| 303 | 306 |
| 304 SourceWriter({this.indentCount: 0, this.lineSeparator: NEW_LINE, | 307 SourceWriter({this.indentCount: 0, this.lineSeparator: NEW_LINE, |
| 305 bool useTabs: false, int spacesPerIndent: 2, int maxLineLength: 80}) { | 308 bool useTabs: false, int spacesPerIndent: 2, int maxLineLength: 80}) { |
| 306 if (maxLineLength > 0) { | 309 if (maxLineLength > 0) { |
| 307 linePrinter = new SimpleLineBreaker(maxLineLength, (n) => | 310 linePrinter = new SimpleLineBreaker(maxLineLength, (n) => |
| 308 getIndentString(n, useTabs: useTabs, spacesPerIndent: spacesPerIndent)
); | 311 getIndentString(n, useTabs: useTabs, spacesPerIndent: spacesPerIndent)
); |
| 309 } else { | 312 } else { |
| 310 linePrinter = new SimpleLinePrinter(); | 313 linePrinter = new SimpleLinePrinter(); |
| 311 } | 314 } |
| 312 currentLine = new Line(indent: indentCount, printer: linePrinter); | 315 currentLine = newLine(); |
| 313 } | 316 } |
| 314 | 317 |
| 315 LineToken get lastToken => _lastToken; | 318 LineToken get lastToken => _lastToken; |
| 316 | 319 |
| 317 _addToken(LineToken token) { | 320 _addToken(LineToken token) { |
| 318 _lastToken = token; | 321 _lastToken = token; |
| 319 currentLine.addToken(token); | 322 currentLine.addToken(token); |
| 320 } | 323 } |
| 321 | 324 |
| 322 void indent() { | 325 void indent() { |
| 323 ++indentCount; | 326 ++indentCount; |
| 327 // Rather than fiddle with deletions/insertions just start fresh |
| 328 if (currentLine.isWhitespace()) { |
| 329 currentLine = newLine(); |
| 330 } |
| 324 } | 331 } |
| 325 | 332 |
| 326 void newline() { | 333 void newline() { |
| 327 if (currentLine.isWhitespace()) { | 334 if (currentLine.isWhitespace()) { |
| 328 currentLine.tokens.clear(); | 335 currentLine.tokens.clear(); |
| 329 } | 336 } |
| 330 _addToken(new NewlineToken(this.lineSeparator)); | 337 _addToken(new NewlineToken(this.lineSeparator)); |
| 331 buffer.write(currentLine.toString()); | 338 buffer.write(currentLine.toString()); |
| 332 currentLine = new Line(indent: indentCount, printer: linePrinter); | 339 currentLine = newLine(); |
| 333 } | 340 } |
| 334 | 341 |
| 335 void newlines(int num) { | 342 void newlines(int num) { |
| 336 while (num-- > 0) { | 343 while (num-- > 0) { |
| 337 newline(); | 344 newline(); |
| 338 } | 345 } |
| 339 } | 346 } |
| 340 | 347 |
| 341 void print(x) { | 348 void write(x) { |
| 342 _addToken(new LineToken(x)); | 349 _addToken(new LineToken(x)); |
| 343 } | 350 } |
| 344 | 351 |
| 345 void println(String s) { | 352 void writeln(String s) { |
| 346 print(s); | 353 write(s); |
| 347 newline(); | 354 newline(); |
| 348 } | 355 } |
| 349 | 356 |
| 350 void space() { | 357 void space() { |
| 351 spaces(1); | 358 spaces(1); |
| 352 } | 359 } |
| 353 | 360 |
| 354 void spaces(n, {breakWeight: DEFAULT_SPACE_WEIGHT}) { | 361 void spaces(n, {breakWeight: DEFAULT_SPACE_WEIGHT}) { |
| 355 currentLine.addSpaces(n, breakWeight: breakWeight); | 362 currentLine.addSpaces(n, breakWeight: breakWeight); |
| 356 } | 363 } |
| 357 | 364 |
| 358 void unindent() { | 365 void unindent() { |
| 359 --indentCount; | 366 --indentCount; |
| 367 // Rather than fiddle with deletions/insertions just start fresh |
| 368 if (currentLine.isWhitespace()) { |
| 369 currentLine = newLine(); |
| 370 } |
| 360 } | 371 } |
| 361 | 372 |
| 373 Line newLine() => new Line(indentLevel: indentCount, printer: linePrinter); |
| 374 |
| 362 String toString() { | 375 String toString() { |
| 363 var source = new StringBuffer(buffer.toString()); | 376 var source = new StringBuffer(buffer.toString()); |
| 364 if (!currentLine.isWhitespace()) { | 377 if (!currentLine.isWhitespace()) { |
| 365 source.write(currentLine); | 378 source.write(currentLine); |
| 366 } | 379 } |
| 367 return source.toString(); | 380 return source.toString(); |
| 368 } | 381 } |
| 369 | 382 |
| 370 } | 383 } |
| 371 | 384 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 | 430 |
| 418 String getTabs(int n) => n < TABS.length ? TABS[n] : repeat('\t', n); | 431 String getTabs(int n) => n < TABS.length ? TABS[n] : repeat('\t', n); |
| 419 | 432 |
| 420 String repeat(String ch, int times) { | 433 String repeat(String ch, int times) { |
| 421 var sb = new StringBuffer(); | 434 var sb = new StringBuffer(); |
| 422 for (var i = 0; i < times; ++i) { | 435 for (var i = 0; i < times; ++i) { |
| 423 sb.write(ch); | 436 sb.write(ch); |
| 424 } | 437 } |
| 425 return sb.toString(); | 438 return sb.toString(); |
| 426 } | 439 } |
| OLD | NEW |