Chromium Code Reviews| 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 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 | 225 |
| 226 int maxLength; | 226 int maxLength; |
| 227 SpaceToken start; | 227 SpaceToken start; |
| 228 | 228 |
| 229 Chunk({this.start, this.maxLength}) { | 229 Chunk({this.start, this.maxLength}) { |
| 230 if (start == null) { | 230 if (start == null) { |
| 231 start = LINE_START; | 231 start = LINE_START; |
| 232 } | 232 } |
| 233 } | 233 } |
| 234 | 234 |
| 235 bool fits(LineText text) => length + text.length < maxLength; | 235 bool fits(LineText text) => length + text.length <= maxLength; |
| 236 | 236 |
| 237 int get length => start.value.length + buffer.length; | 237 int get length => start.value.length + buffer.length; |
| 238 | 238 |
| 239 void add(LineText text) { | 239 void add(LineText text) { |
| 240 text.addTo(this); | 240 text.addTo(this); |
| 241 } | 241 } |
| 242 | 242 |
| 243 String toString() => buffer.toString(); | 243 String toString() => buffer.toString(); |
| 244 | 244 |
| 245 void addTo(Chunk chunk) { | 245 void addTo(Chunk chunk) { |
| 246 chunk.buffer.write(start.value); | 246 chunk.buffer.write(start.value); |
| 247 chunk.buffer.write(buffer.toString()); | 247 chunk.buffer.write(buffer.toString()); |
| 248 } | 248 } |
| 249 } | 249 } |
| 250 | 250 |
| 251 | 251 |
| 252 class LineToken implements LineText { | 252 class LineToken implements LineText { |
| 253 | 253 |
| 254 final String value; | 254 final String value; |
| 255 | 255 |
| 256 LineToken(this.value); | 256 LineToken(this.value); |
| 257 | 257 |
| 258 String toString() => value; | 258 String toString() => value; |
| 259 | 259 |
| 260 int get length => value.length; | 260 int get length => lengthLessNewlines(value); |
| 261 | 261 |
| 262 void addTo(Chunk chunk) { | 262 void addTo(Chunk chunk) { |
| 263 chunk.buffer.write(value); | 263 chunk.buffer.write(value); |
| 264 } | 264 } |
| 265 | |
| 266 int lengthLessNewlines(String str) => | |
| 267 str.endsWith('\n') ? str.length - 1 : str.length; | |
|
Brian Wilkerson
2014/01/30 00:51:20
Do we also need to test for "\r\n"?
| |
| 268 | |
| 265 } | 269 } |
| 266 | 270 |
| 267 | 271 |
| 268 class SpaceToken extends LineToken { | 272 class SpaceToken extends LineToken { |
| 269 | 273 |
| 270 final int breakWeight; | 274 final int breakWeight; |
| 271 | 275 |
| 272 SpaceToken(int n, {this.breakWeight: DEFAULT_SPACE_WEIGHT}) : | 276 SpaceToken(int n, {this.breakWeight: DEFAULT_SPACE_WEIGHT}) : |
| 273 super(getSpaces(n)); | 277 super(getSpaces(n)); |
| 274 } | 278 } |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 413 | 417 |
| 414 String getTabs(int n) => n < TABS.length ? TABS[n] : repeat('\t', n); | 418 String getTabs(int n) => n < TABS.length ? TABS[n] : repeat('\t', n); |
| 415 | 419 |
| 416 String repeat(String ch, int times) { | 420 String repeat(String ch, int times) { |
| 417 var sb = new StringBuffer(); | 421 var sb = new StringBuffer(); |
| 418 for (var i = 0; i < times; ++i) { | 422 for (var i = 0; i < times; ++i) { |
| 419 sb.write(ch); | 423 sb.write(ch); |
| 420 } | 424 } |
| 421 return sb.toString(); | 425 return sb.toString(); |
| 422 } | 426 } |
| OLD | NEW |