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

Unified Diff: pkg/kernel/lib/ast.dart

Issue 2788373002: Add Source.getTextLine and use it to display source snippets in error messages. (Closed)
Patch Set: Created 3 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: pkg/kernel/lib/ast.dart
diff --git a/pkg/kernel/lib/ast.dart b/pkg/kernel/lib/ast.dart
index 0112b5a7286a9cfd651dfbe5f032e6e43bbf8ae5..9b531d7b8b92a6b947a902ec23dd98f4545a909e 100644
--- a/pkg/kernel/lib/ast.dart
+++ b/pkg/kernel/lib/ast.dart
@@ -50,6 +50,8 @@
///
library kernel.ast;
+import 'dart:convert' show UTF8;
+
import 'visitor.dart';
export 'visitor.dart';
@@ -4228,9 +4230,36 @@ class _ChildReplacer extends Transformer {
class Source {
final List<int> lineStarts;
+
final List<int> source;
+ String cachedText;
+
Source(this.lineStarts, this.source);
+
+ /// Return the text corresponding to [line] which is a 1-based line number.
+ String getTextLine(int line) {
+ RangeError.checkValueInInterval(
+ line,
+ 1,
+ lineStarts.length,
+ "line",
+ "The value of 'line' ($line) must be between 1 and "
+ "${lineStarts.length}.");
+ if (source == null) return null;
+
+ // -1 as line numbers start at 1.
+ int index = line - 1;
+ if (index + 1 == lineStarts.length) {
+ // Last line.
+ return cachedText.substring(lineStarts[index]);
asgerf 2017/04/03 08:34:55 cachedText could be null here. The initialization
ahe 2017/04/03 09:26:39 Done.
+ } else if (index < lineStarts.length) {
+ cachedText ??= UTF8.decode(source, allowMalformed: true);
+ return cachedText.substring(lineStarts[index], lineStarts[index + 1]);
+ }
+ // This shouldn't happen: should have been caught by the range check above.
+ throw "Internal error";
+ }
}
/// Returns the [Reference] object for the given member.
« pkg/compiler/lib/src/io/source_file.dart ('K') | « pkg/front_end/lib/src/fasta/messages.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698