Chromium Code Reviews| Index: pkg/source_span/lib/src/source_span_mixin.dart |
| diff --git a/pkg/source_span/lib/src/source_span_mixin.dart b/pkg/source_span/lib/src/source_span_mixin.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c4d28663bed3426e51aa1c481652ddc16aafbd39 |
| --- /dev/null |
| +++ b/pkg/source_span/lib/src/source_span_mixin.dart |
| @@ -0,0 +1,74 @@ |
| +// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +library source_span.source_span_mixin; |
| + |
| +import 'package:path/path.dart' as p; |
| + |
| +import 'colors.dart' as colors; |
| +import 'source_span.dart'; |
| +import 'utils.dart'; |
| + |
| +/// A mixin for easily implementing [SourceSpan]. |
| +/// |
| +/// This implements the [SourceSpan] methods in terms of [start], [end], and |
| +/// [text]. This assumes that [start] and [end] have the same source URL, that |
| +/// [start] comes before [end], and that [text] has a number of characters equal |
| +/// to the distance between [start] and [end]. |
| +abstract class SourceSpanMixin implements SourceSpan { |
| + Uri get sourceUrl => start.sourceUrl; |
| + int get length => end.offset - start.offset; |
| + |
| + int compareTo(SourceSpan other) { |
| + int d = start.compareTo(other.start); |
| + return d == 0 ? end.compareTo(other.end) : d; |
| + } |
| + |
| + SourceSpan union(SourceSpan other) { |
| + if (sourceUrl != other.sourceUrl) { |
| + throw new ArgumentError("Source URLs \"${sourceUrl}\" and " |
| + " \"${other.sourceUrl}\" don't match."); |
| + } |
| + |
| + var start = min(this.start, other.start); |
| + var end = max(this.end, other.end); |
| + var beginSpan = start == this.start ? this : other; |
| + var endSpan = end == this.end ? this : other; |
| + |
| + if (beginSpan.end.compareTo(endSpan.start) < 0) { |
| + throw new ArgumentError("Spans $this and $other are disjoint."); |
| + } |
| + |
| + var text = beginSpan.text + |
| + endSpan.text.substring(beginSpan.end.distance(endSpan.start)); |
|
Siggi Cherem (dart-lang)
2014/07/16 21:26:10
for disjoint unions, we could use a text with ' ..
nweiz
2014/07/17 20:22:08
I don't like the idea of making [text] not match [
|
| + return new SourceSpan(start, end, text); |
| + } |
| + |
| + String message(String message, {color}) { |
| + if (color == true) color = colors.RED; |
| + if (color == false) color = null; |
| + |
| + var buffer = new StringBuffer(); |
| + buffer.write('line ${start.line + 1}, column ${start.column + 1}'); |
| + if (sourceUrl != null) buffer.write(' of ${p.prettyUri(sourceUrl)}'); |
| + buffer.write(': $message'); |
| + if (length == 0) return buffer.toString(); |
| + |
| + buffer.write("\n"); |
| + var textLine = text.split("\n").first; |
|
Siggi Cherem (dart-lang)
2014/07/16 21:26:10
not sure I follow why just the first line?
nweiz
2014/07/17 20:22:08
To match the old behavior. I'd like to look into m
|
| + if (color != null) buffer.write(color); |
| + buffer.write(textLine); |
| + buffer.write("\n"); |
| + buffer.write('^' * textLine.length); |
| + if (color != null) buffer.write(colors.NONE); |
| + return buffer.toString(); |
| + } |
| + |
| + bool operator ==(SourceSpan other) => |
| + start == other.start && end == other.end; |
| + |
| + int get hashCode => start.hashCode + (31 * end.hashCode); |
| + |
| + String toString() => '<$runtimeType: from $start to $end "$text">'; |
| +} |