| 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'; | 10 import 'source_code.dart'; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 final bool followLinks; | 25 final bool followLinks; |
| 26 | 26 |
| 27 FormatterOptions(this.reporter, | 27 FormatterOptions(this.reporter, |
| 28 {this.indent: 0, this.pageWidth: 80, this.followLinks: false}); | 28 {this.indent: 0, this.pageWidth: 80, this.followLinks: false}); |
| 29 } | 29 } |
| 30 | 30 |
| 31 /// How the formatter reports the results it produces. | 31 /// How the formatter reports the results it produces. |
| 32 abstract class OutputReporter { | 32 abstract class OutputReporter { |
| 33 /// Prints only the names of files whose contents are different from their | 33 /// Prints only the names of files whose contents are different from their |
| 34 /// formatted version. | 34 /// formatted version. |
| 35 static final dryRun = new _DryRunReporter(); | 35 static final OutputReporter dryRun = new _DryRunReporter(); |
| 36 | 36 |
| 37 /// Prints the formatted results of each file to stdout. | 37 /// Prints the formatted results of each file to stdout. |
| 38 static final print = new _PrintReporter(); | 38 static final OutputReporter print = new _PrintReporter(); |
| 39 | 39 |
| 40 /// Prints the formatted result and selection info of each file to stdout as | 40 /// Prints the formatted result and selection info of each file to stdout as |
| 41 /// a JSON map. | 41 /// a JSON map. |
| 42 static final printJson = new _PrintJsonReporter(); | 42 static final OutputReporter printJson = new _PrintJsonReporter(); |
| 43 | 43 |
| 44 /// Overwrites each file with its formatted result. | 44 /// Overwrites each file with its formatted result. |
| 45 static final overwrite = new _OverwriteReporter(); | 45 static final OutputReporter overwrite = new _OverwriteReporter(); |
| 46 | 46 |
| 47 /// Describe the directory whose contents are about to be processed. | 47 /// Describe the directory whose contents are about to be processed. |
| 48 void showDirectory(String path) {} | 48 void showDirectory(String path) {} |
| 49 | 49 |
| 50 /// Describe the symlink at [path] that wasn't followed. | 50 /// Describe the symlink at [path] that wasn't followed. |
| 51 void showSkippedLink(String path) {} | 51 void showSkippedLink(String path) {} |
| 52 | 52 |
| 53 /// Describe the hidden [path] that wasn't processed. | 53 /// Describe the hidden [path] that wasn't processed. |
| 54 void showHiddenPath(String path) {} | 54 void showHiddenPath(String path) {} |
| 55 | 55 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 "length": output.selectionLength != null ? output.selectionLength : -1 | 109 "length": output.selectionLength != null ? output.selectionLength : -1 |
| 110 } | 110 } |
| 111 })); | 111 })); |
| 112 } | 112 } |
| 113 } | 113 } |
| 114 | 114 |
| 115 /// Overwrites each file with its formatted result. | 115 /// Overwrites each file with its formatted result. |
| 116 class _OverwriteReporter extends _PrintReporter { | 116 class _OverwriteReporter extends _PrintReporter { |
| 117 void afterFile(File file, String label, SourceCode output, {bool changed}) { | 117 void afterFile(File file, String label, SourceCode output, {bool changed}) { |
| 118 if (changed) { | 118 if (changed) { |
| 119 file.writeAsStringSync(output.text); | 119 try { |
| 120 print("Formatted $label"); | 120 file.writeAsStringSync(output.text); |
| 121 print("Formatted $label"); |
| 122 } on FileSystemException catch (err) { |
| 123 stderr.writeln("Could not overwrite $label: " |
| 124 "${err.osError.message} (error code ${err.osError.errorCode})"); |
| 125 } |
| 121 } else { | 126 } else { |
| 122 print("Unchanged $label"); | 127 print("Unchanged $label"); |
| 123 } | 128 } |
| 124 } | 129 } |
| 125 } | 130 } |
| 126 | 131 |
| 127 /// A decorating reporter that reports how long it took for format each file. | 132 /// Base clase for a reporter that decorates an inner reporter. |
| 128 class ProfileReporter implements OutputReporter { | 133 abstract class _ReporterDecorator implements OutputReporter { |
| 129 final OutputReporter _inner; | 134 final OutputReporter _inner; |
| 130 | 135 |
| 136 _ReporterDecorator(this._inner); |
| 137 |
| 138 void showDirectory(String path) { |
| 139 _inner.showDirectory(path); |
| 140 } |
| 141 |
| 142 void showSkippedLink(String path) { |
| 143 _inner.showSkippedLink(path); |
| 144 } |
| 145 |
| 146 void showHiddenPath(String path) { |
| 147 _inner.showHiddenPath(path); |
| 148 } |
| 149 |
| 150 void beforeFile(File file, String label) { |
| 151 _inner.beforeFile(file, label); |
| 152 } |
| 153 |
| 154 void afterFile(File file, String label, SourceCode output, {bool changed}) { |
| 155 _inner.afterFile(file, label, output, changed: changed); |
| 156 } |
| 157 } |
| 158 |
| 159 /// A decorating reporter that reports how long it took for format each file. |
| 160 class ProfileReporter extends _ReporterDecorator { |
| 131 /// The files that have been started but have not completed yet. | 161 /// The files that have been started but have not completed yet. |
| 132 /// | 162 /// |
| 133 /// Maps a file label to the time that it started being formatted. | 163 /// Maps a file label to the time that it started being formatted. |
| 134 final Map<String, DateTime> _ongoing = {}; | 164 final Map<String, DateTime> _ongoing = {}; |
| 135 | 165 |
| 136 /// The elapsed time it took to format each completed file. | 166 /// The elapsed time it took to format each completed file. |
| 137 final Map<String, Duration> _elapsed = {}; | 167 final Map<String, Duration> _elapsed = {}; |
| 138 | 168 |
| 139 /// The number of files that completed so fast that they aren't worth | 169 /// The number of files that completed so fast that they aren't worth |
| 140 /// tracking. | 170 /// tracking. |
| 141 int _elided = 0; | 171 int _elided = 0; |
| 142 | 172 |
| 143 ProfileReporter(this._inner); | 173 ProfileReporter(OutputReporter inner) : super(inner); |
| 144 | 174 |
| 145 /// Show the times for the slowest files to format. | 175 /// Show the times for the slowest files to format. |
| 146 void showProfile() { | 176 void showProfile() { |
| 147 // Everything should be done. | 177 // Everything should be done. |
| 148 assert(_ongoing.isEmpty); | 178 assert(_ongoing.isEmpty); |
| 149 | 179 |
| 150 var files = _elapsed.keys.toList(); | 180 var files = _elapsed.keys.toList(); |
| 151 files.sort((a, b) => _elapsed[b].compareTo(_elapsed[a])); | 181 files.sort((a, b) => _elapsed[b].compareTo(_elapsed[a])); |
| 152 | 182 |
| 153 for (var file in files) { | 183 for (var file in files) { |
| 154 print("${_elapsed[file]}: $file"); | 184 print("${_elapsed[file]}: $file"); |
| 155 } | 185 } |
| 156 | 186 |
| 157 if (_elided >= 1) { | 187 if (_elided >= 1) { |
| 158 var s = _elided > 1 ? 's' : ''; | 188 var s = _elided > 1 ? 's' : ''; |
| 159 print("...$_elided more file$s each took less than 10ms."); | 189 print("...$_elided more file$s each took less than 10ms."); |
| 160 } | 190 } |
| 161 } | 191 } |
| 162 | 192 |
| 163 /// Describe the directory whose contents are about to be processed. | |
| 164 void showDirectory(String path) { | |
| 165 _inner.showDirectory(path); | |
| 166 } | |
| 167 | |
| 168 /// Describe the symlink at [path] that wasn't followed. | |
| 169 void showSkippedLink(String path) { | |
| 170 _inner.showSkippedLink(path); | |
| 171 } | |
| 172 | |
| 173 /// Describe the hidden [path] that wasn't processed. | |
| 174 void showHiddenPath(String path) { | |
| 175 _inner.showHiddenPath(path); | |
| 176 } | |
| 177 | |
| 178 /// Called when [file] is about to be formatted. | 193 /// Called when [file] is about to be formatted. |
| 179 void beforeFile(File file, String label) { | 194 void beforeFile(File file, String label) { |
| 180 _inner.beforeFile(file, label); | 195 super.beforeFile(file, label); |
| 181 | |
| 182 _ongoing[label] = new DateTime.now(); | 196 _ongoing[label] = new DateTime.now(); |
| 183 } | 197 } |
| 184 | 198 |
| 185 /// Describe the processed file at [path] whose formatted result is [output]. | 199 /// Describe the processed file at [path] whose formatted result is [output]. |
| 186 /// | 200 /// |
| 187 /// If the contents of the file are the same as the formatted output, | 201 /// If the contents of the file are the same as the formatted output, |
| 188 /// [changed] will be false. | 202 /// [changed] will be false. |
| 189 void afterFile(File file, String label, SourceCode output, {bool changed}) { | 203 void afterFile(File file, String label, SourceCode output, {bool changed}) { |
| 190 var elapsed = new DateTime.now().difference(_ongoing.remove(label)); | 204 var elapsed = new DateTime.now().difference(_ongoing.remove(label)); |
| 191 if (elapsed.inMilliseconds >= 10) { | 205 if (elapsed.inMilliseconds >= 10) { |
| 192 _elapsed[label] = elapsed; | 206 _elapsed[label] = elapsed; |
| 193 } else { | 207 } else { |
| 194 _elided++; | 208 _elided++; |
| 195 } | 209 } |
| 196 | 210 |
| 197 _inner.afterFile(file, label, output, changed: changed); | 211 super.afterFile(file, label, output, changed: changed); |
| 198 } | 212 } |
| 199 } | 213 } |
| 214 |
| 215 /// A decorating reporter that sets the exit code to 1 if any changes are made. |
| 216 class SetExitReporter extends _ReporterDecorator { |
| 217 SetExitReporter(OutputReporter inner) : super(inner); |
| 218 |
| 219 /// Describe the processed file at [path] whose formatted result is [output]. |
| 220 /// |
| 221 /// If the contents of the file are the same as the formatted output, |
| 222 /// [changed] will be false. |
| 223 void afterFile(File file, String label, SourceCode output, {bool changed}) { |
| 224 if (changed) exitCode = 1; |
| 225 |
| 226 super.afterFile(file, label, output, changed: changed); |
| 227 } |
| 228 } |
| OLD | NEW |