| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:math' as math; | 5 import 'dart:math' as math; |
| 6 | 6 |
| 7 import 'package:js/js.dart'; | 7 import 'package:js/js.dart'; |
| 8 | 8 |
| 9 import 'package:dart_style/dart_style.dart'; | 9 import 'package:dart_style/dart_style.dart'; |
| 10 | 10 |
| 11 @JS() | 11 @JS() |
| 12 @anonymous | 12 @anonymous |
| 13 class FormatResult { | 13 class FormatResult { |
| 14 external factory FormatResult({String code, String error}); | 14 external factory FormatResult({String code, String error}); |
| 15 external String get code; | 15 external String get code; |
| 16 external String get error; | 16 external String get error; |
| 17 } | 17 } |
| 18 | 18 |
| 19 @JS('exports.formatCode') | 19 @JS('exports.formatCode') |
| 20 external set formatCode(Function formatter); | 20 external set formatCode(Function formatter); |
| 21 | 21 |
| 22 void main() { | 22 void main() { |
| 23 formatCode = allowInterop((String source) { | 23 formatCode = allowInterop((String source) { |
| 24 var formatter = new DartFormatter(); | 24 var formatter = new DartFormatter(); |
| 25 | 25 |
| 26 var exception; | 26 var exception; |
| 27 try { | 27 try { |
| 28 return new FormatResult( | 28 return new FormatResult(code: new DartFormatter().format(source)); |
| 29 code: new DartFormatter().format(source)); | |
| 30 } on FormatterException catch (err) { | 29 } on FormatterException catch (err) { |
| 31 // Couldn't parse it as a compilation unit. | 30 // Couldn't parse it as a compilation unit. |
| 32 exception = err; | 31 exception = err; |
| 33 } | 32 } |
| 34 | 33 |
| 35 // Maybe it's a statement. | 34 // Maybe it's a statement. |
| 36 try { | 35 try { |
| 37 return new FormatResult( | 36 return new FormatResult(code: formatter.formatStatement(source)); |
| 38 code: formatter.formatStatement(source)); | |
| 39 } on FormatterException catch (err) { | 37 } on FormatterException catch (err) { |
| 40 // There is an error when parsing it both as a compilation unit and a | 38 // There is an error when parsing it both as a compilation unit and a |
| 41 // statement, so we aren't sure which one the user intended. As a | 39 // statement, so we aren't sure which one the user intended. As a |
| 42 // heuristic, we'll choose that whichever one we managed to parse more of | 40 // heuristic, we'll choose that whichever one we managed to parse more of |
| 43 // before hitting an error is probably the right one. | 41 // before hitting an error is probably the right one. |
| 44 if (_firstOffset(exception) < _firstOffset(err)) { | 42 if (_firstOffset(exception) < _firstOffset(err)) { |
| 45 exception = err; | 43 exception = err; |
| 46 } | 44 } |
| 47 } | 45 } |
| 48 | 46 |
| 49 // If we get here, it couldn't be parsed at all. | 47 // If we get here, it couldn't be parsed at all. |
| 50 return new FormatResult(code: source, error: "$exception"); | 48 return new FormatResult(code: source, error: "$exception"); |
| 51 }); | 49 }); |
| 52 } | 50 } |
| 53 | 51 |
| 54 /// Returns the offset of the error nearest the beginning of the file out of | 52 /// Returns the offset of the error nearest the beginning of the file out of |
| 55 /// all the errors in [exception]. | 53 /// all the errors in [exception]. |
| 56 int _firstOffset(FormatterException exception) => | 54 int _firstOffset(FormatterException exception) => |
| 57 exception.errors.map((error) => error.offset).reduce(math.min); | 55 exception.errors.map((error) => error.offset).reduce(math.min); |
| OLD | NEW |