| 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 | 8 |
| 9 class Line { | 9 class Line { |
| 10 | 10 |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 var curr; | 145 var curr; |
| 146 | 146 |
| 147 tok.forEach((token){ | 147 tok.forEach((token){ |
| 148 if (token is! SpaceToken) { | 148 if (token is! SpaceToken) { |
| 149 if (curr == null) { | 149 if (curr == null) { |
| 150 curr = token; | 150 curr = token; |
| 151 } else { | 151 } else { |
| 152 curr = merge(curr, token); | 152 curr = merge(curr, token); |
| 153 } | 153 } |
| 154 } else { | 154 } else { |
| 155 if (curr != null) { | 155 if (isNonbreaking(token)) { |
| 156 tokens.add(curr); | 156 curr = merge(curr, token); |
| 157 curr = null; | 157 } else { |
| 158 if (curr != null) { |
| 159 tokens.add(curr); |
| 160 curr = null; |
| 161 } |
| 162 tokens.add(token); |
| 158 } | 163 } |
| 159 tokens.add(token); | |
| 160 } | 164 } |
| 161 }); | 165 }); |
| 162 | 166 |
| 163 if (curr != null) { | 167 if (curr != null) { |
| 164 tokens.add(curr); | 168 tokens.add(curr); |
| 165 } | 169 } |
| 166 | 170 |
| 167 return tokens; | 171 return tokens; |
| 168 } | 172 } |
| 169 | 173 |
| 174 static bool isNonbreaking(SpaceToken token) => |
| 175 token.breakWeight == UNBREAKABLE_SPACE_WEIGHT; |
| 176 |
| 170 static LineToken merge(LineToken first, LineToken second) => | 177 static LineToken merge(LineToken first, LineToken second) => |
| 171 new LineToken(first.value + second.value); | 178 new LineToken(first.value + second.value); |
| 172 | 179 |
| 173 bool isAllWhitespace(Chunk chunk) => isWhitespace(chunk.buffer.toString()); | 180 bool isAllWhitespace(Chunk chunk) => isWhitespace(chunk.buffer.toString()); |
| 174 | 181 |
| 175 bool isLineStart(chunk) => chunk.length == 0 && chunk.start == LINE_START; | 182 bool isLineStart(chunk) => chunk.length == 0 && chunk.start == LINE_START; |
| 176 | 183 |
| 177 /// Test whether this token is a good start for a new working chunk | 184 /// Test whether this token is a good start for a new working chunk |
| 178 bool goodStart(LineToken tok, Chunk workingChunk) => | 185 bool goodStart(LineToken tok, Chunk workingChunk) => |
| 179 tok is SpaceToken && tok.breakWeight >= workingChunk.start.breakWeight; | 186 tok is SpaceToken && tok.breakWeight >= workingChunk.start.breakWeight; |
| 180 | 187 |
| 181 } | 188 } |
| 182 | 189 |
| 183 /// Test if this [string] contains only whitespace characters | 190 /// Test if this [string] contains only whitespace characters |
| 184 bool isWhitespace(String string) => string.codeUnits.every( | 191 bool isWhitespace(String string) => string.codeUnits.every( |
| 185 (c) => c == 0x09 || c == 0x20 || c == 0x0A || c == 0x0D); | 192 (c) => c == 0x09 || c == 0x20 || c == 0x0A || c == 0x0D); |
| 186 | 193 |
| 187 /// Special token indicating a line start | 194 /// Special token indicating a line start |
| 188 final LINE_START = new SpaceToken(0); | 195 final LINE_START = new SpaceToken(0); |
| 189 | 196 |
| 190 const DEFAULT_SPACE_WEIGHT = -1; | 197 const DEFAULT_SPACE_WEIGHT = 0; |
| 198 const UNBREAKABLE_SPACE_WEIGHT = -1; |
| 191 | 199 |
| 192 /// Simple non-breaking printer | 200 /// Simple non-breaking printer |
| 193 class SimpleLinePrinter extends LinePrinter { | 201 class SimpleLinePrinter extends LinePrinter { |
| 194 | 202 |
| 195 const SimpleLinePrinter(); | 203 const SimpleLinePrinter(); |
| 196 | 204 |
| 197 String printLine(Line line) { | 205 String printLine(Line line) { |
| 198 var buffer = new StringBuffer(); | 206 var buffer = new StringBuffer(); |
| 199 line.tokens.forEach((tok) => buffer.write(tok.toString())); | 207 line.tokens.forEach((tok) => buffer.write(tok.toString())); |
| 200 return buffer.toString(); | 208 return buffer.toString(); |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 | 413 |
| 406 String getTabs(int n) => n < TABS.length ? TABS[n] : repeat('\t', n); | 414 String getTabs(int n) => n < TABS.length ? TABS[n] : repeat('\t', n); |
| 407 | 415 |
| 408 String repeat(String ch, int times) { | 416 String repeat(String ch, int times) { |
| 409 var sb = new StringBuffer(); | 417 var sb = new StringBuffer(); |
| 410 for (var i = 0; i < times; ++i) { | 418 for (var i = 0; i < times; ++i) { |
| 411 sb.write(ch); | 419 sb.write(ch); |
| 412 } | 420 } |
| 413 return sb.toString(); | 421 return sb.toString(); |
| 414 } | 422 } |
| OLD | NEW |