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

Side by Side Diff: pkg/source_span/lib/src/location.dart

Issue 381363002: Extract out a source_span package from source_maps. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review Created 6 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 | « pkg/source_span/lib/src/file.dart ('k') | pkg/source_span/lib/src/span.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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.location;
6
7 import '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 toolString {
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 $toolString>';
86 }
OLDNEW
« no previous file with comments | « pkg/source_span/lib/src/file.dart ('k') | pkg/source_span/lib/src/span.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698