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

Side by Side Diff: source_span/lib/src/span_mixin.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 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
« no previous file with comments | « source_span/lib/src/span_exception.dart ('k') | source_span/lib/src/span_with_context.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.span_mixin;
6
7 import 'dart:math' as math;
8 import 'package:path/path.dart' as p;
9
10 import 'colors.dart' as colors;
11 import 'span.dart';
12 import 'span_with_context.dart';
13 import 'utils.dart';
14
15 /// A mixin for easily implementing [SourceSpan].
16 ///
17 /// This implements the [SourceSpan] methods in terms of [start], [end], and
18 /// [text]. This assumes that [start] and [end] have the same source URL, that
19 /// [start] comes before [end], and that [text] has a number of characters equal
20 /// to the distance between [start] and [end].
21 abstract class SourceSpanMixin implements SourceSpan {
22 Uri get sourceUrl => start.sourceUrl;
23 int get length => end.offset - start.offset;
24
25 int compareTo(SourceSpan other) {
26 var result = start.compareTo(other.start);
27 return result == 0 ? end.compareTo(other.end) : result;
28 }
29
30 SourceSpan union(SourceSpan other) {
31 if (sourceUrl != other.sourceUrl) {
32 throw new ArgumentError("Source URLs \"${sourceUrl}\" and "
33 " \"${other.sourceUrl}\" don't match.");
34 }
35
36 var start = min(this.start, other.start);
37 var end = max(this.end, other.end);
38 var beginSpan = start == this.start ? this : other;
39 var endSpan = end == this.end ? this : other;
40
41 if (beginSpan.end.compareTo(endSpan.start) < 0) {
42 throw new ArgumentError("Spans $this and $other are disjoint.");
43 }
44
45 var text = beginSpan.text +
46 endSpan.text.substring(beginSpan.end.distance(endSpan.start));
47 return new SourceSpan(start, end, text);
48 }
49
50 String message(String message, {color}) {
51 if (color == true) color = colors.RED;
52 if (color == false) color = null;
53
54 var line = start.line;
55 var column = start.column;
56
57 var buffer = new StringBuffer();
58 buffer.write('line ${line + 1}, column ${column + 1}');
59 if (sourceUrl != null) buffer.write(' of ${p.prettyUri(sourceUrl)}');
60 buffer.write(': $message');
61
62 if (length == 0 && this is! SourceSpanWithContext) return buffer.toString();
63 buffer.write("\n");
64
65 var textLine;
66 if (this is SourceSpanWithContext) {
67 var context = (this as SourceSpanWithContext).context;
68 var lineStart = findLineStart(context, text, column);
69 if (lineStart != null && lineStart > 0) {
70 buffer.write(context.substring(0, lineStart));
71 context = context.substring(lineStart);
72 }
73 var endIndex = context.indexOf('\n');
74 textLine = endIndex == -1 ? context : context.substring(0, endIndex + 1);
75 column = math.min(column, textLine.length - 1);
76 } else {
77 textLine = text.split("\n").first;
78 column = 0;
79 }
80
81 var toColumn =
82 math.min(column + end.offset - start.offset, textLine.length);
83 if (color != null) {
84 buffer.write(textLine.substring(0, column));
85 buffer.write(color);
86 buffer.write(textLine.substring(column, toColumn));
87 buffer.write(colors.NONE);
88 buffer.write(textLine.substring(toColumn));
89 } else {
90 buffer.write(textLine);
91 }
92 if (!textLine.endsWith('\n')) buffer.write('\n');
93 buffer.write(' ' * column);
94 if (color != null) buffer.write(color);
95 buffer.write('^' * math.max(toColumn - column, 1));
96 if (color != null) buffer.write(colors.NONE);
97 return buffer.toString();
98 }
99
100 bool operator ==(other) => other is SourceSpan &&
101 start == other.start && end == other.end;
102
103 int get hashCode => start.hashCode + (31 * end.hashCode);
104
105 String toString() => '<$runtimeType: from $start to $end "$text">';
106 }
OLDNEW
« no previous file with comments | « source_span/lib/src/span_exception.dart ('k') | source_span/lib/src/span_with_context.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698