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_options; | |
6 | |
7 import 'dart:convert'; | |
8 import 'dart:io'; | |
9 | |
10 import 'source_code.dart'; | |
11 | |
12 /// Global options that affect how the formatter produces and uses its outputs. | |
13 class FormatterOptions { | |
14 /// The [OutputReporter] used to show the formatting results. | |
15 final OutputReporter reporter; | |
16 | |
17 /// The number of columns that formatted output should be constrained to fit | |
18 /// within. | |
19 final int pageWidth; | |
20 | |
21 /// Whether symlinks should be traversed when formatting a directory. | |
22 final bool followLinks; | |
23 | |
24 FormatterOptions(this.reporter, | |
25 {this.pageWidth: 80, this.followLinks: false}); | |
26 } | |
27 | |
28 /// How the formatter reports the results it produces. | |
29 abstract class OutputReporter { | |
30 /// Prints only the names of files whose contents are different from their | |
31 /// formatted version. | |
32 static final dryRun = new _DryRunReporter(); | |
33 | |
34 /// Prints the formatted results of each file to stdout. | |
35 static final print = new _PrintReporter(); | |
36 | |
37 /// Prints the formatted result and selection info of each file to stdout as | |
38 /// a JSON map. | |
39 static final printJson = new _PrintJsonReporter(); | |
40 | |
41 /// Overwrites each file with its formatted result. | |
42 static final overwrite = new _OverwriteReporter(); | |
43 | |
44 /// Describe the directory whose contents are about to be processed. | |
45 void showDirectory(String path) {} | |
46 | |
47 /// Describe the symlink at [path] that wasn't followed. | |
48 void showSkippedLink(String path) {} | |
49 | |
50 /// Describe the hidden file at [path] that wasn't processed. | |
51 void showHiddenFile(String path) {} | |
52 | |
53 /// Describe the processed file at [path] whose formatted result is [output]. | |
54 /// | |
55 /// If the contents of the file are the same as the formatted output, | |
56 /// [changed] will be false. | |
57 void showFile(File file, String label, SourceCode output, {bool changed}); | |
58 } | |
59 | |
60 /// Prints only the names of files whose contents are different from their | |
61 /// formatted version. | |
62 class _DryRunReporter extends OutputReporter { | |
63 void showFile(File file, String label, SourceCode output, {bool changed}) { | |
64 // Only show the changed files. | |
65 if (changed) print(label); | |
66 } | |
67 } | |
68 | |
69 /// Prints the formatted results of each file to stdout. | |
70 class _PrintReporter extends OutputReporter { | |
71 void showDirectory(String path) { | |
72 print("Formatting directory $path:"); | |
73 } | |
74 | |
75 void showSkippedLink(String path) { | |
76 print("Skipping link $path"); | |
77 } | |
78 | |
79 void showHiddenFile(String path) { | |
80 print("Skipping hidden file $path"); | |
81 } | |
82 | |
83 void showFile(File file, String label, SourceCode output, {bool changed}) { | |
84 // Don't add an extra newline. | |
85 stdout.write(output.text); | |
86 } | |
87 } | |
88 | |
89 /// Prints the formatted result and selection info of each file to stdout as a | |
90 /// JSON map. | |
91 class _PrintJsonReporter extends OutputReporter { | |
92 void showFile(File file, String label, SourceCode output, {bool changed}) { | |
93 // TODO(rnystrom): Put an empty selection in here to remain compatible with | |
94 // the old formatter. Since there's no way to pass a selection on the | |
95 // command line, this will never be used, which is why it's hard-coded to | |
96 // -1, -1. If we add support for passing in a selection, put the real | |
97 // result here. | |
98 print(JSON.encode({ | |
99 "path": label, | |
100 "source": output.text, | |
101 "selection": { | |
102 "offset": output.selectionStart != null ? output.selectionStart : -1, | |
103 "length": output.selectionLength != null ? output.selectionLength : -1 | |
104 } | |
105 })); | |
106 } | |
107 } | |
108 | |
109 /// Overwrites each file with its formatted result. | |
110 class _OverwriteReporter extends _PrintReporter { | |
111 void showFile(File file, String label, SourceCode output, {bool changed}) { | |
112 if (changed) { | |
113 file.writeAsStringSync(output.text); | |
114 print("Formatted $label"); | |
115 } else { | |
116 print("Unchanged $label"); | |
117 } | |
118 } | |
119 } | |
OLD | NEW |