| 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 tokens = <LineToken>[]; | 10 final tokens = <LineToken>[]; |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 if (current.length > 0) { | 109 if (current.length > 0) { |
| 110 chunks.add(current); | 110 chunks.add(current); |
| 111 } | 111 } |
| 112 current = work; | 112 current = work; |
| 113 } | 113 } |
| 114 work = new Chunk(start: tok, maxLength: maxLength - current.length); | 114 work = new Chunk(start: tok, maxLength: maxLength - current.length); |
| 115 } else { | 115 } else { |
| 116 if (work.fits(tok)) { | 116 if (work.fits(tok)) { |
| 117 work.add(tok); | 117 work.add(tok); |
| 118 } else { | 118 } else { |
| 119 if (!isWhitespace(work)) { | 119 if (!isAllWhitespace(work)) { |
| 120 current.add(work); | 120 current.add(work); |
| 121 } | 121 } |
| 122 if (current.length > 0) { | 122 if (current.length > 0) { |
| 123 chunks.add(current); | 123 chunks.add(current); |
| 124 current = new Chunk(maxLength: maxLength); | 124 current = new Chunk(maxLength: maxLength); |
| 125 } | 125 } |
| 126 work = new Chunk(maxLength: maxLength); | 126 work = new Chunk(maxLength: maxLength); |
| 127 work.add(tok); | 127 work.add(tok); |
| 128 } | 128 } |
| 129 } | 129 } |
| 130 | 130 |
| 131 }); | 131 }); |
| 132 | 132 |
| 133 current.add(work); | 133 current.add(work); |
| 134 if (current.length > 0) { | 134 if (current.length > 0) { |
| 135 chunks.add(current); | 135 chunks.add(current); |
| 136 } | 136 } |
| 137 return chunks; | 137 return chunks; |
| 138 } | 138 } |
| 139 | 139 |
| 140 bool isWhitespace(Chunk chunk) { | 140 bool isAllWhitespace(Chunk chunk) => isWhitespace(chunk.buffer.toString()); |
| 141 var str = chunk.buffer.toString(); | |
| 142 return str.lastIndexOf(new RegExp(r"(\w+)")) == str.length - 1; | |
| 143 } | |
| 144 | 141 |
| 145 /// Test whether this token is a good start for a new working chunk | 142 /// Test whether this token is a good start for a new working chunk |
| 146 bool goodStart(LineToken tok, Chunk workingChunk) => | 143 bool goodStart(LineToken tok, Chunk workingChunk) => |
| 147 tok is SpaceToken && tok.breakWeight >= workingChunk.start.breakWeight; | 144 tok is SpaceToken && tok.breakWeight >= workingChunk.start.breakWeight; |
| 148 | 145 |
| 149 } | 146 } |
| 150 | 147 |
| 148 /// Test if this [string] contains only whitespace characters |
| 149 bool isWhitespace(String string) => string.codeUnits.every( |
| 150 (c) => c == 0x09 || c == 0x20 || c == 0x0A || c == 0x0D); |
| 151 | 151 |
| 152 /// Special token indicating a line start | 152 /// Special token indicating a line start |
| 153 final LINE_START = new SpaceToken(0); | 153 final LINE_START = new SpaceToken(0); |
| 154 | 154 |
| 155 const DEFAULT_SPACE_WEIGHT = -1; | 155 const DEFAULT_SPACE_WEIGHT = -1; |
| 156 | 156 |
| 157 /// Simple non-breaking printer | 157 /// Simple non-breaking printer |
| 158 class SimpleLinePrinter extends LinePrinter { | 158 class SimpleLinePrinter extends LinePrinter { |
| 159 | 159 |
| 160 const SimpleLinePrinter(); | 160 const SimpleLinePrinter(); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 Line currentLine; | 249 Line currentLine; |
| 250 | 250 |
| 251 final String lineSeparator; | 251 final String lineSeparator; |
| 252 int indentCount = 0; | 252 int indentCount = 0; |
| 253 | 253 |
| 254 LinePrinter linePrinter; | 254 LinePrinter linePrinter; |
| 255 LineToken _lastToken; | 255 LineToken _lastToken; |
| 256 | 256 |
| 257 SourceWriter({this.indentCount: 0, this.lineSeparator: NEW_LINE, | 257 SourceWriter({this.indentCount: 0, this.lineSeparator: NEW_LINE, |
| 258 bool useTabs: false, int spacesPerIndent: 2, int maxLineLength: 80}) { | 258 bool useTabs: false, int spacesPerIndent: 2, int maxLineLength: 80}) { |
| 259 // linePrinter = new SimpleLineBreaker(maxLineLength, (n) => | 259 linePrinter = new SimpleLineBreaker(maxLineLength, (n) => |
| 260 // getIndentString(n, useTabs: useTabs, spacesPerIndent: spacesPerIndent)
); | 260 getIndentString(n, useTabs: useTabs, spacesPerIndent: spacesPerIndent)); |
| 261 linePrinter = new SimpleLinePrinter(); | 261 // linePrinter = new SimpleLinePrinter(); |
| 262 currentLine = new Line(indent: indentCount, printer: linePrinter); | 262 currentLine = new Line(indent: indentCount, printer: linePrinter); |
| 263 } | 263 } |
| 264 | 264 |
| 265 LineToken get lastToken => _lastToken; | 265 LineToken get lastToken => _lastToken; |
| 266 | 266 |
| 267 _addToken(LineToken token) { | 267 _addToken(LineToken token) { |
| 268 _lastToken = token; | 268 _lastToken = token; |
| 269 currentLine.addToken(token); | 269 currentLine.addToken(token); |
| 270 } | 270 } |
| 271 | 271 |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 | 367 |
| 368 String getTabs(int n) => n < TABS.length ? TABS[n] : repeat('\t', n); | 368 String getTabs(int n) => n < TABS.length ? TABS[n] : repeat('\t', n); |
| 369 | 369 |
| 370 String repeat(String ch, int times) { | 370 String repeat(String ch, int times) { |
| 371 var sb = new StringBuffer(); | 371 var sb = new StringBuffer(); |
| 372 for (var i = 0; i < times; ++i) { | 372 for (var i = 0; i < times; ++i) { |
| 373 sb.write(ch); | 373 sb.write(ch); |
| 374 } | 374 } |
| 375 return sb.toString(); | 375 return sb.toString(); |
| 376 } | 376 } |
| OLD | NEW |