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 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
192 /// [start] is lazily generated from this to avoid allocating unnecessary | 192 /// [start] is lazily generated from this to avoid allocating unnecessary |
193 /// objects. | 193 /// objects. |
194 final int _start; | 194 final int _start; |
195 | 195 |
196 /// The offset of the end of the span. | 196 /// The offset of the end of the span. |
197 /// | 197 /// |
198 /// [end] is lazily generated from this to avoid allocating unnecessary | 198 /// [end] is lazily generated from this to avoid allocating unnecessary |
199 /// objects. | 199 /// objects. |
200 final int _end; | 200 final int _end; |
201 | 201 |
202 Uri get sourceUrl => file.url; | |
203 int get length => _end - _start; | |
202 FileLocation get start => new FileLocation._(file, _start); | 204 FileLocation get start => new FileLocation._(file, _start); |
203 FileLocation get end => new FileLocation._(file, _end); | 205 FileLocation get end => new FileLocation._(file, _end); |
204 | 206 String get text => file.getText(_start, _end); |
205 String get text => file.getText(start.offset, end.offset); | |
206 | 207 |
207 FileSpan._(this.file, this._start, this._end) { | 208 FileSpan._(this.file, this._start, this._end) { |
208 if (_end < _start) { | 209 if (_end < _start) { |
209 throw new ArgumentError('End $_end must come after start $_start.'); | 210 throw new ArgumentError('End $_end must come after start $_start.'); |
210 } else if (_end > file.length) { | 211 } else if (_end > file.length) { |
211 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 " |
212 "of characters in the file, ${file.length}."); | 213 "of characters in the file, ${file.length}."); |
213 } else if (_start < 0) { | 214 } else if (_start < 0) { |
214 throw new RangeError("Start may not be negative, was $_start."); | 215 throw new RangeError("Start may not be negative, was $_start."); |
215 } | 216 } |
216 } | 217 } |
217 | 218 |
219 int compareTo(SourceSpan other) { | |
220 if (other is! FileSpan) return super.compareTo(other); | |
221 | |
222 FileSpan otherFile = other; | |
223 var d = _start.compareTo(otherFile._start); | |
Bob Nystrom
2014/11/21 23:38:18
"d" seems random to me. How about "result"?
nweiz
2014/11/21 23:47:31
Done. This is a relic of the old source_maps code.
| |
224 return d == 0 ? _end.compareTo(otherFile._end) : d; | |
225 } | |
226 | |
218 SourceSpan union(SourceSpan other) { | 227 SourceSpan union(SourceSpan other) { |
Bob Nystrom
2014/11/21 23:38:17
Is this an override? If not, doc comment.
nweiz
2014/11/21 23:47:31
Yes, it is.
| |
219 if (other is! FileSpan) return super.union(other); | 228 if (other is! FileSpan) return super.union(other); |
220 | 229 |
221 var span = expand(other); | 230 var span = expand(other); |
222 var beginSpan = span.start == this.start ? this : other; | 231 var beginSpan = span._start == _start ? this : other; |
223 var endSpan = span.end == this.end ? this : other; | 232 var endSpan = span._end == _end ? this : other; |
224 | 233 |
225 if (beginSpan.end.compareTo(endSpan.start) < 0) { | 234 if (beginSpan._end < endSpan._start) { |
226 throw new ArgumentError("Spans $this and $other are disjoint."); | 235 throw new ArgumentError("Spans $this and $other are disjoint."); |
227 } | 236 } |
228 | 237 |
229 return span; | 238 return span; |
230 } | 239 } |
231 | 240 |
241 bool operator ==(other) { | |
242 if (other is! FileSpan) return super == other; | |
243 return _start == other._start && _end == other._end && | |
244 sourceUrl == other.sourceUrl; | |
245 } | |
246 | |
247 int get hashCode => _start.hashCode + 5 * _end.hashCode + | |
248 7 * sourceUrl.hashCode; | |
249 | |
232 /// Returns a new span that covers both [this] and [other]. | 250 /// Returns a new span that covers both [this] and [other]. |
233 /// | 251 /// |
234 /// Unlike [union], [other] may be disjoint from [this]. If it is, the text | 252 /// Unlike [union], [other] may be disjoint from [this]. If it is, the text |
235 /// between the two will be covered by the returned span. | 253 /// between the two will be covered by the returned span. |
236 FileSpan expand(FileSpan other) { | 254 FileSpan expand(FileSpan other) { |
237 if (sourceUrl != other.sourceUrl) { | 255 if (sourceUrl != other.sourceUrl) { |
238 throw new ArgumentError("Source URLs \"${sourceUrl}\" and " | 256 throw new ArgumentError("Source URLs \"${sourceUrl}\" and " |
239 " \"${other.sourceUrl}\" don't match."); | 257 " \"${other.sourceUrl}\" don't match."); |
240 } | 258 } |
241 | 259 |
242 var start = math.min(this._start, other._start); | 260 var start = math.min(this._start, other._start); |
243 var end = math.max(this._end, other._end); | 261 var end = math.max(this._end, other._end); |
244 return new FileSpan._(file, start, end); | 262 return new FileSpan._(file, start, end); |
245 } | 263 } |
246 | 264 |
247 String message(String message, {color}) { | 265 String message(String message, {color}) { |
248 if (color == true) color = colors.RED; | 266 if (color == true) color = colors.RED; |
249 if (color == false) color = null; | 267 if (color == false) color = null; |
250 | 268 |
251 var line = start.line; | 269 var line = start.line; |
252 var column = start.column; | 270 var column = start.column; |
253 | 271 |
254 var buffer = new StringBuffer(); | 272 var buffer = new StringBuffer(); |
(...skipping 19 matching lines...) Expand all Loading... | |
274 } | 292 } |
275 if (!textLine.endsWith('\n')) buffer.write('\n'); | 293 if (!textLine.endsWith('\n')) buffer.write('\n'); |
276 | 294 |
277 buffer.write(' ' * column); | 295 buffer.write(' ' * column); |
278 if (color != null) buffer.write(color); | 296 if (color != null) buffer.write(color); |
279 buffer.write('^' * math.max(toColumn - column, 1)); | 297 buffer.write('^' * math.max(toColumn - column, 1)); |
280 if (color != null) buffer.write(colors.NONE); | 298 if (color != null) buffer.write(colors.NONE); |
281 return buffer.toString(); | 299 return buffer.toString(); |
282 } | 300 } |
283 } | 301 } |
OLD | NEW |