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

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
« no previous file with comments | « lib/src/file.dart ('k') | lib/src/span_with_context.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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();
}
« 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