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

Unified Diff: pkg/analyzer/bin/formatter.dart

Issue 82043002: Source kind arg support for dartftmt. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analyzer/bin/formatter.dart
===================================================================
--- pkg/analyzer/bin/formatter.dart (revision 30555)
+++ pkg/analyzer/bin/formatter.dart (working copy)
@@ -20,6 +20,7 @@
var formatterSettings;
+CodeKind kind;
bool machineFormat;
bool overwriteFileContents;
Selection selection;
@@ -36,13 +37,14 @@
_readOptions(options);
if (options.rest.isEmpty) {
- _formatStdin(options);
+ _formatStdin(kind);
} else {
_formatPaths(options.rest);
}
}
_readOptions(options) {
+ kind = _parseKind(options['kind']);
machineFormat = options['machine'];
overwriteFileContents = options['write'];
selection = _parseSelection(options['selection']);
@@ -50,6 +52,15 @@
new FormatterOptions(codeTransforms: options['transform']);
}
+CodeKind _parseKind(kindOption) {
+ switch(kindOption) {
+ case 'stmt' :
+ return CodeKind.STATEMENT;
+ default:
+ return CodeKind.COMPILATION_UNIT;
+ }
+}
+
Selection _parseSelection(selectionOption) {
if (selectionOption != null) {
var units = selectionOption.split(',');
@@ -93,7 +104,7 @@
try {
var buffer = new StringBuffer();
var rawSource = file.readAsStringSync();
- var formatted = _formatCU(rawSource);
+ var formatted = _format(rawSource, CodeKind.COMPILATION_UNIT);
if (overwriteFileContents) {
file.writeAsStringSync(formatted);
} else {
@@ -107,12 +118,12 @@
_isDartFile(file) => dartFileRegExp.hasMatch(path.basename(file.path));
-_formatStdin(options) {
+_formatStdin(kind) {
var input = new StringBuffer();
stdin.transform(new Utf8Decoder())
.listen((data) => input.write(data),
onError: (error) => _log('Error reading from stdin'),
- onDone: () => print(_formatCU(input.toString())));
+ onDone: () => print(_format(input.toString(), kind)));
}
/// Initialize the arg parser instance.
@@ -122,6 +133,9 @@
parser.addFlag('write', abbr: 'w', negatable: false,
help: 'Write reformatted sources to files (overwriting contents). '
'Do not print reformatted sources to standard output.');
+ parser.addOption('kind', abbr: 'k', defaultsTo: 'cu',
+ help: 'Specify source snippet kind ("stmt" or "cu")'
+ ' --- [PROVISIONAL API].');
parser.addFlag('machine', abbr: 'm', negatable: false,
help: 'Produce output in a format suitable for parsing.');
parser.addOption('selection', abbr: 's',
@@ -153,10 +167,10 @@
_log(buffer.toString());
}
-/// Format the given [src] as a compilation unit.
-String _formatCU(src) {
+/// Format this [src], treating it as the given snippet [kind].
+String _format(src, kind) {
var formatResult = new CodeFormatter(formatterSettings).format(
- CodeKind.COMPILATION_UNIT, src, selection: selection);
+ kind, src, selection: selection);
if (machineFormat) {
if (formatResult.selection == null) {
formatResult.selection = defaultSelection;
« 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