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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: lib/src/span_mixin.dart
diff --git a/lib/src/span_mixin.dart b/lib/src/span_mixin.dart
index 716e6e07b1f4638c7ecce38a383e5546691f6717..b0e74f06481ae555aa351e9d28e9b483e140faf1 100644
--- a/lib/src/span_mixin.dart
+++ b/lib/src/span_mixin.dart
@@ -4,6 +4,7 @@
library source_span.span_mixin;
+import 'dart:math' as math;
import 'package:path/path.dart' as p;
import 'colors.dart' as colors;
@@ -49,24 +50,47 @@ abstract class SourceSpanMixin implements SourceSpan {
if (color == true) color = colors.RED;
if (color == false) color = null;
+ var line = start.line;
+ var column = start.column;
+
var buffer = new StringBuffer();
- buffer.write('line ${start.line + 1}, column ${start.column + 1}');
+ buffer.write('line ${line + 1}, column ${column + 1}');
if (sourceUrl != null) buffer.write(' of ${p.prettyUri(sourceUrl)}');
buffer.write(': $message');
- if (length == 0) return buffer.toString();
+ var textLine;
+ if (this is SourceSpanWithContext) {
+ textLine = (this as SourceSpanWithContext).context;
+ column = math.min(column, textLine.length - 1);
+ } else {
+ textLine = text.split("\n").first;
+ column = 0;
+ }
+
+ if (length == 0 && this is! SourceSpanWithContext) return buffer.toString();
buffer.write("\n");
- var textLine = text.split("\n").first;
+
+ var toColumn =
+ math.min(column + end.offset - start.offset, textLine.length);
+ if (color != null) {
+ buffer.write(textLine.substring(0, column));
+ buffer.write(color);
+ buffer.write(textLine.substring(column, toColumn));
+ buffer.write(colors.NONE);
+ buffer.write(textLine.substring(toColumn));
+ } else {
+ buffer.write(textLine);
+ }
+ if (!textLine.endsWith('\n')) buffer.write('\n');
+ buffer.write(' ' * column);
if (color != null) buffer.write(color);
- buffer.write(textLine);
- buffer.write("\n");
- buffer.write('^' * textLine.length);
+ buffer.write('^' * math.max(toColumn - column, 1));
if (color != null) buffer.write(colors.NONE);
return buffer.toString();
}
- bool operator ==(other) => other is SourceSpan &&
- start == other.start && end == other.end;
+ bool operator ==(other) =>
+ other is SourceSpan && start == other.start && end == other.end;
int get hashCode => start.hashCode + (31 * end.hashCode);
« 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