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

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

Issue 24371003: Formatter selection preservation support. (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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/analyzer_experimental/lib/src/services/formatter_impl.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analyzer_experimental/bin/formatter.dart
===================================================================
--- pkg/analyzer_experimental/bin/formatter.dart (revision 27780)
+++ pkg/analyzer_experimental/bin/formatter.dart (working copy)
@@ -4,6 +4,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
+import 'dart:convert';
import 'dart:io';
import 'dart:utf';
@@ -17,7 +18,9 @@
final dartFileRegExp = new RegExp(r'^[^.].*\.dart$', caseSensitive: false);
final argParser = _initArgParser();
+bool machineFormat;
bool overwriteFileContents;
+Selection selection;
const followLinks = false;
main() {
@@ -26,8 +29,9 @@
_printUsage();
return;
}
- overwriteFileContents = options['write'];
+ _readOptions(options);
+
if (options.rest.isEmpty) {
_formatStdin(options);
} else {
@@ -35,6 +39,29 @@
}
}
+_readOptions(options) {
+ machineFormat = options['machine'];
+ overwriteFileContents = options['write'];
+ selection = _parseSelection(options['selection']);
+}
+
+Selection _parseSelection(selectionOption) {
+ if (selectionOption != null) {
+ var units = selectionOption.split(',');
+ if (units.length == 2) {
+ var offset = _toInt(units[0]);
+ var length = _toInt(units[1]);
+ if (offset != null && length != null) {
+ return new Selection(offset, length);
+ }
+ }
+ throw new FormatterException('Selections are specified as integer pairs '
+ '(e.g., "(offset, length)".');
+ }
+}
+
+int _toInt(str) => int.parse(str, onError: (_) => null);
+
_formatPaths(paths) {
paths.forEach((path) {
if (FileSystemEntity.isDirectorySync(path)) {
@@ -90,6 +117,11 @@
parser.addFlag('write', abbr: 'w', negatable: false,
help: 'Write reformatted sources to files (overwriting contents). '
'Do not print reformatted sources to standard output.');
+ parser.addFlag('machine', abbr: 'm', negatable: false,
+ help: 'Produce output in a format suitable for parsing.');
+ parser.addOption('selection', abbr: 's',
+ help: 'Specify selection information as an offset,length pair '
+ '(e.g., -s "0,4").');
parser.addFlag('help', abbr: 'h', negatable: false,
help: 'Print this usage information.');
return parser;
@@ -115,9 +147,20 @@
}
/// Format the given [src] as a compilation unit.
-String _formatCU(src, {options: const FormatterOptions()}) =>
- new CodeFormatter(options).format(CodeKind.COMPILATION_UNIT, src).source;
+String _formatCU(src, {options: const FormatterOptions()}) {
+ var formatResult = new CodeFormatter(options).format(
+ CodeKind.COMPILATION_UNIT, src, selection: selection);
+ if (machineFormat) {
+ return _toJson(formatResult);
+ }
+ return formatResult.source;
+}
+_toJson(formatResult) =>
+ // Actual JSON format TBD
+ JSON.encode({'source': formatResult.source,
+ 'selection': formatResult.selection.toString()});
+
/// Log the given [msg].
_log(String msg) {
//TODO(pquitslund): add proper log support
« no previous file with comments | « no previous file | pkg/analyzer_experimental/lib/src/services/formatter_impl.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698