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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library fasta.messages; 5 library fasta.messages;
6 6
7 import 'package:kernel/ast.dart' show Location, Program; 7 import 'package:kernel/ast.dart' show Location;
8 8
9 import 'util/relativize.dart' show relativizeUri; 9 import 'util/relativize.dart' show relativizeUri;
10 10
11 import 'compiler_context.dart' show CompilerContext; 11 import 'compiler_context.dart' show CompilerContext;
12 12
13 import 'errors.dart' show InputError; 13 import 'errors.dart' show InputError;
14 14
15 import 'colors.dart' show cyan, magenta; 15 import 'colors.dart' show cyan, magenta;
16 16
17 const bool hideWarnings = false; 17 const bool hideWarnings = false;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 54
55 String colorNit(String message) { 55 String colorNit(String message) {
56 // TODO(ahe): Colors need to be optional. Doesn't work well in Emacs or on 56 // TODO(ahe): Colors need to be optional. Doesn't work well in Emacs or on
57 // Windows. 57 // Windows.
58 return cyan(message); 58 return cyan(message);
59 } 59 }
60 60
61 String format(Uri uri, int charOffset, String message) { 61 String format(Uri uri, int charOffset, String message) {
62 if (uri != null) { 62 if (uri != null) {
63 String path = relativizeUri(uri); 63 String path = relativizeUri(uri);
64 String position = 64 Location location = charOffset == -1 ? null : getLocation(path, charOffset);
65 charOffset == -1 ? path : "${getLocation(path, charOffset)}"; 65 String sourceLine = getSourceLine(location);
66 return "$position: $message"; 66 if (sourceLine == null) {
67 sourceLine = "";
68 } else {
69 sourceLine = "\n$sourceLine\n"
70 "${' ' * (location.column - 1)}^";
71 }
72 String position = location?.toString() ?? path;
73 return "$position: $message$sourceLine";
67 } else { 74 } else {
68 return message; 75 return message;
69 } 76 }
70 } 77 }
71 78
72 Location getLocation(String path, int charOffset) { 79 Location getLocation(String path, int charOffset) {
73 if (CompilerContext.current.uriToSource[path] == null) { 80 return CompilerContext.current.uriToSource[path]
74 return new Location(path, 1, 1); 81 ?.getLocation(path, charOffset);
75 }
76 return new Program(null, CompilerContext.current.uriToSource)
77 .getLocation(path, charOffset);
78 } 82 }
83
84 String getSourceLine(Location location) {
85 if (location == null) return null;
86 return CompilerContext.current.uriToSource[location.file]
87 ?.getTextLine(location.line);
88 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698