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_span; | |
6 | |
7 import 'source_location.dart'; | |
8 import 'source_span_mixin.dart'; | |
9 | |
10 /// A class that describes a segment of source text. | |
11 abstract class SourceSpan implements Comparable<SourceSpan> { | |
12 /// The start location of this span. | |
13 final SourceLocation start; | |
14 | |
15 /// The end location of this span, exclusive. | |
16 final SourceLocation end; | |
17 | |
18 /// The source text for this span. | |
19 final String text; | |
20 | |
21 /// The URL of the source (typically a file) of this span. | |
22 /// | |
23 /// This may be null, indicating that the source URL is unknown or | |
24 /// unavailable. | |
25 final Uri sourceUrl; | |
26 | |
27 /// The length of this span, in characters. | |
28 final int length; | |
29 | |
30 /// Creates a new span from [start] to [end] (exclusive) containing [text]. | |
31 /// | |
32 /// [start] and [end] must have the same source URL and [start] must come | |
33 /// before [end]. [text] must have a number of characters equal to the | |
34 /// distance between [start] and [end]. | |
35 factory SourceSpan(SourceLocation start, SourceLocation end, String text) => | |
36 new _SourceSpan(start, end, text); | |
37 | |
38 /// Creates a new span that's the union of [this] and [other]. | |
39 /// | |
40 /// The two spans must have the same source URL and may not be disjoint. | |
Siggi Cherem (dart-lang)
2014/07/16 21:26:10
I don't think we should make disjointness an error
nweiz
2014/07/17 20:22:08
I made it forbid disjoint spans because I really w
Siggi Cherem (dart-lang)
2014/07/17 22:24:39
Ah, I had not realized until now that you made `te
nweiz
2014/07/17 23:18:15
Do we have a use case for spans without text avail
Siggi Cherem (dart-lang)
2014/07/18 00:11:54
I understand.
The only use-case I've seen is in t
nweiz
2014/07/18 00:30:24
I like "expand"—I've added it to FileSpan, which I
Siggi Cherem (dart-lang)
2014/07/18 00:45:24
Ah - thanks, I somehow missed it. After the file r
| |
41 /// [text] is computed by combining [this.text] and [other.text]. | |
42 SourceSpan union(SourceSpan other); | |
43 | |
44 /// Compares two spans. | |
45 /// | |
46 /// [other] must have the same source URL as [this]. This orders spans by | |
47 /// [start] then [length]. | |
48 int compareTo(SourceSpan other); | |
49 | |
50 /// Formats [message] in a human-friendly way associated with this span. | |
51 /// | |
52 /// [color] may either be a [String], a [bool], or `null`. If it's a string, | |
53 /// it indicates an ANSII terminal color escape that should be used to | |
54 /// highlight the span's text. If it's `true`, it indicates that the text | |
55 /// should be highlighted using the default color. If it's `false` or `null`, | |
56 /// it indicates that the text shouldn't be highlighted. | |
Siggi Cherem (dart-lang)
2014/07/16 21:26:09
that's a lot of semantics in one argument. I don't
nweiz
2014/07/17 20:22:08
I agree that this is packing "color" a little full
Siggi Cherem (dart-lang)
2014/07/17 22:24:39
ok. sounds good
| |
57 String message(String message, {color}); | |
58 } | |
59 | |
60 class _SourceSpan extends SourceSpanMixin { | |
61 final SourceLocation start; | |
62 final SourceLocation end; | |
63 final String text; | |
64 | |
65 _SourceSpan(this.start, this.end, this.text) { | |
Siggi Cherem (dart-lang)
2014/07/16 21:26:10
why not make this class public? alternatively, mak
nweiz
2014/07/17 20:22:08
I tried to put this stuff in SourceSpan, but it ca
Siggi Cherem (dart-lang)
2014/07/17 22:24:39
Your idea sounds good, it seems to match what we d
nweiz
2014/07/17 23:18:15
I like this idea a lot, but it means that someone
| |
66 if (end.sourceUrl != start.sourceUrl) { | |
67 throw new ArgumentError("Source URLs \"${start.sourceUrl}\" and " | |
68 " \"${end.sourceUrl}\" don't match."); | |
69 } else if (end.offset < start.offset) { | |
70 throw new ArgumentError('End $end must come after start $start.'); | |
71 } else if (text.length != start.distance(end)) { | |
72 throw new ArgumentError('Text "$text" must be ${start.distance(end)} ' | |
73 'characters long.'); | |
74 } | |
75 } | |
76 } | |
OLD | NEW |