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

Unified Diff: lib/src/file.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/source_span.dart ('k') | lib/src/span_mixin.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/file.dart
diff --git a/lib/src/file.dart b/lib/src/file.dart
index ed5f6a8371226ed65065789a6ec8ee043701426f..c7e58982fe06efc2f6a6a6bcc1f818aabe4d25f3 100644
--- a/lib/src/file.dart
+++ b/lib/src/file.dart
@@ -13,6 +13,7 @@ import 'colors.dart' as colors;
import 'location.dart';
import 'span.dart';
import 'span_mixin.dart';
+import 'span_with_context.dart';
import 'utils.dart';
// Constants to determine end-of-lines.
@@ -183,7 +184,7 @@ class FileLocation extends SourceLocation {
/// [FileSpan.union] will return a [FileSpan] if possible.
///
/// A [FileSpan] can be created using [SourceFile.span].
-class FileSpan extends SourceSpanMixin {
+class FileSpan extends SourceSpanMixin implements SourceSpanWithContext {
/// The [file] that [this] belongs to.
final SourceFile file;
@@ -205,6 +206,12 @@ class FileSpan extends SourceSpanMixin {
FileLocation get end => new FileLocation._(file, _end);
String get text => file.getText(_start, _end);
+ String get context {
+ var line = start.line;
+ return file.getText(file.getOffset(line),
+ line == file.lines - 1 ? null : file.getOffset(line + 1));
+ }
+
FileSpan._(this.file, this._start, this._end) {
if (_end < _start) {
throw new ArgumentError('End $_end must come after start $_start.');
@@ -261,41 +268,4 @@ class FileSpan extends SourceSpanMixin {
var end = math.max(this._end, other._end);
return new FileSpan._(file, start, end);
}
-
- String message(String message, {color}) {
- 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}');
- if (sourceUrl != null) buffer.write(' of ${p.prettyUri(sourceUrl)}');
- buffer.write(': $message\n');
-
- var textLine = file.getText(file.getOffset(line),
- line == file.lines - 1 ? null : file.getOffset(line + 1));
-
- column = math.min(column, textLine.length - 1);
- 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('^' * math.max(toColumn - column, 1));
- if (color != null) buffer.write(colors.NONE);
- return buffer.toString();
- }
}
« no previous file with comments | « lib/source_span.dart ('k') | lib/src/span_mixin.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698