OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011, 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 class IndentingWriter implements Writer { |
| 6 IndentingWriter(String this._indentSequence, Writer this._writer) |
| 7 : _currentIndent = ""; |
| 8 |
| 9 /* |
| 10 * nice shorthand for adding content |
| 11 */ |
| 12 void addBlock(String start, String end, void body()) { |
| 13 println(start); |
| 14 indent(); |
| 15 body(); |
| 16 outdent(); |
| 17 println(end); |
| 18 } |
| 19 /* |
| 20 * Shift the left-most character of new lines to the right by one |
| 21 * indentation stop (as set in the constructor). |
| 22 */ |
| 23 void indent() { |
| 24 _currentIndent = _currentIndent.concat(_indentSequence); |
| 25 } |
| 26 |
| 27 /* |
| 28 * Shift the left-most character of new lines to the left by one |
| 29 * indentation stop (as set in the constructor). |
| 30 */ |
| 31 void outdent() { |
| 32 _currentIndent = _currentIndent.substring(0, |
| 33 Math.max(0,_currentIndent.length - _indentSequence.length)); |
| 34 } |
| 35 |
| 36 void print(String stringToPrint) { |
| 37 if (_currentLine === null) { |
| 38 _currentLine = new StringBuffer(); |
| 39 } |
| 40 _currentLine.add(stringToPrint); |
| 41 } |
| 42 |
| 43 /* |
| 44 * Add the specified line to the output buffer with the current |
| 45 * level of indent. If the input string is multiple lines, |
| 46 * split them and apply the indent to each line. |
| 47 * |
| 48 * Lines are effectively joined with new-lines (e.g. not appended |
| 49 * to every interim line). |
| 50 */ |
| 51 void println([String out = null]) { |
| 52 // TODO - not clear how this plays cross-platform. |
| 53 // more sophisticated patterns don't seem to match. |
| 54 String string; |
| 55 if (_currentLine !== null) { |
| 56 string = _currentLine.add(out === null ? "" : out).toString(); |
| 57 _currentLine = null; |
| 58 } else { |
| 59 string = out === null ? "" : out; |
| 60 } |
| 61 |
| 62 List<String> lines = string.split("\n"); |
| 63 for (String line in lines) { |
| 64 if (line.length > 0) _writer.print(_currentIndent); |
| 65 _writer.println(line); |
| 66 } |
| 67 } |
| 68 |
| 69 /* |
| 70 * Add a blank line to the output buffer. |
| 71 */ |
| 72 void blankLine() { |
| 73 println(""); |
| 74 } |
| 75 |
| 76 /* |
| 77 * Push out the current line (see print()) if non-empty. |
| 78 */ |
| 79 void flushLine() { |
| 80 if (_currentLine !== null && _currentLine.length > 0) println(""); |
| 81 } |
| 82 |
| 83 StringBuffer _currentLine; |
| 84 Writer _writer; |
| 85 String _currentIndent; |
| 86 String _indentSequence; |
| 87 } |
OLD | NEW |