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

Unified Diff: pkg/front_end/lib/src/fasta/messages.dart

Issue 2788373002: Add Source.getTextLine and use it to display source snippets in error messages. (Closed)
Patch Set: dartfmt Created 3 years, 8 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/front_end/lib/src/fasta/messages.dart
diff --git a/pkg/front_end/lib/src/fasta/messages.dart b/pkg/front_end/lib/src/fasta/messages.dart
index 4a2d96dfddf598298c417684c6af4575417e0e07..1876ccc16df2d42b44a467aabae21540b2e10ed2 100644
--- a/pkg/front_end/lib/src/fasta/messages.dart
+++ b/pkg/front_end/lib/src/fasta/messages.dart
@@ -4,7 +4,7 @@
library fasta.messages;
-import 'package:kernel/ast.dart' show Location, Program;
+import 'package:kernel/ast.dart' show Location;
import 'util/relativize.dart' show relativizeUri;
@@ -61,18 +61,28 @@ String colorNit(String message) {
String format(Uri uri, int charOffset, String message) {
if (uri != null) {
String path = relativizeUri(uri);
- String position =
- charOffset == -1 ? path : "${getLocation(path, charOffset)}";
- return "$position: $message";
+ Location location = charOffset == -1 ? null : getLocation(path, charOffset);
+ String sourceLine = getSourceLine(location);
+ if (sourceLine == null) {
+ sourceLine = "";
+ } else {
+ sourceLine = "\n$sourceLine\n"
+ "${' ' * (location.column - 1)}^";
+ }
+ String position = location?.toString() ?? path;
+ return "$position: $message$sourceLine";
} else {
return message;
}
}
Location getLocation(String path, int charOffset) {
- if (CompilerContext.current.uriToSource[path] == null) {
- return new Location(path, 1, 1);
- }
- return new Program(null, CompilerContext.current.uriToSource)
- .getLocation(path, charOffset);
+ return CompilerContext.current.uriToSource[path]
+ ?.getLocation(path, charOffset);
+}
+
+String getSourceLine(Location location) {
+ if (location == null) return null;
+ return CompilerContext.current.uriToSource[location.file]
+ ?.getTextLine(location.line);
}

Powered by Google App Engine
This is Rietveld 408576698