OLD | NEW |
---|---|
1 #!/usr/bin/env dart | 1 #!/usr/bin/env dart |
2 | 2 |
3 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 3 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
4 // for details. All rights reserved. Use of this source code is governed by a | 4 // for details. All rights reserved. Use of this source code is governed by a |
5 // BSD-style license that can be found in the LICENSE file. | 5 // BSD-style license that can be found in the LICENSE file. |
6 | 6 |
7 import 'dart:convert'; | 7 import 'dart:convert'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 | 9 |
10 import 'package:args/args.dart'; | 10 import 'package:args/args.dart'; |
11 import 'package:path/path.dart' as path; | 11 import 'package:path/path.dart' as path; |
12 | 12 |
13 import 'package:analyzer/src/services/formatter_impl.dart'; | 13 import 'package:analyzer/src/services/formatter_impl.dart'; |
14 | 14 |
15 | 15 |
16 const BINARY_NAME = 'dartfmt'; | 16 const BINARY_NAME = 'dartfmt'; |
17 final dartFileRegExp = new RegExp(r'^[^.].*\.dart$', caseSensitive: false); | 17 final dartFileRegExp = new RegExp(r'^[^.].*\.dart$', caseSensitive: false); |
18 final argParser = _initArgParser(); | 18 final argParser = _initArgParser(); |
19 final defaultSelection = new Selection(-1, -1); | 19 final defaultSelection = new Selection(-1, -1); |
20 | 20 |
21 var formatterSettings; | 21 var formatterSettings; |
22 | 22 |
23 CodeKind kind; | 23 CodeKind kind; |
24 bool machineFormat; | 24 bool machineFormat; |
25 bool overwriteFileContents; | 25 bool overwriteFileContents; |
26 Selection selection; | 26 Selection selection; |
27 final List<String> paths = []; | 27 final List<String> paths = []; |
28 | 28 |
29 | |
30 const HELP_FLAG = 'help'; | |
31 const KIND_FLAG = 'kind'; | |
32 const MACHINE_FLAG = 'machine'; | |
33 const WRITE_FLAG = 'write'; | |
34 const SELECTION_FLAG = 'selection'; | |
35 const TRANSFORM_FLAG = 'transform'; | |
36 const MAX_LINE_FLAG = 'max_line_length'; | |
37 | |
38 | |
29 const FOLLOW_LINKS = false; | 39 const FOLLOW_LINKS = false; |
30 | 40 |
31 | 41 |
32 main(args) { | 42 main(args) { |
33 var options = argParser.parse(args); | 43 var options = argParser.parse(args); |
34 if (options['help']) { | 44 if (options['help']) { |
35 _printUsage(); | 45 _printUsage(); |
36 return; | 46 return; |
37 } | 47 } |
38 | 48 |
39 _readOptions(options); | 49 _readOptions(options); |
40 | 50 |
41 if (options.rest.isEmpty) { | 51 if (options.rest.isEmpty) { |
42 _formatStdin(kind); | 52 _formatStdin(kind); |
43 } else { | 53 } else { |
44 paths.addAll(options.rest); | 54 paths.addAll(options.rest); |
45 _formatPaths(paths); | 55 _formatPaths(paths); |
46 } | 56 } |
47 } | 57 } |
48 | 58 |
49 _readOptions(options) { | 59 _readOptions(options) { |
50 kind = _parseKind(options['kind']); | 60 kind = _parseKind(options[KIND_FLAG]); |
51 machineFormat = options['machine']; | 61 machineFormat = options[MACHINE_FLAG]; |
52 overwriteFileContents = options['write']; | 62 overwriteFileContents = options[WRITE_FLAG]; |
53 selection = _parseSelection(options['selection']); | 63 selection = _parseSelection(options[SELECTION_FLAG]); |
54 formatterSettings = | 64 formatterSettings = |
55 new FormatterOptions(codeTransforms: options['transform']); | 65 new FormatterOptions(codeTransforms: options[TRANSFORM_FLAG], |
66 pageWidth: _toInt(options[MAX_LINE_FLAG])); | |
56 } | 67 } |
57 | 68 |
58 CodeKind _parseKind(kindOption) { | 69 CodeKind _parseKind(kindOption) { |
59 switch(kindOption) { | 70 switch(kindOption) { |
60 case 'stmt' : | 71 case 'stmt' : |
61 return CodeKind.STATEMENT; | 72 return CodeKind.STATEMENT; |
62 default: | 73 default: |
63 return CodeKind.COMPILATION_UNIT; | 74 return CodeKind.COMPILATION_UNIT; |
64 } | 75 } |
65 } | 76 } |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
135 stdin.transform(new Utf8Decoder()) | 146 stdin.transform(new Utf8Decoder()) |
136 .listen((data) => input.write(data), | 147 .listen((data) => input.write(data), |
137 onError: (error) => _log('Error reading from stdin'), | 148 onError: (error) => _log('Error reading from stdin'), |
138 onDone: () => print(_format(input.toString(), kind))); | 149 onDone: () => print(_format(input.toString(), kind))); |
139 } | 150 } |
140 | 151 |
141 /// Initialize the arg parser instance. | 152 /// Initialize the arg parser instance. |
142 ArgParser _initArgParser() { | 153 ArgParser _initArgParser() { |
143 // NOTE: these flags are placeholders only! | 154 // NOTE: these flags are placeholders only! |
144 var parser = new ArgParser(); | 155 var parser = new ArgParser(); |
145 parser.addFlag('write', abbr: 'w', negatable: false, | 156 parser.addFlag(WRITE_FLAG, abbr: 'w', negatable: false, |
146 help: 'Write reformatted sources to files (overwriting contents). ' | 157 help: 'Write reformatted sources to files (overwriting contents). ' |
147 'Do not print reformatted sources to standard output.'); | 158 'Do not print reformatted sources to standard output.'); |
148 parser.addOption('kind', abbr: 'k', defaultsTo: 'cu', | 159 parser.addFlag(TRANSFORM_FLAG, abbr: 't', negatable: false, |
160 help: 'Perform code transformations.'); | |
161 parser.addOption(MAX_LINE_FLAG, abbr: 'l', defaultsTo: '80', | |
Brian Wilkerson
2014/01/27 18:38:42
Do we want to support an easy way to not wrap line
pquitslund
2014/01/27 19:06:03
Maybe. I'd rather not add another flag. Do you t
Brian Wilkerson
2014/01/27 19:12:27
I agree. Especially since it would then be possibl
| |
162 help: 'Wrap lines longer than this length.'); | |
163 parser.addOption(KIND_FLAG, abbr: 'k', defaultsTo: 'cu', | |
149 help: 'Specify source snippet kind ("stmt" or "cu")' | 164 help: 'Specify source snippet kind ("stmt" or "cu")' |
150 ' --- [PROVISIONAL API].', hide: true); | 165 ' --- [PROVISIONAL API].', hide: true); |
151 parser.addFlag('machine', abbr: 'm', negatable: false, | 166 parser.addOption(SELECTION_FLAG, abbr: 's', |
152 help: 'Produce output in a format suitable for parsing.'); | |
153 parser.addOption('selection', abbr: 's', | |
154 help: 'Specify selection information as an offset,length pair ' | 167 help: 'Specify selection information as an offset,length pair ' |
155 '(e.g., -s "0,4").', hide: true); | 168 '(e.g., -s "0,4").', hide: true); |
156 parser.addFlag('transform', abbr: 't', negatable: true, | 169 parser.addFlag(MACHINE_FLAG, abbr: 'm', negatable: false, |
157 help: 'Perform code transformations.'); | 170 help: 'Produce output in a format suitable for parsing.'); |
158 parser.addFlag('help', abbr: 'h', negatable: false, | 171 parser.addFlag(HELP_FLAG, abbr: 'h', negatable: false, |
159 help: 'Print this usage information.'); | 172 help: 'Print this usage information.'); |
160 return parser; | 173 return parser; |
161 } | 174 } |
162 | 175 |
163 | 176 |
164 /// Displays usage information. | 177 /// Displays usage information. |
165 _printUsage() { | 178 _printUsage() { |
166 var buffer = new StringBuffer(); | 179 var buffer = new StringBuffer(); |
167 buffer..write('$BINARY_NAME formats Dart programs.') | 180 buffer..write('$BINARY_NAME formats Dart programs.') |
168 ..write('\n\n') | 181 ..write('\n\n') |
(...skipping 30 matching lines...) Expand all Loading... | |
199 'offset': formatResult.selection.offset, | 212 'offset': formatResult.selection.offset, |
200 'length': formatResult.selection.length | 213 'length': formatResult.selection.length |
201 } | 214 } |
202 }); | 215 }); |
203 | 216 |
204 /// Log the given [msg]. | 217 /// Log the given [msg]. |
205 _log(String msg) { | 218 _log(String msg) { |
206 //TODO(pquitslund): add proper log support | 219 //TODO(pquitslund): add proper log support |
207 print(msg); | 220 print(msg); |
208 } | 221 } |
OLD | NEW |