OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 library error; |
| 5 |
| 6 import 'dart:collection'; |
| 7 |
| 8 import 'generated/error.dart'; |
| 9 import 'generated/source.dart'; |
| 10 |
| 11 /// The maximum line length when printing extracted source code when converting |
| 12 /// an [AnalyzerError] to a string. |
| 13 final _MAX_ERROR_LINE_LENGTH = 120; |
| 14 |
| 15 /// An error class that collects multiple [AnalyzerError]s that are emitted |
| 16 /// during a single analysis. |
| 17 class AnalyzerErrorGroup implements Exception { |
| 18 /// The errors in this collection. |
| 19 List<AnalyzerError> get errors => |
| 20 new UnmodifiableListView<AnalyzerError>(_errors); |
| 21 final List<AnalyzerError> _errors; |
| 22 |
| 23 AnalyzerErrorGroup(Iterable<AnalyzerError> errors) |
| 24 : _errors = errors.toList(); |
| 25 |
| 26 /// Creates an [AnalyzerErrorGroup] from a list of lower-level |
| 27 /// [AnalysisError]s. |
| 28 AnalyzerErrorGroup.fromAnalysisErrors(Iterable<AnalysisError> errors) |
| 29 : this(errors.map((e) => new AnalyzerError(e))); |
| 30 |
| 31 String get message => toString(); |
| 32 String toString() => errors.join("\n"); |
| 33 } |
| 34 |
| 35 /// A wrapper around [AnalysisError] that provides a more user-friendly string |
| 36 /// representation. |
| 37 class AnalyzerError implements Exception { |
| 38 final AnalysisError error; |
| 39 |
| 40 AnalyzerError(this.error); |
| 41 |
| 42 String get message => toString(); |
| 43 |
| 44 String toString() { |
| 45 var builder = new StringBuffer(); |
| 46 |
| 47 // Print a less friendly string representation to ensure that |
| 48 // error.source.contents is not executed, as .contents it isn't async |
| 49 builder.write("Error in ${error.source.shortName}: ${error.message}"); |
| 50 |
| 51 // var content = error.source.contents.data; |
| 52 // var beforeError = content.substring(0, error.offset); |
| 53 // var lineNumber = "\n".allMatches(beforeError).length + 1; |
| 54 // builder.writeln("Error on line $lineNumber of ${error.source.fullName}: " |
| 55 // "${error.message}"); |
| 56 |
| 57 // var errorLineIndex = beforeError.lastIndexOf("\n") + 1; |
| 58 // var errorEndOfLineIndex = content.indexOf("\n", error.offset); |
| 59 // if (errorEndOfLineIndex == -1) errorEndOfLineIndex = content.length; |
| 60 // var errorLine = content.substring( |
| 61 // errorLineIndex, errorEndOfLineIndex); |
| 62 // var errorColumn = error.offset - errorLineIndex; |
| 63 // var errorLength = error.length; |
| 64 // |
| 65 // // Ensure that the error line we display isn't too long. |
| 66 // if (errorLine.length > _MAX_ERROR_LINE_LENGTH) { |
| 67 // var leftLength = errorColumn; |
| 68 // var rightLength = errorLine.length - leftLength; |
| 69 // if (leftLength > _MAX_ERROR_LINE_LENGTH ~/ 2 && |
| 70 // rightLength > _MAX_ERROR_LINE_LENGTH ~/ 2) { |
| 71 // errorLine = "..." + errorLine.substring( |
| 72 // errorColumn - _MAX_ERROR_LINE_LENGTH ~/ 2 + 3, |
| 73 // errorColumn + _MAX_ERROR_LINE_LENGTH ~/ 2 - 3) |
| 74 // + "..."; |
| 75 // errorColumn = _MAX_ERROR_LINE_LENGTH ~/ 2; |
| 76 // } else if (rightLength > _MAX_ERROR_LINE_LENGTH ~/ 2) { |
| 77 // errorLine = errorLine.substring(0, _MAX_ERROR_LINE_LENGTH - 3) + "..."
; |
| 78 // } else { |
| 79 // assert(leftLength > _MAX_ERROR_LINE_LENGTH ~/ 2); |
| 80 // errorColumn -= errorLine.length - _MAX_ERROR_LINE_LENGTH; |
| 81 // errorLine = "..." + errorLine.substring( |
| 82 // errorLine.length - _MAX_ERROR_LINE_LENGTH + 3, errorLine.length); |
| 83 // } |
| 84 // errorLength = math.min(errorLength, _MAX_ERROR_LINE_LENGTH - errorColumn
); |
| 85 // } |
| 86 // builder.writeln(errorLine); |
| 87 // |
| 88 // for (var i = 0; i < errorColumn; i++) builder.write(" "); |
| 89 // for (var i = 0; i < errorLength; i++) builder.write("^"); |
| 90 builder.writeln(); |
| 91 |
| 92 return builder.toString(); |
| 93 } |
| 94 } |
| 95 |
| 96 // A content receiver that collects all the content into a string. |
| 97 class _ContentReceiver implements Source_ContentReceiver { |
| 98 final _buffer = new StringBuffer(); |
| 99 |
| 100 String get result => _buffer.toString(); |
| 101 |
| 102 void accept(String contents, _) => |
| 103 _buffer.write(contents.substring(0, contents.length)); |
| 104 } |
OLD | NEW |