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

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, 8 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 | « lib/src/file.dart ('k') | 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
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_with_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 if (length == 0 && this is! SourceSpanWithContext) return buffer.toString();
58 buffer.write("\n"); 63 buffer.write("\n");
59 var textLine = text.split("\n").first; 64
65 var textLine;
66 if (this is SourceSpanWithContext) {
67 var context = (this as SourceSpanWithContext).context;
68 var textIndex = context.indexOf(text.split('\n').first);
69 var lineStart = context.lastIndexOf('\n', textIndex);
70 if (lineStart != -1) {
71 buffer.write(context.substring(0, lineStart + 1));
72 context = context.substring(lineStart + 1);
73 }
74 var endIndex = context.indexOf('\n');
75 textLine = endIndex == -1 ? context : context.substring(0, endIndex + 1);
76 column = math.min(column, textLine.length - 1);
77 } else {
78 textLine = text.split("\n").first;
79 column = 0;
80 }
81
82 var toColumn =
83 math.min(column + end.offset - start.offset, textLine.length);
84 if (color != null) {
85 buffer.write(textLine.substring(0, column));
86 buffer.write(color);
87 buffer.write(textLine.substring(column, toColumn));
88 buffer.write(colors.NONE);
89 buffer.write(textLine.substring(toColumn));
90 } else {
91 buffer.write(textLine);
92 }
93 if (!textLine.endsWith('\n')) buffer.write('\n');
94 buffer.write(' ' * column);
60 if (color != null) buffer.write(color); 95 if (color != null) buffer.write(color);
61 buffer.write(textLine); 96 buffer.write('^' * math.max(toColumn - column, 1));
62 buffer.write("\n");
63 buffer.write('^' * textLine.length);
64 if (color != null) buffer.write(colors.NONE); 97 if (color != null) buffer.write(colors.NONE);
65 return buffer.toString(); 98 return buffer.toString();
66 } 99 }
67 100
68 bool operator ==(other) => other is SourceSpan && 101 bool operator ==(other) => other is SourceSpan &&
69 start == other.start && end == other.end; 102 start == other.start && end == other.end;
70 103
71 int get hashCode => start.hashCode + (31 * end.hashCode); 104 int get hashCode => start.hashCode + (31 * end.hashCode);
72 105
73 String toString() => '<$runtimeType: from $start to $end "$text">'; 106 String toString() => '<$runtimeType: from $start to $end "$text">';
74 } 107 }
OLDNEW
« no previous file with comments | « lib/src/file.dart ('k') | lib/src/span_with_context.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698