| Index: lib/src/span_mixin.dart
|
| diff --git a/lib/src/span_mixin.dart b/lib/src/span_mixin.dart
|
| index 716e6e07b1f4638c7ecce38a383e5546691f6717..a93723f10420335257147c6b55b4808534e8d542 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_with_context.dart';
|
| import 'utils.dart';
|
|
|
| /// A mixin for easily implementing [SourceSpan].
|
| @@ -49,18 +51,49 @@ 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();
|
|
|
| + if (length == 0 && this is! SourceSpanWithContext) return buffer.toString();
|
| buffer.write("\n");
|
| - var textLine = text.split("\n").first;
|
| +
|
| + var textLine;
|
| + if (this is SourceSpanWithContext) {
|
| + var context = (this as SourceSpanWithContext).context;
|
| + var textIndex = context.indexOf(text.split('\n').first);
|
| + var lineStart = context.lastIndexOf('\n', textIndex);
|
| + if (lineStart != -1) {
|
| + buffer.write(context.substring(0, lineStart + 1));
|
| + context = context.substring(lineStart + 1);
|
| + }
|
| + var endIndex = context.indexOf('\n');
|
| + textLine = endIndex == -1 ? context : context.substring(0, endIndex + 1);
|
| + column = math.min(column, textLine.length - 1);
|
| + } else {
|
| + textLine = text.split("\n").first;
|
| + column = 0;
|
| + }
|
| +
|
| + 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();
|
| }
|
|
|