Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1072)

Side by Side Diff: pkg/analyzer_experimental/bin/formatter.dart

Issue 26280005: Formatter code transform bit-flip support. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 import 'dart:utf'; 9 import 'dart:utf';
10 10
11 import 'package:args/args.dart'; 11 import 'package:args/args.dart';
12 import 'package:path/path.dart' as path; 12 import 'package:path/path.dart' as path;
13 13
14 import 'package:analyzer_experimental/src/services/formatter_impl.dart'; 14 import 'package:analyzer_experimental/src/services/formatter_impl.dart';
15 15
16 16
17 const BINARY_NAME = 'dartfmt'; 17 const BINARY_NAME = 'dartfmt';
18 final dartFileRegExp = new RegExp(r'^[^.].*\.dart$', caseSensitive: false); 18 final dartFileRegExp = new RegExp(r'^[^.].*\.dart$', caseSensitive: false);
19 final argParser = _initArgParser(); 19 final argParser = _initArgParser();
20 final defaultSelection = new Selection(-1, -1);
20 21
21 bool machineFormat; 22 bool machineFormat;
22 bool overwriteFileContents; 23 bool overwriteFileContents;
23 Selection selection; 24 Selection selection;
24 const followLinks = false; 25 const followLinks = false;
25 26
26 main() { 27 main() {
27 var options = argParser.parse(new Options().arguments); 28 var options = argParser.parse(new Options().arguments);
28 if (options['help']) { 29 if (options['help']) {
29 _printUsage(); 30 _printUsage();
30 return; 31 return;
31 } 32 }
32 33
33 _readOptions(options); 34 _readOptions(options);
34 35
35 if (options.rest.isEmpty) { 36 if (options.rest.isEmpty) {
36 _formatStdin(options); 37 _formatStdin(options);
37 } else { 38 } else {
38 _formatPaths(options.rest); 39 _formatPaths(options.rest);
39 } 40 }
40 } 41 }
41 42
42 _readOptions(options) { 43 _readOptions(options) {
43 machineFormat = options['machine']; 44 machineFormat = options['machine'];
44 overwriteFileContents = options['write']; 45 overwriteFileContents = options['write'];
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 // NOTE: these flags are placeholders only! 116 // NOTE: these flags are placeholders only!
116 var parser = new ArgParser(); 117 var parser = new ArgParser();
117 parser.addFlag('write', abbr: 'w', negatable: false, 118 parser.addFlag('write', abbr: 'w', negatable: false,
118 help: 'Write reformatted sources to files (overwriting contents). ' 119 help: 'Write reformatted sources to files (overwriting contents). '
119 'Do not print reformatted sources to standard output.'); 120 'Do not print reformatted sources to standard output.');
120 parser.addFlag('machine', abbr: 'm', negatable: false, 121 parser.addFlag('machine', abbr: 'm', negatable: false,
121 help: 'Produce output in a format suitable for parsing.'); 122 help: 'Produce output in a format suitable for parsing.');
122 parser.addOption('selection', abbr: 's', 123 parser.addOption('selection', abbr: 's',
123 help: 'Specify selection information as an offset,length pair ' 124 help: 'Specify selection information as an offset,length pair '
124 '(e.g., -s "0,4").'); 125 '(e.g., -s "0,4").');
126 parser.addFlag('transform', abbr: 't', negatable: true,
127 help: 'Perform code transformations.');
125 parser.addFlag('help', abbr: 'h', negatable: false, 128 parser.addFlag('help', abbr: 'h', negatable: false,
126 help: 'Print this usage information.'); 129 help: 'Print this usage information.');
127 return parser; 130 return parser;
128 } 131 }
129 132
130 133
131 /// Displays usage information. 134 /// Displays usage information.
132 _printUsage() { 135 _printUsage() {
133 var buffer = new StringBuffer(); 136 var buffer = new StringBuffer();
134 buffer..write('$BINARY_NAME formats Dart programs.') 137 buffer..write('$BINARY_NAME formats Dart programs.')
135 ..write('\n\n') 138 ..write('\n\n')
136 ..write('Without an explicit path, $BINARY_NAME processes the standard ' 139 ..write('Without an explicit path, $BINARY_NAME processes the standard '
137 'input. Given a file, it operates on that file; given a ' 140 'input. Given a file, it operates on that file; given a '
138 'directory, it operates on all .dart files in that directory, ' 141 'directory, it operates on all .dart files in that directory, '
139 'recursively. (Files starting with a period are ignored.) By ' 142 'recursively. (Files starting with a period are ignored.) By '
140 'default, $BINARY_NAME prints the reformatted sources to ' 143 'default, $BINARY_NAME prints the reformatted sources to '
141 'standard output.') 144 'standard output.')
142 ..write('\n\n') 145 ..write('\n\n')
143 ..write('Supported flags are:') 146 ..write('Supported flags are:')
144 ..write('Usage: $BINARY_NAME [flags] [path...]\n\n') 147 ..write('Usage: $BINARY_NAME [flags] [path...]\n\n')
145 ..write('${argParser.getUsage()}\n\n'); 148 ..write('${argParser.getUsage()}\n\n');
146 _log(buffer.toString()); 149 _log(buffer.toString());
147 } 150 }
148 151
149 /// Format the given [src] as a compilation unit. 152 /// Format the given [src] as a compilation unit.
150 String _formatCU(src, {options: const FormatterOptions()}) { 153 String _formatCU(src, {options: const FormatterOptions()}) {
151 var formatResult = new CodeFormatter(options).format( 154 var formatResult = new CodeFormatter(options).format(
152 CodeKind.COMPILATION_UNIT, src, selection: selection); 155 CodeKind.COMPILATION_UNIT, src, selection: selection);
153 if (machineFormat) { 156 if (machineFormat) {
157 if (formatResult.selection == null) {
158 formatResult.selection = defaultSelection;
159 }
154 return _toJson(formatResult); 160 return _toJson(formatResult);
155 } 161 }
156 return formatResult.source; 162 return formatResult.source;
157 } 163 }
158 164
159 _toJson(formatResult) => 165 _toJson(formatResult) =>
160 // Actual JSON format TBD 166 // Actual JSON format TBD
161 JSON.encode({'source': formatResult.source, 167 JSON.encode({'source': formatResult.source,
162 'selection': { 168 'selection': {
163 'offset': formatResult.selection.offset, 169 'offset': formatResult.selection.offset,
164 'length': formatResult.selection.length 170 'length': formatResult.selection.length
165 } 171 }
166 }); 172 });
167 173
168 /// Log the given [msg]. 174 /// Log the given [msg].
169 _log(String msg) { 175 _log(String msg) {
170 //TODO(pquitslund): add proper log support 176 //TODO(pquitslund): add proper log support
171 print(msg); 177 print(msg);
172 } 178 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698