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

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/src/colors.dart ('k') | lib/src/location.dart » ('j') | lib/src/span.dart » ('J')
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..daac9a84969ba594e872befb802066c0605f152a 100644
--- a/lib/src/file.dart
+++ b/lib/src/file.dart
@@ -48,8 +48,7 @@ class SourceFile {
/// Creates a new source file from [text].
///
/// [url] may be either a [String], a [Uri], or `null`.
- SourceFile(String text, {url})
- : this.decoded(text.runes, url: url);
+ SourceFile(String text, {url}) : this.decoded(text.runes, url: url);
/// Creates a new source file from a list of decoded characters.
///
@@ -164,8 +163,7 @@ class FileLocation extends SourceLocation {
int get line => file.getLine(offset);
int get column => file.getColumn(offset);
- FileLocation._(this.file, int offset)
- : super(offset) {
+ FileLocation._(this.file, int offset) : super(offset) {
if (offset > file.length) {
throw new RangeError("Offset $offset must not be greater than the number "
"of characters in the file, ${file.length}.");
@@ -183,7 +181,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 +203,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.');
@@ -240,12 +244,13 @@ class FileSpan extends SourceSpanMixin {
bool operator ==(other) {
if (other is! FileSpan) return super == other;
- return _start == other._start && _end == other._end &&
+ return _start == other._start &&
+ _end == other._end &&
sourceUrl == other.sourceUrl;
}
- int get hashCode => _start.hashCode + 5 * _end.hashCode +
- 7 * sourceUrl.hashCode;
+ int get hashCode =>
+ _start.hashCode + 5 * _end.hashCode + 7 * sourceUrl.hashCode;
/// Returns a new span that covers both [this] and [other].
///
@@ -261,41 +266,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/src/colors.dart ('k') | lib/src/location.dart » ('j') | lib/src/span.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698