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; | |
11 | |
12 import 'colors.dart' as colors; | |
13 import 'location.dart'; | 10 import 'location.dart'; |
14 import 'span.dart'; | 11 import 'span.dart'; |
15 import 'span_mixin.dart'; | 12 import 'span_mixin.dart'; |
16 import 'span_with_context.dart'; | 13 import 'span_with_context.dart'; |
17 import 'utils.dart'; | 14 import 'utils.dart'; |
18 | 15 |
19 // Constants to determine end-of-lines. | 16 // Constants to determine end-of-lines. |
20 const int _LF = 10; | 17 const int _LF = 10; |
21 const int _CR = 13; | 18 const int _CR = 13; |
22 | 19 |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
213 throw new ArgumentError('End $_end must come after start $_start.'); | 210 throw new ArgumentError('End $_end must come after start $_start.'); |
214 } else if (_end > file.length) { | 211 } else if (_end > file.length) { |
215 throw new RangeError("End $_end must not be greater than the number " | 212 throw new RangeError("End $_end must not be greater than the number " |
216 "of characters in the file, ${file.length}."); | 213 "of characters in the file, ${file.length}."); |
217 } else if (_start < 0) { | 214 } else if (_start < 0) { |
218 throw new RangeError("Start may not be negative, was $_start."); | 215 throw new RangeError("Start may not be negative, was $_start."); |
219 } | 216 } |
220 } | 217 } |
221 | 218 |
222 int compareTo(SourceSpan other) { | 219 int compareTo(SourceSpan other) { |
223 if (other is! FileSpan) return super.compareTo(other); | 220 // Check runtimeType to be resilient to external FileSpan implementations. |
221 if (other.runtimeType != FileSpan) return super.compareTo(other); | |
Siggi Cherem (dart-lang)
2015/08/18 18:01:59
Could we instead move some of the shared code into
nweiz
2015/08/18 20:28:51
I don't think a mixin would work, but I've split o
| |
224 | 222 |
225 FileSpan otherFile = other; | 223 FileSpan otherFile = other; |
226 var result = _start.compareTo(otherFile._start); | 224 var result = _start.compareTo(otherFile._start); |
227 return result == 0 ? _end.compareTo(otherFile._end) : result; | 225 return result == 0 ? _end.compareTo(otherFile._end) : result; |
228 } | 226 } |
229 | 227 |
230 SourceSpan union(SourceSpan other) { | 228 SourceSpan union(SourceSpan other) { |
231 if (other is! FileSpan) return super.union(other); | 229 if (other is! FileSpan) return super.union(other); |
232 | 230 |
233 var span = expand(other); | 231 var span = expand(other); |
234 var beginSpan = span._start == _start ? this : other; | 232 var beginSpan = span._start == _start ? this : other; |
235 var endSpan = span._end == _end ? this : other; | 233 var endSpan = span._end == _end ? this : other; |
236 | 234 |
237 if (beginSpan._end < endSpan._start) { | 235 if (beginSpan._end < endSpan._start) { |
238 throw new ArgumentError("Spans $this and $other are disjoint."); | 236 throw new ArgumentError("Spans $this and $other are disjoint."); |
239 } | 237 } |
240 | 238 |
241 return span; | 239 return span; |
242 } | 240 } |
243 | 241 |
244 bool operator ==(other) { | 242 bool operator ==(other) { |
243 // Check runtimeType to be resilient to external FileSpan implementations. | |
245 if (other is! FileSpan) return super == other; | 244 if (other is! FileSpan) return super == other; |
246 return _start == other._start && _end == other._end && | 245 if (other.runtimeType != FileSpan) { |
247 sourceUrl == other.sourceUrl; | 246 return super == other && sourceUrl == other.sourceUrl; |
247 } | |
248 | |
249 return _start == other._start && _end == other._end; | |
Siggi Cherem (dart-lang)
2015/08/18 18:01:59
mmm - I thought we would keep the check on sourceU
nweiz
2015/08/18 20:28:51
Yeah, good catch. Just a cut/paste error.
| |
248 } | 250 } |
249 | 251 |
250 int get hashCode => _start.hashCode + 5 * _end.hashCode + | |
251 7 * sourceUrl.hashCode; | |
252 | |
253 /// Returns a new span that covers both [this] and [other]. | 252 /// Returns a new span that covers both [this] and [other]. |
254 /// | 253 /// |
255 /// Unlike [union], [other] may be disjoint from [this]. If it is, the text | 254 /// Unlike [union], [other] may be disjoint from [this]. If it is, the text |
256 /// between the two will be covered by the returned span. | 255 /// between the two will be covered by the returned span. |
257 FileSpan expand(FileSpan other) { | 256 FileSpan expand(FileSpan other) { |
258 if (sourceUrl != other.sourceUrl) { | 257 if (sourceUrl != other.sourceUrl) { |
259 throw new ArgumentError("Source URLs \"${sourceUrl}\" and " | 258 throw new ArgumentError("Source URLs \"${sourceUrl}\" and " |
260 " \"${other.sourceUrl}\" don't match."); | 259 " \"${other.sourceUrl}\" don't match."); |
261 } | 260 } |
262 | 261 |
263 var start = math.min(this._start, other._start); | 262 // Check runtimeType to be resilient to external FileSpan implementations. |
264 var end = math.max(this._end, other._end); | 263 if (other.runtimeType == FileSpan) { |
265 return new FileSpan._(file, start, end); | 264 var start = math.min(this._start, other._start); |
Siggi Cherem (dart-lang)
2015/08/18 18:01:59
I wonder if we should have these optimizations or
nweiz
2015/08/18 20:28:51
In my experiments, there's a lot of benefit that c
| |
265 var end = math.max(this._end, other._end); | |
266 return new FileSpan._(file, start, end); | |
267 } else { | |
268 var start = math.min(this._start, other.start.offset); | |
269 var end = math.max(this._end, other.end.offset); | |
270 return new FileSpan._(file, start, end); | |
271 } | |
266 } | 272 } |
267 } | 273 } |
OLD | NEW |