OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014, 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 | |
5 library dart_style.src.formatter_exception; | |
6 | |
7 import 'package:analyzer/analyzer.dart'; | |
8 import 'package:source_span/source_span.dart'; | |
9 | |
10 /// Thrown when one or more errors occurs while parsing the code to be | |
11 /// formatted. | |
12 class FormatterException implements Exception { | |
13 /// The [AnalysisError]s that occurred. | |
14 final List<AnalysisError> errors; | |
15 | |
16 /// Creates a new FormatterException with an optional error [message]. | |
17 const FormatterException(this.errors); | |
18 | |
19 /// Creates a human-friendly representation of the analysis errors. | |
20 String message({bool color}) { | |
21 var buffer = new StringBuffer(); | |
22 buffer.writeln("Could not format because the source could not be parsed:"); | |
23 | |
24 // In case we get a huge series of cascaded errors, just show the first few. | |
25 var shownErrors = errors; | |
26 if (errors.length > 10) shownErrors = errors.take(10); | |
27 | |
28 for (var error in shownErrors) { | |
29 var source = error.source.contents.data; | |
30 | |
31 // If the parse error is for something missing from the end of the file, | |
32 // the error position will go past the end of the source. In that case, | |
33 // just pad the source with spaces so we can report it nicely. | |
34 if (error.offset + error.length > source.length) { | |
35 source += " " * (error.offset + error.length - source.length); | |
36 } | |
37 | |
38 var file = new SourceFile(source, url: error.source.fullName); | |
39 var span = file.span(error.offset, error.offset + error.length); | |
40 if (buffer.isNotEmpty) buffer.writeln(); | |
41 buffer.write(span.message(error.message, color: color)); | |
42 } | |
43 | |
44 if (shownErrors.length != errors.length) { | |
45 buffer.writeln(); | |
46 buffer.write("(${errors.length - shownErrors.length} more errors...)"); | |
47 } | |
48 | |
49 return buffer.toString(); | |
50 } | |
51 | |
52 String toString() => message(); | |
53 } | |
OLD | NEW |