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

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

Issue 23480055: Formatter sanity-checking (via token stream verification). (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 3 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:io'; 7 import 'dart:io';
8 8
9 import 'package:args/args.dart'; 9 import 'package:args/args.dart';
10 import 'package:path/path.dart' as path; 10 import 'package:path/path.dart' as path;
11 11
12 import 'package:analyzer_experimental/src/services/formatter_impl.dart'; 12 import 'package:analyzer_experimental/src/services/formatter_impl.dart';
13 13
14 14
15 const BINARY_NAME = 'dartfmt'; 15 const BINARY_NAME = 'dartfmt';
16 final dartFileRegExp = new RegExp(r'^[^.].*\.dart$', caseSensitive: false); 16 final dartFileRegExp = new RegExp(r'^[^.].*\.dart$', caseSensitive: false);
17 final argParser = _initArgParser(); 17 final argParser = _initArgParser();
18 18
19 bool overwriteFileContents; 19 bool overwriteFileContents;
20 20
21 void main() { 21 main() {
22 var options = argParser.parse(new Options().arguments); 22 var options = argParser.parse(new Options().arguments);
23 if (options['help']) { 23 if (options['help']) {
24 _printUsage(); 24 _printUsage();
25 return; 25 return;
26 } 26 }
27 overwriteFileContents = options['write']; 27 overwriteFileContents = options['write'];
28 28
29 if (options.rest.isEmpty) { 29 if (options.rest.isEmpty) {
30 _formatStdin(options); 30 _formatStdin(options);
31 } else { 31 } else {
32 _formatPaths(options.rest); 32 _formatPaths(options.rest);
33 } 33 }
34 } 34 }
35 35
36 _formatPaths(paths) { 36 _formatPaths(paths) {
37 paths.forEach((path) { 37 paths.forEach((path) {
38 if (FileSystemEntity.isDirectorySync(path)) { 38 if (FileSystemEntity.isDirectorySync(path)) {
39 _formatDirectory(new Directory(path)); 39 _formatDirectory(new Directory(path));
40 } else { 40 } else {
41 _formatFile(new File(path)); 41 _formatFile(new File(path));
42 } 42 }
43 }); 43 });
44 } 44 }
45 45
46 _formatResource(resource) { 46 _formatResource(resource) {
47 if (resource is Directory) { 47 if (resource is Directory) {
48 _formatDirectory(resource); 48 _formatDirectory(resource);
49 } else if (resource is File) { 49 } else if (resource is File) {
50 _formatFile(resource); 50 _formatFile(resource);
51 } 51 }
52 } 52 }
53 53
54 _formatDirectory(dir) => 54 _formatDirectory(dir) =>
55 dir.listSync().forEach((resource) => _formatResource(resource)); 55 dir.listSync().forEach((resource) => _formatResource(resource));
56 56
57 void _formatFile(file) { 57 _formatFile(file) {
58 if (_isDartFile(file)) { 58 if (_isDartFile(file)) {
59 try { 59 try {
60 var buffer = new StringBuffer(); 60 var buffer = new StringBuffer();
61 var rawSource = file.readAsStringSync(); 61 var rawSource = file.readAsStringSync();
62 var formatted = _formatCU(rawSource); 62 var formatted = _formatCU(rawSource);
63 if (overwriteFileContents) { 63 if (overwriteFileContents) {
64 file.writeAsStringSync(formatted); 64 file.writeAsStringSync(formatted);
65 } else { 65 } else {
66 print(formatted); 66 print(formatted);
67 } 67 }
68 } catch (e) { 68 } catch (e) {
69 _log('Error formatting "${file.path}": $e'); 69 _log('Unable to format "${file.path}": $e');
70 } 70 }
71 } 71 }
72 } 72 }
73 73
74 _isDartFile(file) => dartFileRegExp.hasMatch(path.basename(file.path)); 74 _isDartFile(file) => dartFileRegExp.hasMatch(path.basename(file.path));
75 75
76 _formatStdin(options) { 76 _formatStdin(options) {
77 _log('not supported yet!'); 77 _log('not supported yet!');
78 // stdin.transform(new StringDecoder()) 78 // stdin.transform(new StringDecoder())
79 // .listen((String data) => print(data), 79 // .listen((String data) => print(data),
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 114
115 /// Format the given [src] as a compilation unit. 115 /// Format the given [src] as a compilation unit.
116 String _formatCU(src, {options: const FormatterOptions()}) => 116 String _formatCU(src, {options: const FormatterOptions()}) =>
117 new CodeFormatter(options).format(CodeKind.COMPILATION_UNIT, src); 117 new CodeFormatter(options).format(CodeKind.COMPILATION_UNIT, src);
118 118
119 /// Log the given [msg]. 119 /// Log the given [msg].
120 _log(String msg) { 120 _log(String msg) {
121 //TODO(pquitslund): add proper log support 121 //TODO(pquitslund): add proper log support
122 print(msg); 122 print(msg);
123 } 123 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698