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/error/error.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 | 1 | const FormatterException(this.errors);
|
18 | |
|
19 | | /// Creates a human-friendly representation of the analysis errors.
|
20 | 1 | String message({bool color}) {
|
21 | 1 | var buffer = new StringBuffer();
|
22 | 1 | 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 | 1 | var shownErrors = errors;
|
26 | 3 | if (errors.length > 10) shownErrors = errors.take(10).toList();
|
27 | |
|
28 | 2 | for (var error in shownErrors) {
|
29 | 3 | 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 | 5 | if (error.offset + error.length > source.length) {
|
35 | 0 | source += " " * (error.offset + error.length - source.length);
|
36 | | }
|
37 | |
|
38 | 3 | var file = new SourceFile.fromString(source, url: error.source.fullName);
|
39 | 5 | var span = file.span(error.offset, error.offset + error.length);
|
40 | 2 | if (buffer.isNotEmpty) buffer.writeln();
|
41 | 3 | buffer.write(span.message(error.message, color: color));
|
42 | | }
|
43 | |
|
44 | 4 | if (shownErrors.length != errors.length) {
|
45 | 0 | buffer.writeln();
|
46 | 0 | buffer.write("(${errors.length - shownErrors.length} more errors...)");
|
47 | | }
|
48 | |
|
49 | 1 | return buffer.toString();
|
50 | | }
|
51 | |
|
52 | 0 | String toString() => message();
|
53 | | }
|
54 | |
|
55 | | /// Exception thrown when the internal sanity check that only whitespace
|
56 | | /// changes are made fails.
|
57 | | class UnexpectedOutputException implements Exception {
|
58 | | /// The source being formatted.
|
59 | | final String _input;
|
60 | |
|
61 | | /// The resulting output.
|
62 | | final String _output;
|
63 | |
|
64 | 1 | UnexpectedOutputException(this._input, this._output);
|
65 | |
|
66 | 0 | String toString() {
|
67 | | return """The formatter produced unexpected output. Input was:
|
68 | 0 | $_input
|
69 | | Which formatted to:
|
70 | 0 | $_output""";
|
71 | | }
|
72 | | }
|