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

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..2f5da15085c3d85b65a38ff3d77b684e2412da2f 100644
--- a/lib/src/span_mixin.dart
+++ b/lib/src/span_mixin.dart
@@ -4,10 +4,12 @@
library source_span.span_mixin;
+import 'dart:math' as math;
import 'package:path/path.dart' as p;
import 'colors.dart' as colors;
import 'span.dart';
+import 'span_context.dart';
import 'utils.dart';
/// A mixin for easily implementing [SourceSpan].
@@ -49,18 +51,41 @@ 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;
Siggi Cherem (dart-lang) 2015/03/21 00:19:59 this condition is the only new part, the rest of t
+ if (this is SourceSpanContext) {
+ textLine = this.contextLine;
+ column = math.min(column, textLine.length - 1);
+ } else {
+ textLine = text.split("\n").first;
+ column = 0;
+ }
+
+ if (length == 0 && textLine.isEmpty) 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();
}

Powered by Google App Engine
This is Rietveld 408576698