| 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 | 8 /// DEBUG flag indicating whether to use the experimental line-breaker |
| 9 const _USE_LINE_BREAKER = false; | 9 const _USE_LINE_BREAKER = false; |
| 10 | 10 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 } | 88 } |
| 89 } | 89 } |
| 90 return buf.toString(); | 90 return buf.toString(); |
| 91 } | 91 } |
| 92 | 92 |
| 93 String indent(Chunk chunk, int level) => | 93 String indent(Chunk chunk, int level) => |
| 94 '\n' + indenter(level + 2) + chunk.toString(); | 94 '\n' + indenter(level + 2) + chunk.toString(); |
| 95 | 95 |
| 96 List<Chunk> breakLine(Line line) { | 96 List<Chunk> breakLine(Line line) { |
| 97 | 97 |
| 98 var tokens = preprocess(line.tokens); |
| 99 |
| 98 var chunks = <Chunk>[]; | 100 var chunks = <Chunk>[]; |
| 99 | 101 |
| 100 // The current unbroken line | 102 // The current unbroken line |
| 101 var current = new Chunk(maxLength: maxLength); | 103 var current = new Chunk(maxLength: maxLength); |
| 102 | 104 |
| 103 // A tentative working chunk that will either start a new line or get | 105 // A tentative working chunk that will either start a new line or get |
| 104 // absorbed into 'current' | 106 // absorbed into 'current' |
| 105 var work = new Chunk(maxLength: maxLength); | 107 var work = new Chunk(maxLength: maxLength); |
| 106 | 108 |
| 107 line.tokens.forEach((tok) { | 109 tokens.forEach((tok) { |
| 108 | 110 |
| 109 if (goodStart(tok, work)) { | 111 if (goodStart(tok, work)) { |
| 110 if (current.fits(work)) { | 112 if (current.fits(work)) { |
| 111 current.add(work); | 113 current.add(work); |
| 112 } else { | 114 } else { |
| 113 if (current.length > 0) { | 115 if (current.length > 0) { |
| 114 chunks.add(current); | 116 chunks.add(current); |
| 115 } | 117 } |
| 116 current = work; | 118 current = work; |
| 117 } | 119 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 134 | 136 |
| 135 }); | 137 }); |
| 136 | 138 |
| 137 current.add(work); | 139 current.add(work); |
| 138 if (current.length > 0) { | 140 if (current.length > 0) { |
| 139 chunks.add(current); | 141 chunks.add(current); |
| 140 } | 142 } |
| 141 return chunks; | 143 return chunks; |
| 142 } | 144 } |
| 143 | 145 |
| 146 static List<LineToken> preprocess(List<LineToken> tok) { |
| 147 |
| 148 var tokens = <LineToken>[]; |
| 149 var curr; |
| 150 |
| 151 tok.forEach((token){ |
| 152 if (token is! SpaceToken) { |
| 153 if (curr == null) { |
| 154 curr = token; |
| 155 } else { |
| 156 curr = merge(curr, token); |
| 157 } |
| 158 } else { |
| 159 if (curr != null) { |
| 160 tokens.add(curr); |
| 161 curr = null; |
| 162 } |
| 163 tokens.add(token); |
| 164 } |
| 165 }); |
| 166 |
| 167 if (curr != null) { |
| 168 tokens.add(curr); |
| 169 } |
| 170 |
| 171 return tokens; |
| 172 } |
| 173 |
| 174 static LineToken merge(LineToken first, LineToken second) => |
| 175 new LineToken(first.value + second.value); |
| 176 |
| 144 bool isAllWhitespace(Chunk chunk) => isWhitespace(chunk.buffer.toString()); | 177 bool isAllWhitespace(Chunk chunk) => isWhitespace(chunk.buffer.toString()); |
| 145 | 178 |
| 146 /// Test whether this token is a good start for a new working chunk | 179 /// Test whether this token is a good start for a new working chunk |
| 147 bool goodStart(LineToken tok, Chunk workingChunk) => | 180 bool goodStart(LineToken tok, Chunk workingChunk) => |
| 148 tok is SpaceToken && tok.breakWeight >= workingChunk.start.breakWeight; | 181 tok is SpaceToken && tok.breakWeight >= workingChunk.start.breakWeight; |
| 149 | 182 |
| 150 } | 183 } |
| 151 | 184 |
| 152 /// Test if this [string] contains only whitespace characters | 185 /// Test if this [string] contains only whitespace characters |
| 153 bool isWhitespace(String string) => string.codeUnits.every( | 186 bool isWhitespace(String string) => string.codeUnits.every( |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 | 407 |
| 375 String getTabs(int n) => n < TABS.length ? TABS[n] : repeat('\t', n); | 408 String getTabs(int n) => n < TABS.length ? TABS[n] : repeat('\t', n); |
| 376 | 409 |
| 377 String repeat(String ch, int times) { | 410 String repeat(String ch, int times) { |
| 378 var sb = new StringBuffer(); | 411 var sb = new StringBuffer(); |
| 379 for (var i = 0; i < times; ++i) { | 412 for (var i = 0; i < times; ++i) { |
| 380 sb.write(ch); | 413 sb.write(ch); |
| 381 } | 414 } |
| 382 return sb.toString(); | 415 return sb.toString(); |
| 383 } | 416 } |
| OLD | NEW |