OLD | NEW |
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 import 'dart:math' as math; |
6 | 6 |
7 import 'dart:math' as math; | 7 import 'package:charcode/charcode.dart'; |
8 import 'package:path/path.dart' as p; | 8 import 'package:path/path.dart' as p; |
9 | 9 |
10 import 'colors.dart' as colors; | 10 import 'colors.dart' as colors; |
11 import 'span.dart'; | 11 import 'span.dart'; |
12 import 'span_with_context.dart'; | 12 import 'span_with_context.dart'; |
13 import 'utils.dart'; | 13 import 'utils.dart'; |
14 | 14 |
15 /// A mixin for easily implementing [SourceSpan]. | 15 /// A mixin for easily implementing [SourceSpan]. |
16 /// | 16 /// |
17 /// This implements the [SourceSpan] methods in terms of [start], [end], and | 17 /// This implements the [SourceSpan] methods in terms of [start], [end], and |
(...skipping 23 matching lines...) Expand all Loading... |
41 if (beginSpan.end.compareTo(endSpan.start) < 0) { | 41 if (beginSpan.end.compareTo(endSpan.start) < 0) { |
42 throw new ArgumentError("Spans $this and $other are disjoint."); | 42 throw new ArgumentError("Spans $this and $other are disjoint."); |
43 } | 43 } |
44 | 44 |
45 var text = beginSpan.text + | 45 var text = beginSpan.text + |
46 endSpan.text.substring(beginSpan.end.distance(endSpan.start)); | 46 endSpan.text.substring(beginSpan.end.distance(endSpan.start)); |
47 return new SourceSpan(start, end, text); | 47 return new SourceSpan(start, end, text); |
48 } | 48 } |
49 | 49 |
50 String message(String message, {color}) { | 50 String message(String message, {color}) { |
| 51 var buffer = new StringBuffer(); |
| 52 buffer.write('line ${start.line + 1}, column ${start.column + 1}'); |
| 53 if (sourceUrl != null) buffer.write(' of ${p.prettyUri(sourceUrl)}'); |
| 54 buffer.write(': $message'); |
| 55 |
| 56 var highlight = this.highlight(color: color); |
| 57 if (!highlight.isEmpty) { |
| 58 buffer.writeln(); |
| 59 buffer.write(highlight); |
| 60 } |
| 61 |
| 62 return buffer.toString(); |
| 63 } |
| 64 |
| 65 String highlight({color}) { |
51 if (color == true) color = colors.RED; | 66 if (color == true) color = colors.RED; |
52 if (color == false) color = null; | 67 if (color == false) color = null; |
53 | 68 |
54 var line = start.line; | |
55 var column = start.column; | 69 var column = start.column; |
56 | |
57 var buffer = new StringBuffer(); | 70 var buffer = new StringBuffer(); |
58 buffer.write('line ${line + 1}, column ${column + 1}'); | 71 String textLine; |
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) { | 72 if (this is SourceSpanWithContext) { |
67 var context = (this as SourceSpanWithContext).context; | 73 var context = (this as SourceSpanWithContext).context; |
68 var lineStart = findLineStart(context, text, column); | 74 var lineStart = findLineStart(context, text, column); |
69 if (lineStart != null && lineStart > 0) { | 75 if (lineStart != null && lineStart > 0) { |
70 buffer.write(context.substring(0, lineStart)); | 76 buffer.write(context.substring(0, lineStart)); |
71 context = context.substring(lineStart); | 77 context = context.substring(lineStart); |
72 } | 78 } |
73 var endIndex = context.indexOf('\n'); | 79 var endIndex = context.indexOf('\n'); |
74 textLine = endIndex == -1 ? context : context.substring(0, endIndex + 1); | 80 textLine = endIndex == -1 ? context : context.substring(0, endIndex + 1); |
75 column = math.min(column, textLine.length - 1); | 81 column = math.min(column, textLine.length); |
| 82 } else if (length == 0) { |
| 83 return ""; |
76 } else { | 84 } else { |
77 textLine = text.split("\n").first; | 85 textLine = text.split("\n").first; |
78 column = 0; | 86 column = 0; |
79 } | 87 } |
80 | 88 |
81 var toColumn = | 89 var toColumn = |
82 math.min(column + end.offset - start.offset, textLine.length); | 90 math.min(column + end.offset - start.offset, textLine.length); |
83 if (color != null) { | 91 if (color != null) { |
84 buffer.write(textLine.substring(0, column)); | 92 buffer.write(textLine.substring(0, column)); |
85 buffer.write(color); | 93 buffer.write(color); |
86 buffer.write(textLine.substring(column, toColumn)); | 94 buffer.write(textLine.substring(column, toColumn)); |
87 buffer.write(colors.NONE); | 95 buffer.write(colors.NONE); |
88 buffer.write(textLine.substring(toColumn)); | 96 buffer.write(textLine.substring(toColumn)); |
89 } else { | 97 } else { |
90 buffer.write(textLine); | 98 buffer.write(textLine); |
91 } | 99 } |
92 if (!textLine.endsWith('\n')) buffer.write('\n'); | 100 if (!textLine.endsWith('\n')) buffer.write('\n'); |
93 buffer.write(' ' * column); | 101 |
| 102 for (var i = 0; i < column; i++) { |
| 103 if (textLine.codeUnitAt(i) == $tab) { |
| 104 buffer.writeCharCode($tab); |
| 105 } else { |
| 106 buffer.writeCharCode($space); |
| 107 } |
| 108 } |
| 109 |
94 if (color != null) buffer.write(color); | 110 if (color != null) buffer.write(color); |
95 buffer.write('^' * math.max(toColumn - column, 1)); | 111 buffer.write('^' * math.max(toColumn - column, 1)); |
96 if (color != null) buffer.write(colors.NONE); | 112 if (color != null) buffer.write(colors.NONE); |
97 return buffer.toString(); | 113 return buffer.toString(); |
98 } | 114 } |
99 | 115 |
100 bool operator ==(other) => other is SourceSpan && | 116 bool operator ==(other) => other is SourceSpan && |
101 start == other.start && end == other.end; | 117 start == other.start && end == other.end; |
102 | 118 |
103 int get hashCode => start.hashCode + (31 * end.hashCode); | 119 int get hashCode => start.hashCode + (31 * end.hashCode); |
104 | 120 |
105 String toString() => '<$runtimeType: from $start to $end "$text">'; | 121 String toString() => '<$runtimeType: from $start to $end "$text">'; |
106 } | 122 } |
OLD | NEW |