Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(225)

Side by Side Diff: pkg/source_maps/lib/span.dart

Issue 18749005: Fixes in sourcemaps discovered in csslib (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/source_maps/test/span_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 /// Dart classes representing the souce spans and source files. 5 /// Dart classes representing the souce spans and source files.
6 library source_maps.span; 6 library source_maps.span;
7 7
8 import 'dart:utf' show stringToCodepoints; 8 import 'dart:utf' show stringToCodepoints;
9 import 'dart:math' show min, max; 9 import 'dart:math' show min, max;
10 10
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 Span span(int start, [int end, bool isIdentifier = false]) => 212 Span span(int start, [int end, bool isIdentifier = false]) =>
213 new FileSpan(this, start, end, isIdentifier); 213 new FileSpan(this, start, end, isIdentifier);
214 214
215 /// Returns a location in this [SourceFile] with the given offset. 215 /// Returns a location in this [SourceFile] with the given offset.
216 Location location(int offset) => new FileLocation(this, offset); 216 Location location(int offset) => new FileLocation(this, offset);
217 217
218 /// Gets the 0-based line corresponding to an offset. 218 /// Gets the 0-based line corresponding to an offset.
219 int getLine(int offset) => binarySearch(_lineStarts, (o) => o > offset) - 1; 219 int getLine(int offset) => binarySearch(_lineStarts, (o) => o > offset) - 1;
220 220
221 /// Gets the 0-based column corresponding to an offset. 221 /// Gets the 0-based column corresponding to an offset.
222 int getColumn(int line, int offset) => offset - _lineStarts[line]; 222 int getColumn(int line, int offset) {
223 if (line < 0 || line >= _lineStarts.length) return 0;
224 return offset - _lineStarts[line];
225 }
223 226
224 /// Get the offset for a given line and column 227 /// Get the offset for a given line and column
225 int getOffset(int line, int column) => 228 int getOffset(int line, int column) {
226 _lineStarts[max(min(line, _lineStarts.length - 1), 0)] + column; 229 if (line < 0) return getOffset(0, 0);
230 if (line < _lineStarts.length) {
231 return _lineStarts[line] + column;
232 } else {
233 return _decodedChars.length;
234 }
235 }
227 236
228 /// Gets the text at the given offsets. 237 /// Gets the text at the given offsets.
229 String getText(int start, [int end]) => 238 String getText(int start, [int end]) {
230 new String.fromCharCodes(_decodedChars.sublist(max(start, 0), end)); 239 var res = new String.fromCharCodes(_decodedChars.sublist(max(start, 0), end) );
dgrove 2013/07/10 16:53:01 80 chars.
Siggi Cherem (dart-lang) 2013/07/10 16:55:30 oops, I had fixed this and forgot to upload it =)
240 return res;
241 }
231 242
232 /// Create a pretty string representation from a span. 243 /// Create a pretty string representation from a span.
233 String getLocationMessage(String message, int start, int end, 244 String getLocationMessage(String message, int start, int end,
234 {bool useColors: false, String color}) { 245 {bool useColors: false, String color}) {
235 // TODO(jmesserly): it would be more useful to pass in an object that 246 // TODO(jmesserly): it would be more useful to pass in an object that
236 // controls how the errors are printed. This method is a bit too smart. 247 // controls how the errors are printed. This method is a bit too smart.
237 var line = getLine(start); 248 var line = getLine(start);
238 var column = getColumn(line, start); 249 var column = getColumn(line, start);
239 250
240 var src = url == null ? '' : url; 251 var src = url == null ? '' : url;
241 var msg = '$src:${line + 1}:${column + 1}: $message'; 252 var msg = '$src:${line + 1}:${column + 1}: $message';
242 253
243 if (_decodedChars == null) { 254 if (_decodedChars == null) {
244 // We don't have any text to include, so exit. 255 // We don't have any text to include, so exit.
245 return msg; 256 return msg;
246 } 257 }
247 258
248 var buf = new StringBuffer(msg); 259 var buf = new StringBuffer(msg);
249 buf.write('\n'); 260 buf.write('\n');
250 261
251 // +1 for 0-indexing, +1 again to avoid the last line 262 // +1 for 0-indexing, +1 again to avoid the last line
252 var textLine = getText(getOffset(line, 0), getOffset(line + 1, 0)); 263 var textLine = getText(getOffset(line, 0), getOffset(line + 1, 0));
253 264
254
255 column = min(column, textLine.length - 1); 265 column = min(column, textLine.length - 1);
256 int toColumn = min(column + end - start, textLine.length); 266 int toColumn = min(column + end - start, textLine.length);
257 if (useColors) { 267 if (useColors) {
258 if (color == null) { 268 if (color == null) {
259 color = _RED_COLOR; 269 color = _RED_COLOR;
260 } 270 }
261 buf.write(textLine.substring(0, column)); 271 buf.write(textLine.substring(0, column));
262 buf.write(color); 272 buf.write(color);
263 buf.write(textLine.substring(column, toColumn)); 273 buf.write(textLine.substring(column, toColumn));
264 buf.write(_NO_COLOR); 274 buf.write(_NO_COLOR);
265 buf.write(textLine.substring(toColumn)); 275 buf.write(textLine.substring(toColumn));
266 } else { 276 } else {
267 buf.write(textLine); 277 buf.write(textLine);
278 if (textLine != '' && !textLine.endsWith('\n')) buf.write('\n');
268 } 279 }
269 280
270 int i = 0; 281 int i = 0;
271 for (; i < column; i++) { 282 for (; i < column; i++) {
272 buf.write(' '); 283 buf.write(' ');
273 } 284 }
274 285
275 if (useColors) buf.write(color); 286 if (useColors) buf.write(color);
276 for (; i < toColumn; i++) { 287 for (; i < toColumn; i++) {
277 buf.write('^'); 288 buf.write('^');
278 } 289 }
279 if (useColors) buf.write(_NO_COLOR); 290 if (useColors) buf.write(_NO_COLOR);
280 return buf.toString(); 291 return buf.toString();
281 } 292 }
282 } 293 }
283 294
284 /// A convenience type to treat a code segment as if it were a separate 295 /// A convenience type to treat a code segment as if it were a separate
285 /// [SourceFile]. A [SourceFileSegment] shifts all locations by an offset, which 296 /// [SourceFile]. A [SourceFileSegment] shifts all locations by an offset, which
286 /// allows you to set source-map locations based on the locations relative to 297 /// allows you to set source-map locations based on the locations relative to
287 /// the start of the segment, but that get translated to absolute locations in 298 /// the start of the segment, but that get translated to absolute locations in
288 /// the original source file. 299 /// the original source file.
289 class SourceFileSegment extends SourceFile { 300 class SourceFileSegment extends SourceFile {
290 final int _baseOffset; 301 final int _baseOffset;
291 final int _baseLine; 302 final int _baseLine;
292 final int _baseColumn; 303 final int _baseColumn;
304 final int _maxOffset;
293 305
294 // TODO(sigmund): consider providing an end-offset to correctly truncate
295 // values passed the end of the segment.
296 SourceFileSegment(String url, String textSegment, Location startOffset) 306 SourceFileSegment(String url, String textSegment, Location startOffset)
297 : _baseOffset = startOffset.offset, 307 : _baseOffset = startOffset.offset,
298 _baseLine = startOffset.line, 308 _baseLine = startOffset.line,
299 _baseColumn = startOffset.column, 309 _baseColumn = startOffset.column,
310 _maxOffset = startOffset.offset + textSegment.length,
300 super.text(url, textSegment); 311 super.text(url, textSegment);
301 312
302 /// Craete a span, where [start] is relative to this segment's base offset. 313 /// Craete a span, where [start] is relative to this segment's base offset.
303 /// The returned span stores the real offset on the file, so that error 314 /// The returned span stores the real offset on the file, so that error
304 /// messages are reported at the real location. 315 /// messages are reported at the real location.
305 Span span(int start, [int end, bool isIdentifier = false]) => 316 Span span(int start, [int end, bool isIdentifier = false]) =>
306 super.span(start + _baseOffset, 317 super.span(start + _baseOffset,
307 end == null ? null : end + _baseOffset, isIdentifier); 318 end == null ? null : end + _baseOffset, isIdentifier);
308 319
309 /// Create a location, where [offset] relative to this segment's base offset. 320 /// Create a location, where [offset] relative to this segment's base offset.
310 /// The returned span stores the real offset on the file, so that error 321 /// The returned span stores the real offset on the file, so that error
311 /// messages are reported at the real location. 322 /// messages are reported at the real location.
312 Location location(int offset) => super.location(offset + _baseOffset); 323 Location location(int offset) => super.location(offset + _baseOffset);
313 324
314 /// Return the line on the underlying file associated with the [offset] of the 325 /// Return the line on the underlying file associated with the [offset] of the
315 /// underlying file. This method operates on the real offsets from the 326 /// underlying file. This method operates on the real offsets from the
316 /// original file, so that error messages can be reported accurately. 327 /// original file, so that error messages can be reported accurately. When the
317 int getLine(int offset) => 328 /// requested offset is past the length of the segment, this returns the line
318 super.getLine(max(offset - _baseOffset, 0)) + _baseLine; 329 /// number after the end of the segment (total lines + 1).
330 int getLine(int offset) {
331 var res = super.getLine(max(offset - _baseOffset, 0)) + _baseLine;
332 return (offset > _maxOffset) ? res + 1 : res;
333 }
319 334
320 /// Return the column on the underlying file associated with [line] and 335 /// Return the column on the underlying file associated with [line] and
321 /// [offset], where [line] is absolute from the beginning of the underlying 336 /// [offset], where [line] is absolute from the beginning of the underlying
322 /// file. This method operates on the real offsets from the original file, so 337 /// file. This method operates on the real offsets from the original file, so
323 /// that error messages can be reported accurately. 338 /// that error messages can be reported accurately.
324 int getColumn(int line, int offset) { 339 int getColumn(int line, int offset) {
325 var col = super.getColumn(line - _baseLine, max(offset - _baseOffset, 0)); 340 var col = super.getColumn(line - _baseLine, max(offset - _baseOffset, 0));
326 return line == _baseLine ? col + _baseColumn : col; 341 return line == _baseLine ? col + _baseColumn : col;
327 } 342 }
328 343
329 /// Return the offset associated with a line and column. This method operates 344 /// Return the offset associated with a line and column. This method operates
330 /// on the real offsets from the original file, so that error messages can be 345 /// on the real offsets from the original file, so that error messages can be
331 /// reported accurately. 346 /// reported accurately.
332 int getOffset(int line, int column) => 347 int getOffset(int line, int column) =>
333 super.getOffset(line - _baseLine, 348 super.getOffset(line - _baseLine,
334 line == _baseLine ? column - _baseColumn : column) + _baseOffset; 349 line == _baseLine ? column - _baseColumn : column) + _baseOffset;
335 350
336 /// Retrieve the text associated with the specified range. This method 351 /// Retrieve the text associated with the specified range. This method
337 /// operates on the real offsets from the original file, so that error 352 /// operates on the real offsets from the original file, so that error
338 /// messages can be reported accurately. 353 /// messages can be reported accurately.
339 String getText(int start, [int end]) => 354 String getText(int start, [int end]) =>
340 super.getText(start - _baseOffset, 355 super.getText(start - _baseOffset, end == null ? null : end - _baseOffset);
341 end == null ? null : end - _baseOffset);
342 } 356 }
OLDNEW
« no previous file with comments | « no previous file | pkg/source_maps/test/span_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698