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

Unified Diff: lib/protobuf/plugin/IndentingWriter.dart

Issue 10595002: Protocol Buffer runtime library and 'protoc' plugin (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Work around http://code.google.com/p/dart/issues/detail?id=3806 Created 8 years, 6 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/protobuf/plugin/ExtensionGenerator.dart ('k') | lib/protobuf/plugin/MessageGenerator.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/protobuf/plugin/IndentingWriter.dart
diff --git a/lib/protobuf/plugin/IndentingWriter.dart b/lib/protobuf/plugin/IndentingWriter.dart
new file mode 100644
index 0000000000000000000000000000000000000000..354cd8f52c2b24ae04f91222298371e031976956
--- /dev/null
+++ b/lib/protobuf/plugin/IndentingWriter.dart
@@ -0,0 +1,87 @@
+// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+class IndentingWriter implements Writer {
+ IndentingWriter(String this._indentSequence, Writer this._writer)
+ : _currentIndent = "";
+
+ /*
+ * nice shorthand for adding content
+ */
+ void addBlock(String start, String end, void body()) {
+ println(start);
+ indent();
+ body();
+ outdent();
+ println(end);
+ }
+ /*
+ * Shift the left-most character of new lines to the right by one
+ * indentation stop (as set in the constructor).
+ */
+ void indent() {
+ _currentIndent = _currentIndent.concat(_indentSequence);
+ }
+
+ /*
+ * Shift the left-most character of new lines to the left by one
+ * indentation stop (as set in the constructor).
+ */
+ void outdent() {
+ _currentIndent = _currentIndent.substring(0,
+ Math.max(0,_currentIndent.length - _indentSequence.length));
+ }
+
+ void print(String stringToPrint) {
+ if (_currentLine === null) {
+ _currentLine = new StringBuffer();
+ }
+ _currentLine.add(stringToPrint);
+ }
+
+ /*
+ * Add the specified line to the output buffer with the current
+ * level of indent. If the input string is multiple lines,
+ * split them and apply the indent to each line.
+ *
+ * Lines are effectively joined with new-lines (e.g. not appended
+ * to every interim line).
+ */
+ void println([String out = null]) {
+ // TODO - not clear how this plays cross-platform.
+ // more sophisticated patterns don't seem to match.
+ String string;
+ if (_currentLine !== null) {
+ string = _currentLine.add(out === null ? "" : out).toString();
+ _currentLine = null;
+ } else {
+ string = out === null ? "" : out;
+ }
+
+ List<String> lines = string.split("\n");
+ for (String line in lines) {
+ if (line.length > 0) _writer.print(_currentIndent);
+ _writer.println(line);
+ }
+ }
+
+ /*
+ * Add a blank line to the output buffer.
+ */
+ void blankLine() {
+ println("");
+ }
+
+ /*
+ * Push out the current line (see print()) if non-empty.
+ */
+ void flushLine() {
+ if (_currentLine !== null && _currentLine.length > 0) println("");
+ }
+
+ StringBuffer _currentLine;
+ Writer _writer;
+ String _currentIndent;
+ String _indentSequence;
+}
« no previous file with comments | « lib/protobuf/plugin/ExtensionGenerator.dart ('k') | lib/protobuf/plugin/MessageGenerator.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698