OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 library error; | 4 library error; |
5 | 5 |
6 import 'dart:collection'; | 6 import 'dart:collection'; |
7 import 'dart:math' as math; | 7 import 'dart:math' as math; |
8 | 8 |
9 import 'generated/error.dart'; | 9 import 'generated/error.dart'; |
10 import 'generated/source.dart'; | 10 import 'generated/source.dart'; |
(...skipping 26 matching lines...) Expand all Loading... |
37 /// representation. | 37 /// representation. |
38 class AnalyzerError implements Exception { | 38 class AnalyzerError implements Exception { |
39 final AnalysisError error; | 39 final AnalysisError error; |
40 | 40 |
41 AnalyzerError(this.error); | 41 AnalyzerError(this.error); |
42 | 42 |
43 String get message => toString(); | 43 String get message => toString(); |
44 | 44 |
45 String toString() { | 45 String toString() { |
46 var builder = new StringBuffer(); | 46 var builder = new StringBuffer(); |
47 var receiver = new _ContentReceiver(); | 47 var content = error.source.contents.data; |
48 error.source.getContents(receiver); | 48 var beforeError = content.substring(0, error.offset); |
49 var beforeError = receiver.result.substring(0, error.offset); | |
50 var lineNumber = "\n".allMatches(beforeError).length + 1; | 49 var lineNumber = "\n".allMatches(beforeError).length + 1; |
51 builder.writeln("Error on line $lineNumber of ${error.source.fullName}: " | 50 builder.writeln("Error on line $lineNumber of ${error.source.fullName}: " |
52 "${error.message}"); | 51 "${error.message}"); |
53 | 52 |
54 var errorLineIndex = beforeError.lastIndexOf("\n") + 1; | 53 var errorLineIndex = beforeError.lastIndexOf("\n") + 1; |
55 var errorEndOfLineIndex = receiver.result.indexOf("\n", error.offset); | 54 var errorEndOfLineIndex = content.indexOf("\n", error.offset); |
56 if (errorEndOfLineIndex == -1) errorEndOfLineIndex = receiver.result.length; | 55 if (errorEndOfLineIndex == -1) errorEndOfLineIndex = content.length; |
57 var errorLine = receiver.result.substring( | 56 var errorLine = content.substring( |
58 errorLineIndex, errorEndOfLineIndex); | 57 errorLineIndex, errorEndOfLineIndex); |
59 var errorColumn = error.offset - errorLineIndex; | 58 var errorColumn = error.offset - errorLineIndex; |
60 var errorLength = error.length; | 59 var errorLength = error.length; |
61 | 60 |
62 // Ensure that the error line we display isn't too long. | 61 // Ensure that the error line we display isn't too long. |
63 if (errorLine.length > _MAX_ERROR_LINE_LENGTH) { | 62 if (errorLine.length > _MAX_ERROR_LINE_LENGTH) { |
64 var leftLength = errorColumn; | 63 var leftLength = errorColumn; |
65 var rightLength = errorLine.length - leftLength; | 64 var rightLength = errorLine.length - leftLength; |
66 if (leftLength > _MAX_ERROR_LINE_LENGTH ~/ 2 && | 65 if (leftLength > _MAX_ERROR_LINE_LENGTH ~/ 2 && |
67 rightLength > _MAX_ERROR_LINE_LENGTH ~/ 2) { | 66 rightLength > _MAX_ERROR_LINE_LENGTH ~/ 2) { |
(...skipping 24 matching lines...) Expand all Loading... |
92 | 91 |
93 // A content receiver that collects all the content into a string. | 92 // A content receiver that collects all the content into a string. |
94 class _ContentReceiver implements Source_ContentReceiver { | 93 class _ContentReceiver implements Source_ContentReceiver { |
95 final _buffer = new StringBuffer(); | 94 final _buffer = new StringBuffer(); |
96 | 95 |
97 String get result => _buffer.toString(); | 96 String get result => _buffer.toString(); |
98 | 97 |
99 void accept(String contents, _) => | 98 void accept(String contents, _) => |
100 _buffer.write(contents.substring(0, contents.length)); | 99 _buffer.write(contents.substring(0, contents.length)); |
101 } | 100 } |
OLD | NEW |