| OLD | NEW |
| 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 Loading... |
| 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 } |
| OLD | NEW |