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

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';
12 import 'span_context.dart';
11 import 'utils.dart'; 13 import 'utils.dart';
12 14
13 /// A mixin for easily implementing [SourceSpan]. 15 /// A mixin for easily implementing [SourceSpan].
14 /// 16 ///
15 /// This implements the [SourceSpan] methods in terms of [start], [end], and 17 /// 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 18 /// [text]. This assumes that [start] and [end] have the same source URL, that
17 /// [start] comes before [end], and that [text] has a number of characters equal 19 /// [start] comes before [end], and that [text] has a number of characters equal
18 /// to the distance between [start] and [end]. 20 /// to the distance between [start] and [end].
19 abstract class SourceSpanMixin implements SourceSpan { 21 abstract class SourceSpanMixin implements SourceSpan {
20 Uri get sourceUrl => start.sourceUrl; 22 Uri get sourceUrl => start.sourceUrl;
(...skipping 21 matching lines...) Expand all
42 44
43 var text = beginSpan.text + 45 var text = beginSpan.text +
44 endSpan.text.substring(beginSpan.end.distance(endSpan.start)); 46 endSpan.text.substring(beginSpan.end.distance(endSpan.start));
45 return new SourceSpan(start, end, text); 47 return new SourceSpan(start, end, text);
46 } 48 }
47 49
48 String message(String message, {color}) { 50 String message(String message, {color}) {
49 if (color == true) color = colors.RED; 51 if (color == true) color = colors.RED;
50 if (color == false) color = null; 52 if (color == false) color = null;
51 53
54 var line = start.line;
55 var column = start.column;
56
52 var buffer = new StringBuffer(); 57 var buffer = new StringBuffer();
53 buffer.write('line ${start.line + 1}, column ${start.column + 1}'); 58 buffer.write('line ${line + 1}, column ${column + 1}');
54 if (sourceUrl != null) buffer.write(' of ${p.prettyUri(sourceUrl)}'); 59 if (sourceUrl != null) buffer.write(' of ${p.prettyUri(sourceUrl)}');
55 buffer.write(': $message'); 60 buffer.write(': $message');
56 if (length == 0) return buffer.toString();
57 61
62 var textLine;
Siggi Cherem (dart-lang) 2015/03/21 00:19:59 this condition is the only new part, the rest of t
63 if (this is SourceSpanContext) {
64 textLine = this.contextLine;
65 column = math.min(column, textLine.length - 1);
66 } else {
67 textLine = text.split("\n").first;
68 column = 0;
69 }
70
71 if (length == 0 && textLine.isEmpty) return buffer.toString();
58 buffer.write("\n"); 72 buffer.write("\n");
59 var textLine = text.split("\n").first; 73
74 var toColumn =
75 math.min(column + end.offset - start.offset, textLine.length);
76 if (color != null) {
77 buffer.write(textLine.substring(0, column));
78 buffer.write(color);
79 buffer.write(textLine.substring(column, toColumn));
80 buffer.write(colors.NONE);
81 buffer.write(textLine.substring(toColumn));
82 } else {
83 buffer.write(textLine);
84 }
85 if (!textLine.endsWith('\n')) buffer.write('\n');
86 buffer.write(' ' * column);
60 if (color != null) buffer.write(color); 87 if (color != null) buffer.write(color);
61 buffer.write(textLine); 88 buffer.write('^' * math.max(toColumn - column, 1));
62 buffer.write("\n");
63 buffer.write('^' * textLine.length);
64 if (color != null) buffer.write(colors.NONE); 89 if (color != null) buffer.write(colors.NONE);
65 return buffer.toString(); 90 return buffer.toString();
66 } 91 }
67 92
68 bool operator ==(other) => other is SourceSpan && 93 bool operator ==(other) => other is SourceSpan &&
69 start == other.start && end == other.end; 94 start == other.start && end == other.end;
70 95
71 int get hashCode => start.hashCode + (31 * end.hashCode); 96 int get hashCode => start.hashCode + (31 * end.hashCode);
72 97
73 String toString() => '<$runtimeType: from $start to $end "$text">'; 98 String toString() => '<$runtimeType: from $start to $end "$text">';
74 } 99 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698