OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library source_span.source_location; | |
6 | |
7 import 'source_span.dart'; | |
8 | |
9 // A class that describes a single location within a source file. | |
10 class SourceLocation implements Comparable<SourceLocation> { | |
11 /// URL of the source containing this location. | |
12 /// | |
13 /// This may be null, indicating that the source URL is unknown or | |
14 /// unavailable. | |
15 final Uri sourceUrl; | |
16 | |
17 /// The 0-based offset of this location in the source. | |
18 final int offset; | |
19 | |
20 /// The 0-based line of this location in the source. | |
21 final int line; | |
22 | |
23 /// The 0-based column of this location in the source | |
24 final int column; | |
25 | |
26 /// Returns a representation of this location in the `source:line:column` | |
27 /// format used by text editors. | |
28 /// | |
29 /// This prints 1-based lines and columns. | |
30 String get editorString { | |
Siggi Cherem (dart-lang)
2014/07/16 21:26:09
rename 'editorString'? I'm thinking this is also u
nweiz
2014/07/17 20:22:08
Good point, done.
| |
31 var source = sourceUrl == null ? 'unknown source' : sourceUrl; | |
32 return '$source:${line + 1}:${column + 1}'; | |
33 } | |
34 | |
35 /// Creates a new location indicating [offset] within [sourceUrl]. | |
36 /// | |
37 /// [line] and [column] default to assuming the source is a single line. This | |
38 /// means that [line] defaults to 0 and [column] defaults to [offset]. | |
39 /// | |
40 /// [sourceUrl] may be either a [String], a [Uri], or `null`. | |
41 SourceLocation(int offset, {sourceUrl, int line, int column}) | |
42 : sourceUrl = sourceUrl is String ? Uri.parse(sourceUrl) : sourceUrl, | |
43 offset = offset, | |
44 line = line == null ? 0 : line, | |
45 column = column == null ? offset : column { | |
46 if (this.offset < 0) { | |
47 throw new RangeError("Offset may not be negative, was $offset."); | |
48 } else if (this.line < 0) { | |
49 throw new RangeError("Line may not be negative, was $line."); | |
50 } else if (this.column < 0) { | |
51 throw new RangeError("Column may not be negative, was $column."); | |
52 } | |
53 } | |
54 | |
55 /// Returns the distance in characters between [this] and [other]. | |
56 /// | |
57 /// This always returns a non-negative value. | |
58 int distance(SourceLocation other) { | |
59 if (sourceUrl != other.sourceUrl) { | |
60 throw new ArgumentError("Source URLs \"${sourceUrl}\" and " | |
61 "\"${other.sourceUrl}\" don't match."); | |
62 } | |
63 return (offset - other.offset).abs(); | |
64 } | |
65 | |
66 /// Returns a span that covers only a single point: this location. | |
67 SourceSpan pointSpan() => new SourceSpan(this, this, ""); | |
68 | |
69 /// Compares two locations. | |
70 /// | |
71 /// [other] must have the same source URL as [this]. | |
72 int compareTo(SourceLocation other) { | |
73 if (sourceUrl != other.sourceUrl) { | |
74 throw new ArgumentError("Source URLs \"${sourceUrl}\" and " | |
75 "\"${other.sourceUrl}\" don't match."); | |
76 } | |
77 return offset - other.offset; | |
78 } | |
79 | |
80 bool operator ==(SourceLocation other) => | |
81 sourceUrl == other.sourceUrl && offset == other.offset; | |
82 | |
83 int get hashCode => sourceUrl.hashCode + offset; | |
84 | |
85 String toString() => '<$runtimeType: $offset $editorString>'; | |
86 } | |
OLD | NEW |