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

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

Issue 152863006: pkg/analyzer: fix formatter bin crash due to empty selection flag (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: a few more return types Created 6 years, 10 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 FormatterOptions 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 29
30 const HELP_FLAG = 'help'; 30 const HELP_FLAG = 'help';
31 const KIND_FLAG = 'kind'; 31 const KIND_FLAG = 'kind';
32 const MACHINE_FLAG = 'machine'; 32 const MACHINE_FLAG = 'machine';
33 const WRITE_FLAG = 'write'; 33 const WRITE_FLAG = 'write';
34 const SELECTION_FLAG = 'selection'; 34 const SELECTION_FLAG = 'selection';
35 const TRANSFORM_FLAG = 'transform'; 35 const TRANSFORM_FLAG = 'transform';
36 const MAX_LINE_FLAG = 'max_line_length'; 36 const MAX_LINE_FLAG = 'max_line_length';
37 37
38 38
39 const FOLLOW_LINKS = false; 39 const FOLLOW_LINKS = false;
40 40
41 41
42 main(args) { 42 main(List<String> args) {
pquitslund 2014/02/10 04:01:50 Is this ever NOT a List of Strings? What's the be
43 var options = argParser.parse(args); 43 var options = argParser.parse(args);
44 if (options['help']) { 44 if (options['help']) {
45 _printUsage(); 45 _printUsage();
46 return; 46 return;
47 } 47 }
48 48
49 _readOptions(options); 49 _readOptions(options);
50 50
51 if (options.rest.isEmpty) { 51 if (options.rest.isEmpty) {
52 _formatStdin(kind); 52 _formatStdin(kind);
53 } else { 53 } else {
54 paths.addAll(options.rest); 54 paths.addAll(options.rest);
55 _formatPaths(paths); 55 _formatPaths(paths);
56 } 56 }
57 } 57 }
58 58
59 _readOptions(options) { 59 _readOptions(ArgResults options) {
pquitslund 2014/02/10 04:01:50 Really? It's like you're provoking me... ;) Sure
60 kind = _parseKind(options[KIND_FLAG]); 60 kind = _parseKind(options[KIND_FLAG]);
61 machineFormat = options[MACHINE_FLAG]; 61 machineFormat = options[MACHINE_FLAG];
62 overwriteFileContents = options[WRITE_FLAG]; 62 overwriteFileContents = options[WRITE_FLAG];
63 selection = _parseSelection(options[SELECTION_FLAG]); 63 selection = _parseSelection(options[SELECTION_FLAG]);
64 formatterSettings = 64 formatterSettings =
65 new FormatterOptions(codeTransforms: options[TRANSFORM_FLAG], 65 new FormatterOptions(codeTransforms: options[TRANSFORM_FLAG],
66 pageWidth: _parseLineLength(options[MAX_LINE_FLAG])); 66 pageWidth: _parseLineLength(options[MAX_LINE_FLAG]));
67 } 67 }
68 68
69 CodeKind _parseKind(kindOption) { 69 CodeKind _parseKind(String kindOption) {
pquitslund 2014/02/10 04:01:50 OK, and especially here. Isn't it obvious from th
70 switch(kindOption) { 70 switch(kindOption) {
71 case 'stmt' : 71 case 'stmt' :
72 return CodeKind.STATEMENT; 72 return CodeKind.STATEMENT;
73 default: 73 default:
74 return CodeKind.COMPILATION_UNIT; 74 return CodeKind.COMPILATION_UNIT;
75 } 75 }
76 } 76 }
77 77
78 int _parseLineLength(String lengthOption) { 78 int _parseLineLength(String lengthOption) {
79 var length = _toInt(lengthOption); 79 var length = _toInt(lengthOption);
80 if (length == null) { 80 if (length == null) {
81 var val = lengthOption.toUpperCase(); 81 var val = lengthOption.toUpperCase();
82 if (val == 'INF' || val == 'INFINITY') { 82 if (val == 'INF' || val == 'INFINITY') {
83 length = -1; 83 length = -1;
84 } else { 84 } else {
85 throw new FormatterException('Line length is specified as an Integer or ' 85 throw new FormatterException('Line length is specified as an Integer or '
86 'the value "Inf".'); 86 'the value "Inf".');
87 } 87 }
88 } 88 }
89 return length; 89 return length;
90 } 90 }
91 91
92 92
93 Selection _parseSelection(selectionOption) { 93 Selection _parseSelection(String selectionOption) {
94 if (selectionOption != null) { 94 if(selectionOption == null) return null;
95 var units = selectionOption.split(','); 95
96 if (units.length == 2) { 96 var units = selectionOption.split(',');
97 var offset = _toInt(units[0]); 97 if (units.length == 2) {
98 var length = _toInt(units[1]); 98 var offset = _toInt(units[0]);
99 if (offset != null && length != null) { 99 var length = _toInt(units[1]);
100 return new Selection(offset, length); 100 if (offset != null && length != null) {
101 } 101 return new Selection(offset, length);
102 } 102 }
103 } 103 }
104 throw new FormatterException('Selections are specified as integer pairs ' 104 throw new FormatterException('Selections are specified as integer pairs '
105 '(e.g., "(offset, length)".'); 105 '(e.g., "(offset, length)".');
106 } 106 }
107 107
108 int _toInt(str) => int.parse(str, onError: (_) => null); 108 int _toInt(str) => int.parse(str, onError: (_) => null);
109 109
110 _formatPaths(paths) { 110 _formatPaths(paths) {
111 paths.forEach((path) { 111 paths.forEach((path) {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 } 145 }
146 } else { 146 } else {
147 print(formatted); 147 print(formatted);
148 } 148 }
149 } catch (e) { 149 } catch (e) {
150 _log('Unable to format "${file.path}": $e'); 150 _log('Unable to format "${file.path}": $e');
151 } 151 }
152 } 152 }
153 } 153 }
154 154
155 _isPatchFile(file) => file.path.endsWith('_patch.dart'); 155 bool _isPatchFile(file) => file.path.endsWith('_patch.dart');
156 156
157 _isDartFile(file) => dartFileRegExp.hasMatch(path.basename(file.path)); 157 bool _isDartFile(file) => dartFileRegExp.hasMatch(path.basename(file.path));
158 158
159 _formatStdin(kind) { 159 _formatStdin(kind) {
160 var input = new StringBuffer(); 160 var input = new StringBuffer();
161 stdin.transform(new Utf8Decoder()) 161 stdin.transform(new Utf8Decoder())
162 .listen((data) => input.write(data), 162 .listen((data) => input.write(data),
163 onError: (error) => _log('Error reading from stdin'), 163 onError: (error) => _log('Error reading from stdin'),
164 onDone: () => print(_format(input.toString(), kind))); 164 onDone: () => print(_format(input.toString(), kind)));
165 } 165 }
166 166
167 /// Initialize the arg parser instance. 167 /// Initialize the arg parser instance.
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 kind, src, selection: selection); 214 kind, src, selection: selection);
215 if (machineFormat) { 215 if (machineFormat) {
216 if (formatResult.selection == null) { 216 if (formatResult.selection == null) {
217 formatResult.selection = defaultSelection; 217 formatResult.selection = defaultSelection;
218 } 218 }
219 return _toJson(formatResult); 219 return _toJson(formatResult);
220 } 220 }
221 return formatResult.source; 221 return formatResult.source;
222 } 222 }
223 223
224 _toJson(formatResult) => 224 String _toJson(formatResult) =>
225 // Actual JSON format TBD 225 // Actual JSON format TBD
226 JSON.encode({'source': formatResult.source, 226 JSON.encode({'source': formatResult.source,
227 'selection': { 227 'selection': {
228 'offset': formatResult.selection.offset, 228 'offset': formatResult.selection.offset,
229 'length': formatResult.selection.length 229 'length': formatResult.selection.length
230 } 230 }
231 }); 231 });
232 232
233 /// Log the given [msg]. 233 /// Log the given [msg].
234 _log(String msg) { 234 _log(String msg) {
235 //TODO(pquitslund): add proper log support 235 //TODO(pquitslund): add proper log support
236 print(msg); 236 print(msg);
237 } 237 }
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