OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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_span.file; | 5 library source_span.file; |
6 | 6 |
7 import 'dart:math' as math; | 7 import 'dart:math' as math; |
8 import 'dart:typed_data'; | 8 import 'dart:typed_data'; |
9 | 9 |
10 import 'package:path/path.dart' as p; | 10 import 'package:path/path.dart' as p; |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 /// | 198 /// |
199 /// [end] is lazily generated from this to avoid allocating unnecessary | 199 /// [end] is lazily generated from this to avoid allocating unnecessary |
200 /// objects. | 200 /// objects. |
201 final int _end; | 201 final int _end; |
202 | 202 |
203 Uri get sourceUrl => file.url; | 203 Uri get sourceUrl => file.url; |
204 int get length => _end - _start; | 204 int get length => _end - _start; |
205 FileLocation get start => new FileLocation._(file, _start); | 205 FileLocation get start => new FileLocation._(file, _start); |
206 FileLocation get end => new FileLocation._(file, _end); | 206 FileLocation get end => new FileLocation._(file, _end); |
207 String get text => file.getText(_start, _end); | 207 String get text => file.getText(_start, _end); |
208 | 208 String get context => file.getText(file.getOffset(start.line), |
209 String get context { | 209 end.line == file.lines - 1 ? null : file.getOffset(end.line + 1)); |
210 var line = start.line; | |
211 return file.getText(file.getOffset(line), | |
212 line == file.lines - 1 ? null : file.getOffset(line + 1)); | |
213 } | |
214 | 210 |
215 FileSpan._(this.file, this._start, this._end) { | 211 FileSpan._(this.file, this._start, this._end) { |
216 if (_end < _start) { | 212 if (_end < _start) { |
217 throw new ArgumentError('End $_end must come after start $_start.'); | 213 throw new ArgumentError('End $_end must come after start $_start.'); |
218 } else if (_end > file.length) { | 214 } else if (_end > file.length) { |
219 throw new RangeError("End $_end must not be greater than the number " | 215 throw new RangeError("End $_end must not be greater than the number " |
220 "of characters in the file, ${file.length}."); | 216 "of characters in the file, ${file.length}."); |
221 } else if (_start < 0) { | 217 } else if (_start < 0) { |
222 throw new RangeError("Start may not be negative, was $_start."); | 218 throw new RangeError("Start may not be negative, was $_start."); |
223 } | 219 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 if (sourceUrl != other.sourceUrl) { | 258 if (sourceUrl != other.sourceUrl) { |
263 throw new ArgumentError("Source URLs \"${sourceUrl}\" and " | 259 throw new ArgumentError("Source URLs \"${sourceUrl}\" and " |
264 " \"${other.sourceUrl}\" don't match."); | 260 " \"${other.sourceUrl}\" don't match."); |
265 } | 261 } |
266 | 262 |
267 var start = math.min(this._start, other._start); | 263 var start = math.min(this._start, other._start); |
268 var end = math.max(this._end, other._end); | 264 var end = math.max(this._end, other._end); |
269 return new FileSpan._(file, start, end); | 265 return new FileSpan._(file, start, end); |
270 } | 266 } |
271 } | 267 } |
OLD | NEW |