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

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

Issue 1028813002: Introduce span with line context (Closed) Base URL: git@github.com:dart-lang/source_span.git@master
Patch Set: Created 5 years, 9 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
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library source_span.span_mixin; 5 library source_span.span_mixin;
6 6
7 import 'dart:math' as math;
7 import 'package:path/path.dart' as p; 8 import 'package:path/path.dart' as p;
8 9
9 import 'colors.dart' as colors; 10 import 'colors.dart' as colors;
10 import 'span.dart'; 11 import 'span.dart';
11 import 'utils.dart'; 12 import 'utils.dart';
12 13
13 /// A mixin for easily implementing [SourceSpan]. 14 /// A mixin for easily implementing [SourceSpan].
14 /// 15 ///
15 /// This implements the [SourceSpan] methods in terms of [start], [end], and 16 /// This implements the [SourceSpan] methods in terms of [start], [end], and
16 /// [text]. This assumes that [start] and [end] have the same source URL, that 17 /// [text]. This assumes that [start] and [end] have the same source URL, that
(...skipping 25 matching lines...) Expand all
42 43
43 var text = beginSpan.text + 44 var text = beginSpan.text +
44 endSpan.text.substring(beginSpan.end.distance(endSpan.start)); 45 endSpan.text.substring(beginSpan.end.distance(endSpan.start));
45 return new SourceSpan(start, end, text); 46 return new SourceSpan(start, end, text);
46 } 47 }
47 48
48 String message(String message, {color}) { 49 String message(String message, {color}) {
49 if (color == true) color = colors.RED; 50 if (color == true) color = colors.RED;
50 if (color == false) color = null; 51 if (color == false) color = null;
51 52
53 var line = start.line;
54 var column = start.column;
55
52 var buffer = new StringBuffer(); 56 var buffer = new StringBuffer();
53 buffer.write('line ${start.line + 1}, column ${start.column + 1}'); 57 buffer.write('line ${line + 1}, column ${column + 1}');
54 if (sourceUrl != null) buffer.write(' of ${p.prettyUri(sourceUrl)}'); 58 if (sourceUrl != null) buffer.write(' of ${p.prettyUri(sourceUrl)}');
55 buffer.write(': $message'); 59 buffer.write(': $message');
56 if (length == 0) return buffer.toString();
57 60
61 var textLine;
62 if (this is SourceSpanWithContext) {
63 textLine = (this as SourceSpanWithContext).context;
64 column = math.min(column, textLine.length - 1);
65 } else {
66 textLine = text.split("\n").first;
67 column = 0;
68 }
69
70 if (length == 0 && this is! SourceSpanWithContext) return buffer.toString();
58 buffer.write("\n"); 71 buffer.write("\n");
59 var textLine = text.split("\n").first; 72
73 var toColumn =
74 math.min(column + end.offset - start.offset, textLine.length);
75 if (color != null) {
76 buffer.write(textLine.substring(0, column));
77 buffer.write(color);
78 buffer.write(textLine.substring(column, toColumn));
79 buffer.write(colors.NONE);
80 buffer.write(textLine.substring(toColumn));
81 } else {
82 buffer.write(textLine);
83 }
84 if (!textLine.endsWith('\n')) buffer.write('\n');
85 buffer.write(' ' * column);
60 if (color != null) buffer.write(color); 86 if (color != null) buffer.write(color);
61 buffer.write(textLine); 87 buffer.write('^' * math.max(toColumn - column, 1));
62 buffer.write("\n");
63 buffer.write('^' * textLine.length);
64 if (color != null) buffer.write(colors.NONE); 88 if (color != null) buffer.write(colors.NONE);
65 return buffer.toString(); 89 return buffer.toString();
66 } 90 }
67 91
68 bool operator ==(other) => other is SourceSpan && 92 bool operator ==(other) =>
69 start == other.start && end == other.end; 93 other is SourceSpan && start == other.start && end == other.end;
70 94
71 int get hashCode => start.hashCode + (31 * end.hashCode); 95 int get hashCode => start.hashCode + (31 * end.hashCode);
72 96
73 String toString() => '<$runtimeType: from $start to $end "$text">'; 97 String toString() => '<$runtimeType: from $start to $end "$text">';
74 } 98 }
OLDNEW
« lib/src/span.dart ('K') | « lib/src/span.dart ('k') | lib/src/utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698