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

Unified Diff: packages/dart_style/tool/node_format_service.dart

Issue 2990843002: Removed fixed dependencies (Closed)
Patch Set: Created 3 years, 5 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 | « packages/dart_style/tool/grind.dart ('k') | packages/html/.analysis_options » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: packages/dart_style/tool/node_format_service.dart
diff --git a/packages/dart_style/tool/node_format_service.dart b/packages/dart_style/tool/node_format_service.dart
new file mode 100644
index 0000000000000000000000000000000000000000..d063a3d8d47f54f370f71666741e953339ec768b
--- /dev/null
+++ b/packages/dart_style/tool/node_format_service.dart
@@ -0,0 +1,55 @@
+// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
+// 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:math' as math;
+
+import 'package:js/js.dart';
+
+import 'package:dart_style/dart_style.dart';
+
+@JS()
+@anonymous
+class FormatResult {
+ external factory FormatResult({String code, String error});
+ external String get code;
+ external String get error;
+}
+
+@JS('exports.formatCode')
+external set formatCode(Function formatter);
+
+void main() {
+ formatCode = allowInterop((String source) {
+ var formatter = new DartFormatter();
+
+ var exception;
+ try {
+ return new FormatResult(code: new DartFormatter().format(source));
+ } on FormatterException catch (err) {
+ // Couldn't parse it as a compilation unit.
+ exception = err;
+ }
+
+ // Maybe it's a statement.
+ try {
+ return new FormatResult(code: formatter.formatStatement(source));
+ } on FormatterException catch (err) {
+ // There is an error when parsing it both as a compilation unit and a
+ // statement, so we aren't sure which one the user intended. As a
+ // heuristic, we'll choose that whichever one we managed to parse more of
+ // before hitting an error is probably the right one.
+ if (_firstOffset(exception) < _firstOffset(err)) {
+ exception = err;
+ }
+ }
+
+ // If we get here, it couldn't be parsed at all.
+ return new FormatResult(code: source, error: "$exception");
+ });
+}
+
+/// Returns the offset of the error nearest the beginning of the file out of
+/// all the errors in [exception].
+int _firstOffset(FormatterException exception) =>
+ exception.errors.map((error) => error.offset).reduce(math.min);
« no previous file with comments | « packages/dart_style/tool/grind.dart ('k') | packages/html/.analysis_options » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698