| 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 /// DEBUG flag indicating whether to use the experimental line-breaker | |
| 9 const _USE_LINE_BREAKER = false; | |
| 10 | |
| 11 | 8 |
| 12 class Line { | 9 class Line { |
| 13 | 10 |
| 14 final tokens = <LineToken>[]; | 11 final tokens = <LineToken>[]; |
| 15 final bool useTabs; | 12 final bool useTabs; |
| 16 final int spacesPerIndent; | 13 final int spacesPerIndent; |
| 17 final int indent; | 14 final int indent; |
| 18 final LinePrinter printer; | 15 final LinePrinter printer; |
| 19 | 16 |
| 20 Line({this.indent: 0, this.useTabs: false, this.spacesPerIndent: 2, | 17 Line({this.indent: 0, this.useTabs: false, this.spacesPerIndent: 2, |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 if (current.length > 0) { | 112 if (current.length > 0) { |
| 116 chunks.add(current); | 113 chunks.add(current); |
| 117 } | 114 } |
| 118 current = work; | 115 current = work; |
| 119 } | 116 } |
| 120 work = new Chunk(start: tok, maxLength: maxLength - current.length); | 117 work = new Chunk(start: tok, maxLength: maxLength - current.length); |
| 121 } else { | 118 } else { |
| 122 if (work.fits(tok)) { | 119 if (work.fits(tok)) { |
| 123 work.add(tok); | 120 work.add(tok); |
| 124 } else { | 121 } else { |
| 125 if (!isAllWhitespace(work)) { | 122 if (!isAllWhitespace(work) || isLineStart(current)) { |
| 126 current.add(work); | 123 current.add(work); |
| 127 } | 124 } else if (current.length > 0) { |
| 128 if (current.length > 0) { | |
| 129 chunks.add(current); | 125 chunks.add(current); |
| 130 current = new Chunk(maxLength: maxLength); | 126 current = new Chunk(maxLength: maxLength); |
| 131 } | 127 } |
| 132 work = new Chunk(maxLength: maxLength); | 128 work = new Chunk(maxLength: maxLength); |
| 133 work.add(tok); | 129 work.add(tok); |
| 134 } | 130 } |
| 135 } | 131 } |
| 136 | 132 |
| 137 }); | 133 }); |
| 138 | 134 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 169 } | 165 } |
| 170 | 166 |
| 171 return tokens; | 167 return tokens; |
| 172 } | 168 } |
| 173 | 169 |
| 174 static LineToken merge(LineToken first, LineToken second) => | 170 static LineToken merge(LineToken first, LineToken second) => |
| 175 new LineToken(first.value + second.value); | 171 new LineToken(first.value + second.value); |
| 176 | 172 |
| 177 bool isAllWhitespace(Chunk chunk) => isWhitespace(chunk.buffer.toString()); | 173 bool isAllWhitespace(Chunk chunk) => isWhitespace(chunk.buffer.toString()); |
| 178 | 174 |
| 175 bool isLineStart(chunk) => chunk.length == 0 && chunk.start == LINE_START; |
| 176 |
| 179 /// Test whether this token is a good start for a new working chunk | 177 /// Test whether this token is a good start for a new working chunk |
| 180 bool goodStart(LineToken tok, Chunk workingChunk) => | 178 bool goodStart(LineToken tok, Chunk workingChunk) => |
| 181 tok is SpaceToken && tok.breakWeight >= workingChunk.start.breakWeight; | 179 tok is SpaceToken && tok.breakWeight >= workingChunk.start.breakWeight; |
| 182 | 180 |
| 183 } | 181 } |
| 184 | 182 |
| 185 /// Test if this [string] contains only whitespace characters | 183 /// Test if this [string] contains only whitespace characters |
| 186 bool isWhitespace(String string) => string.codeUnits.every( | 184 bool isWhitespace(String string) => string.codeUnits.every( |
| 187 (c) => c == 0x09 || c == 0x20 || c == 0x0A || c == 0x0D); | 185 (c) => c == 0x09 || c == 0x20 || c == 0x0A || c == 0x0D); |
| 188 | 186 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 Line currentLine; | 284 Line currentLine; |
| 287 | 285 |
| 288 final String lineSeparator; | 286 final String lineSeparator; |
| 289 int indentCount = 0; | 287 int indentCount = 0; |
| 290 | 288 |
| 291 LinePrinter linePrinter; | 289 LinePrinter linePrinter; |
| 292 LineToken _lastToken; | 290 LineToken _lastToken; |
| 293 | 291 |
| 294 SourceWriter({this.indentCount: 0, this.lineSeparator: NEW_LINE, | 292 SourceWriter({this.indentCount: 0, this.lineSeparator: NEW_LINE, |
| 295 bool useTabs: false, int spacesPerIndent: 2, int maxLineLength: 80}) { | 293 bool useTabs: false, int spacesPerIndent: 2, int maxLineLength: 80}) { |
| 296 if (_USE_LINE_BREAKER) { | 294 if (maxLineLength > 0) { |
| 297 linePrinter = new SimpleLineBreaker(maxLineLength, (n) => | 295 linePrinter = new SimpleLineBreaker(maxLineLength, (n) => |
| 298 getIndentString(n, useTabs: useTabs, spacesPerIndent: spacesPerIndent)
); | 296 getIndentString(n, useTabs: useTabs, spacesPerIndent: spacesPerIndent)
); |
| 299 } else { | 297 } else { |
| 300 linePrinter = new SimpleLinePrinter(); | 298 linePrinter = new SimpleLinePrinter(); |
| 301 } | 299 } |
| 302 currentLine = new Line(indent: indentCount, printer: linePrinter); | 300 currentLine = new Line(indent: indentCount, printer: linePrinter); |
| 303 } | 301 } |
| 304 | 302 |
| 305 LineToken get lastToken => _lastToken; | 303 LineToken get lastToken => _lastToken; |
| 306 | 304 |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 | 405 |
| 408 String getTabs(int n) => n < TABS.length ? TABS[n] : repeat('\t', n); | 406 String getTabs(int n) => n < TABS.length ? TABS[n] : repeat('\t', n); |
| 409 | 407 |
| 410 String repeat(String ch, int times) { | 408 String repeat(String ch, int times) { |
| 411 var sb = new StringBuffer(); | 409 var sb = new StringBuffer(); |
| 412 for (var i = 0; i < times; ++i) { | 410 for (var i = 0; i < times; ++i) { |
| 413 sb.write(ch); | 411 sb.write(ch); |
| 414 } | 412 } |
| 415 return sb.toString(); | 413 return sb.toString(); |
| 416 } | 414 } |
| OLD | NEW |