| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 import 'dart:convert'; | 5 import 'dart:convert'; |
| 6 import 'dart:io'; | 6 import 'dart:io'; |
| 7 | 7 |
| 8 import 'package:args/args.dart'; | 8 import 'package:args/args.dart'; |
| 9 | 9 |
| 10 import 'package:dart_style/src/dart_formatter.dart'; | 10 import 'package:dart_style/src/dart_formatter.dart'; |
| 11 import 'package:dart_style/src/formatter_exception.dart'; | 11 import 'package:dart_style/src/formatter_exception.dart'; |
| 12 import 'package:dart_style/src/formatter_options.dart'; | 12 import 'package:dart_style/src/formatter_options.dart'; |
| 13 import 'package:dart_style/src/io.dart'; | 13 import 'package:dart_style/src/io.dart'; |
| 14 import 'package:dart_style/src/source_code.dart'; | 14 import 'package:dart_style/src/source_code.dart'; |
| 15 | 15 |
| 16 // Note: The following line of code is modified by tool/grind.dart. | 16 // Note: The following line of code is modified by tool/grind.dart. |
| 17 const version = "0.2.0+1"; | 17 const version = "0.2.0+1"; |
| 18 | 18 |
| 19 void main(List<String> args) { | 19 void main(List<String> args) { |
| 20 var parser = new ArgParser(allowTrailingOptions: true); | 20 var parser = new ArgParser(allowTrailingOptions: true); |
| 21 | 21 |
| 22 parser.addFlag("help", | 22 parser.addFlag("help", |
| 23 abbr: "h", negatable: false, help: "Shows usage information."); | 23 abbr: "h", negatable: false, help: "Shows usage information."); |
| 24 parser.addFlag("version", | 24 parser.addFlag("version", |
| 25 negatable: false, help: "Shows version information."); | 25 negatable: false, help: "Shows version information."); |
| 26 parser.addOption("line-length", | 26 parser.addOption("line-length", |
| 27 abbr: "l", help: "Wrap lines longer than this.", defaultsTo: "80"); | 27 abbr: "l", help: "Wrap lines longer than this.", defaultsTo: "80"); |
| 28 parser.addOption("indent", |
| 29 abbr: "i", help: "Spaces of leading indentation.", defaultsTo: "0"); |
| 28 parser.addOption("preserve", | 30 parser.addOption("preserve", |
| 29 help: 'Selection to preserve, formatted as "start:length".'); | 31 help: 'Selection to preserve, formatted as "start:length".'); |
| 30 parser.addFlag("dry-run", | 32 parser.addFlag("dry-run", |
| 31 abbr: "n", | 33 abbr: "n", |
| 32 negatable: false, | 34 negatable: false, |
| 33 help: "Show which files would be modified but make no changes."); | 35 help: "Show which files would be modified but make no changes."); |
| 34 parser.addFlag("overwrite", | 36 parser.addFlag("overwrite", |
| 35 abbr: "w", | 37 abbr: "w", |
| 36 negatable: false, | 38 negatable: false, |
| 37 help: "Overwrite input files with formatted output."); | 39 help: "Overwrite input files with formatted output."); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 reporter = OutputReporter.overwrite; | 113 reporter = OutputReporter.overwrite; |
| 112 } else if (argResults["machine"]) { | 114 } else if (argResults["machine"]) { |
| 113 reporter = OutputReporter.printJson; | 115 reporter = OutputReporter.printJson; |
| 114 } | 116 } |
| 115 | 117 |
| 116 if (argResults["profile"]) { | 118 if (argResults["profile"]) { |
| 117 reporter = new ProfileReporter(reporter); | 119 reporter = new ProfileReporter(reporter); |
| 118 } | 120 } |
| 119 | 121 |
| 120 var pageWidth; | 122 var pageWidth; |
| 121 | |
| 122 try { | 123 try { |
| 123 pageWidth = int.parse(argResults["line-length"]); | 124 pageWidth = int.parse(argResults["line-length"]); |
| 124 } on FormatException catch (_) { | 125 } on FormatException catch (_) { |
| 125 usageError( | 126 usageError( |
| 126 parser, | 127 parser, |
| 127 '--line-length must be an integer, was ' | 128 '--line-length must be an integer, was ' |
| 128 '"${argResults['line-length']}".'); | 129 '"${argResults['line-length']}".'); |
| 129 } | 130 } |
| 130 | 131 |
| 132 var indent; |
| 133 |
| 134 try { |
| 135 indent = int.parse(argResults["indent"]); |
| 136 if (indent < 0 || indent.toInt() != indent) throw new FormatException(); |
| 137 } on FormatException catch (_) { |
| 138 usageError( |
| 139 parser, |
| 140 '--indent must be a non-negative integer, was ' |
| 141 '"${argResults['indent']}".'); |
| 142 } |
| 143 |
| 131 var followLinks = argResults["follow-links"]; | 144 var followLinks = argResults["follow-links"]; |
| 132 | 145 |
| 133 var options = new FormatterOptions(reporter, | 146 var options = new FormatterOptions(reporter, |
| 134 pageWidth: pageWidth, followLinks: followLinks); | 147 indent: indent, pageWidth: pageWidth, followLinks: followLinks); |
| 135 | 148 |
| 136 if (argResults.rest.isEmpty) { | 149 if (argResults.rest.isEmpty) { |
| 137 formatStdin(options, selection); | 150 formatStdin(options, selection); |
| 138 } else { | 151 } else { |
| 139 formatPaths(options, argResults.rest); | 152 formatPaths(options, argResults.rest); |
| 140 } | 153 } |
| 141 | 154 |
| 142 if (argResults["profile"]) { | 155 if (argResults["profile"]) { |
| 143 (reporter as ProfileReporter).showProfile(); | 156 (reporter as ProfileReporter).showProfile(); |
| 144 } | 157 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 161 var selectionStart = 0; | 174 var selectionStart = 0; |
| 162 var selectionLength = 0; | 175 var selectionLength = 0; |
| 163 | 176 |
| 164 if (selection != null) { | 177 if (selection != null) { |
| 165 selectionStart = selection[0]; | 178 selectionStart = selection[0]; |
| 166 selectionLength = selection[1]; | 179 selectionLength = selection[1]; |
| 167 } | 180 } |
| 168 | 181 |
| 169 var input = new StringBuffer(); | 182 var input = new StringBuffer(); |
| 170 stdin.transform(new Utf8Decoder()).listen(input.write, onDone: () { | 183 stdin.transform(new Utf8Decoder()).listen(input.write, onDone: () { |
| 171 var formatter = new DartFormatter(pageWidth: options.pageWidth); | 184 var formatter = |
| 185 new DartFormatter(indent: options.indent, pageWidth: options.pageWidth); |
| 172 try { | 186 try { |
| 173 options.reporter.beforeFile(null, "<stdin>"); | 187 options.reporter.beforeFile(null, "<stdin>"); |
| 174 var source = new SourceCode(input.toString(), | 188 var source = new SourceCode(input.toString(), |
| 175 uri: "stdin", | 189 uri: "stdin", |
| 176 selectionStart: selectionStart, | 190 selectionStart: selectionStart, |
| 177 selectionLength: selectionLength); | 191 selectionLength: selectionLength); |
| 178 var output = formatter.formatSource(source); | 192 var output = formatter.formatSource(source); |
| 179 options.reporter | 193 options.reporter |
| 180 .afterFile(null, "<stdin>", output, changed: source != output); | 194 .afterFile(null, "<stdin>", output, changed: source != output); |
| 181 return true; | 195 return true; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 output = stdout; | 243 output = stdout; |
| 230 } | 244 } |
| 231 | 245 |
| 232 output.write("""$message | 246 output.write("""$message |
| 233 | 247 |
| 234 Usage: dartfmt [-n|-w] [files or directories...] | 248 Usage: dartfmt [-n|-w] [files or directories...] |
| 235 | 249 |
| 236 ${parser.usage} | 250 ${parser.usage} |
| 237 """); | 251 """); |
| 238 } | 252 } |
| OLD | NEW |