| 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 class Line { | 8 class Line { |
| 9 | 9 |
| 10 final int lineLength; | 10 final int lineLength; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 addSpaces(int n) { | 26 addSpaces(int n) { |
| 27 if (n > 0) { | 27 if (n > 0) { |
| 28 tokens.add(new SpaceToken(n)); | 28 tokens.add(new SpaceToken(n)); |
| 29 } | 29 } |
| 30 } | 30 } |
| 31 | 31 |
| 32 addToken(LineToken token) { | 32 addToken(LineToken token) { |
| 33 tokens.add(token); | 33 tokens.add(token); |
| 34 } | 34 } |
| 35 | 35 |
| 36 bool isWhitespace() { |
| 37 for (var tok in tokens) { |
| 38 if (!(tok is SpaceToken)) { |
| 39 return false; |
| 40 } |
| 41 } |
| 42 return true; |
| 43 } |
| 44 |
| 36 _indent(int n) { | 45 _indent(int n) { |
| 37 tokens.add(useTabs ? new TabToken(n) : new SpaceToken(n * spacesPerIndent)); | 46 tokens.add(useTabs ? new TabToken(n) : new SpaceToken(n * spacesPerIndent)); |
| 38 } | 47 } |
| 39 | 48 |
| 40 String toString() { | 49 String toString() { |
| 41 var buffer = new StringBuffer(); | 50 var buffer = new StringBuffer(); |
| 42 for (var tok in tokens) { | 51 for (var tok in tokens) { |
| 43 buffer.write(tok.toString()); | 52 buffer.write(tok.toString()); |
| 44 } | 53 } |
| 45 return buffer.toString(); | 54 return buffer.toString(); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 | 102 |
| 94 unindent() { | 103 unindent() { |
| 95 --indentCount; | 104 --indentCount; |
| 96 } | 105 } |
| 97 | 106 |
| 98 print(x) { | 107 print(x) { |
| 99 currentLine.addToken(new LineToken(x)); | 108 currentLine.addToken(new LineToken(x)); |
| 100 } | 109 } |
| 101 | 110 |
| 102 newline() { | 111 newline() { |
| 112 if (currentLine.isWhitespace()) { |
| 113 currentLine.tokens.clear(); |
| 114 } |
| 103 currentLine.addToken(new NewlineToken(this.lineSeparator)); | 115 currentLine.addToken(new NewlineToken(this.lineSeparator)); |
| 104 buffer.write(currentLine.toString()); | 116 buffer.write(currentLine.toString()); |
| 105 currentLine = new Line(indent: indentCount); | 117 currentLine = new Line(indent: indentCount); |
| 106 } | 118 } |
| 107 | 119 |
| 120 newlines(int num) { |
| 121 while (num-- > 0) { |
| 122 newline(); |
| 123 } |
| 124 } |
| 125 |
| 108 space() { | 126 space() { |
| 109 spaces(1); | 127 spaces(1); |
| 110 } | 128 } |
| 111 | 129 |
| 112 spaces(n) { | 130 spaces(n) { |
| 113 currentLine.addSpaces(n); | 131 currentLine.addSpaces(n); |
| 114 } | 132 } |
| 115 | 133 |
| 116 println(String s) { | 134 println(String s) { |
| 117 print(s); | 135 print(s); |
| 118 newline(); | 136 newline(); |
| 119 } | 137 } |
| 120 | 138 |
| 121 String toString() { | 139 String toString() { |
| 122 var source = new StringBuffer(buffer.toString()); | 140 var source = new StringBuffer(buffer.toString()); |
| 123 source.write(currentLine); | 141 if (!currentLine.isWhitespace()) { |
| 142 source.write(currentLine); |
| 143 } |
| 124 return source.toString(); | 144 return source.toString(); |
| 125 } | 145 } |
| 146 |
| 126 } | 147 } |
| 127 | 148 |
| 128 | 149 |
| 129 const SPACE = ' '; | 150 const SPACE = ' '; |
| 130 final SPACES = [ | 151 final SPACES = [ |
| 131 '', | 152 '', |
| 132 ' ', | 153 ' ', |
| 133 ' ', | 154 ' ', |
| 134 ' ', | 155 ' ', |
| 135 ' ', | 156 ' ', |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 | 193 |
| 173 String getTabs(int n) => n < TABS.length ? TABS[n] : repeat('\t', n); | 194 String getTabs(int n) => n < TABS.length ? TABS[n] : repeat('\t', n); |
| 174 | 195 |
| 175 String repeat(String ch, int times) { | 196 String repeat(String ch, int times) { |
| 176 var sb = new StringBuffer(); | 197 var sb = new StringBuffer(); |
| 177 for (var i = 0; i < times; ++i) { | 198 for (var i = 0; i < times; ++i) { |
| 178 sb.write(ch); | 199 sb.write(ch); |
| 179 } | 200 } |
| 180 return sb.toString(); | 201 return sb.toString(); |
| 181 } | 202 } |
| OLD | NEW |