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

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

Issue 138143009: dartfmt patch file side-stepping. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 11 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 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 const followLinks = false; 27 final List<String> paths = [];
28
29 const FOLLOW_LINKS = false;
28 30
29 31
30 main(args) { 32 main(args) {
31 var options = argParser.parse(args); 33 var options = argParser.parse(args);
32 if (options['help']) { 34 if (options['help']) {
33 _printUsage(); 35 _printUsage();
34 return; 36 return;
35 } 37 }
36 38
37 _readOptions(options); 39 _readOptions(options);
38 40
39 if (options.rest.isEmpty) { 41 if (options.rest.isEmpty) {
40 _formatStdin(kind); 42 _formatStdin(kind);
41 } else { 43 } else {
42 _formatPaths(options.rest); 44 paths.addAll(options.rest);
45 _formatPaths(paths);
43 } 46 }
44 } 47 }
45 48
46 _readOptions(options) { 49 _readOptions(options) {
47 kind = _parseKind(options['kind']); 50 kind = _parseKind(options['kind']);
48 machineFormat = options['machine']; 51 machineFormat = options['machine'];
49 overwriteFileContents = options['write']; 52 overwriteFileContents = options['write'];
50 selection = _parseSelection(options['selection']); 53 selection = _parseSelection(options['selection']);
51 formatterSettings = 54 formatterSettings =
52 new FormatterOptions(codeTransforms: options['transform']); 55 new FormatterOptions(codeTransforms: options['transform']);
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 } 92 }
90 93
91 _formatResource(resource) { 94 _formatResource(resource) {
92 if (resource is Directory) { 95 if (resource is Directory) {
93 _formatDirectory(resource); 96 _formatDirectory(resource);
94 } else if (resource is File) { 97 } else if (resource is File) {
95 _formatFile(resource); 98 _formatFile(resource);
96 } 99 }
97 } 100 }
98 101
99 _formatDirectory(dir) => dir.listSync(followLinks: followLinks) 102 _formatDirectory(dir) => dir.listSync(followLinks: FOLLOW_LINKS)
100 .forEach((resource) => _formatResource(resource)); 103 .forEach((resource) => _formatResource(resource));
101 104
102 _formatFile(file) { 105 _formatFile(file) {
103 if (_isDartFile(file)) { 106 if (_isDartFile(file)) {
107 if (_isPatchFile(file) && !paths.contains(file.path)) {
108 _log('Skipping patch file "${file.path}"');
109 return;
110 }
104 try { 111 try {
105 var buffer = new StringBuffer(); 112 var buffer = new StringBuffer();
106 var rawSource = file.readAsStringSync(); 113 var rawSource = file.readAsStringSync();
107 var formatted = _format(rawSource, CodeKind.COMPILATION_UNIT); 114 var formatted = _format(rawSource, CodeKind.COMPILATION_UNIT);
108 if (overwriteFileContents) { 115 if (overwriteFileContents) {
109 file.writeAsStringSync(formatted); 116 file.writeAsStringSync(formatted);
110 } else { 117 } else {
111 print(formatted); 118 print(formatted);
112 } 119 }
113 } catch (e) { 120 } catch (e) {
114 _log('Unable to format "${file.path}": $e'); 121 _log('Unable to format "${file.path}": $e');
115 } 122 }
116 } 123 }
117 } 124 }
118 125
126 _isPatchFile(file) => file.path.endsWith('_patch.dart');
127
119 _isDartFile(file) => dartFileRegExp.hasMatch(path.basename(file.path)); 128 _isDartFile(file) => dartFileRegExp.hasMatch(path.basename(file.path));
120 129
121 _formatStdin(kind) { 130 _formatStdin(kind) {
122 var input = new StringBuffer(); 131 var input = new StringBuffer();
123 stdin.transform(new Utf8Decoder()) 132 stdin.transform(new Utf8Decoder())
124 .listen((data) => input.write(data), 133 .listen((data) => input.write(data),
125 onError: (error) => _log('Error reading from stdin'), 134 onError: (error) => _log('Error reading from stdin'),
126 onDone: () => print(_format(input.toString(), kind))); 135 onDone: () => print(_format(input.toString(), kind)));
127 } 136 }
128 137
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 'offset': formatResult.selection.offset, 196 'offset': formatResult.selection.offset,
188 'length': formatResult.selection.length 197 'length': formatResult.selection.length
189 } 198 }
190 }); 199 });
191 200
192 /// Log the given [msg]. 201 /// Log the given [msg].
193 _log(String msg) { 202 _log(String msg) {
194 //TODO(pquitslund): add proper log support 203 //TODO(pquitslund): add proper log support
195 print(msg); 204 print(msg);
196 } 205 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698