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