| 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 part of protoc; | 5 part of protoc; |
| 6 | 6 |
| 7 class IndentingWriter implements Writer { | 7 class IndentingWriter implements Writer { |
| 8 final String _indentSequence; | 8 final String _indentSequence; |
| 9 final Writer _writer; | 9 final Writer _writer; |
| 10 final StringBuffer _currentText = new StringBuffer(); | 10 final StringBuffer _currentText = new StringBuffer(); |
| 11 String _currentIndent = ''; | 11 String _currentIndent = ''; |
| 12 | 12 |
| 13 IndentingWriter(this._indentSequence, this._writer); | 13 IndentingWriter(this._indentSequence, this._writer); |
| 14 | 14 |
| 15 void addBlock(String start, String end, void body()) { | 15 void addBlock(String start, String end, void body(), {endWithNewline: true}) { |
| 16 println(start); | 16 println(start); |
| 17 var oldIndent = _currentIndent; | 17 var oldIndent = _currentIndent; |
| 18 _currentIndent = '$_currentIndent$_indentSequence'; | 18 _currentIndent = '$_currentIndent$_indentSequence'; |
| 19 body(); | 19 body(); |
| 20 _currentIndent = oldIndent; | 20 _currentIndent = oldIndent; |
| 21 println(end); | 21 if (endWithNewline) { |
| 22 println(end); |
| 23 } else { |
| 24 print(end); |
| 25 } |
| 22 } | 26 } |
| 23 | 27 |
| 24 void print(String stringToPrint) { | 28 void print(String stringToPrint) { |
| 25 _currentText.write(stringToPrint); | 29 _currentText.write(stringToPrint); |
| 26 } | 30 } |
| 27 | 31 |
| 28 /* | 32 /* |
| 29 * Add the specified line to the output buffer with the current | 33 * Add the specified line to the output buffer with the current |
| 30 * level of indent. | 34 * level of indent. |
| 31 */ | 35 */ |
| 32 void println([String out = '']) { | 36 void println([String out = '']) { |
| 33 // TODO - not clear how this plays cross-platform. | 37 // TODO - not clear how this plays cross-platform. |
| 34 // more sophisticated patterns don't seem to match. | 38 // more sophisticated patterns don't seem to match. |
| 35 print(out); | 39 print(out); |
| 36 for (String line in _currentText.toString().split('\n')) { | 40 for (String line in _currentText.toString().split('\n')) { |
| 37 if (!line.isEmpty) _writer.print(_currentIndent); | 41 if (!line.isEmpty) _writer.print(_currentIndent); |
| 38 _writer.println(line); | 42 _writer.println(line); |
| 39 } | 43 } |
| 40 _currentText.clear(); | 44 _currentText.clear(); |
| 41 } | 45 } |
| 42 } | 46 } |
| OLD | NEW |