| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library dart_style.src.dart_formatter; | 5 library dart_style.src.dart_formatter; |
| 6 | 6 |
| 7 import 'dart:math' as math; | 7 import 'dart:math' as math; |
| 8 | 8 |
| 9 import 'package:analyzer/analyzer.dart'; | 9 import 'package:analyzer/analyzer.dart'; |
| 10 import 'package:analyzer/dart/ast/token.dart'; |
| 11 import 'package:analyzer/src/dart/scanner/reader.dart'; |
| 12 import 'package:analyzer/src/dart/scanner/scanner.dart'; |
| 10 import 'package:analyzer/src/generated/parser.dart'; | 13 import 'package:analyzer/src/generated/parser.dart'; |
| 11 import 'package:analyzer/src/generated/scanner.dart'; | |
| 12 import 'package:analyzer/src/generated/source.dart'; | 14 import 'package:analyzer/src/generated/source.dart'; |
| 13 import 'package:analyzer/src/string_source.dart'; | 15 import 'package:analyzer/src/string_source.dart'; |
| 14 | 16 |
| 15 import 'error_listener.dart'; | 17 import 'error_listener.dart'; |
| 16 import 'formatter_exception.dart'; | 18 import 'formatter_exception.dart'; |
| 17 import 'source_code.dart'; | 19 import 'source_code.dart'; |
| 18 import 'source_visitor.dart'; | 20 import 'source_visitor.dart'; |
| 19 | 21 |
| 20 /// Dart source code formatter. | 22 /// Dart source code formatter. |
| 21 class DartFormatter { | 23 class DartFormatter { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 43 DartFormatter({this.lineEnding, int pageWidth, this.indent: 0}) | 45 DartFormatter({this.lineEnding, int pageWidth, this.indent: 0}) |
| 44 : this.pageWidth = (pageWidth == null) ? 80 : pageWidth; | 46 : this.pageWidth = (pageWidth == null) ? 80 : pageWidth; |
| 45 | 47 |
| 46 /// Formats the given [source] string containing an entire Dart compilation | 48 /// Formats the given [source] string containing an entire Dart compilation |
| 47 /// unit. | 49 /// unit. |
| 48 /// | 50 /// |
| 49 /// If [uri] is given, it is a [String] or [Uri] used to identify the file | 51 /// If [uri] is given, it is a [String] or [Uri] used to identify the file |
| 50 /// being formatted in error messages. | 52 /// being formatted in error messages. |
| 51 String format(String source, {uri}) { | 53 String format(String source, {uri}) { |
| 52 if (uri == null) { | 54 if (uri == null) { |
| 53 uri = "<unknown>"; | 55 // Do nothing. |
| 54 } else if (uri is Uri) { | 56 } else if (uri is Uri) { |
| 55 uri = uri.toString(); | 57 uri = uri.toString(); |
| 56 } else if (uri is String) { | 58 } else if (uri is String) { |
| 57 // Do nothing. | 59 // Do nothing. |
| 58 } else { | 60 } else { |
| 59 throw new ArgumentError("uri must be `null`, a Uri, or a String."); | 61 throw new ArgumentError("uri must be `null`, a Uri, or a String."); |
| 60 } | 62 } |
| 61 | 63 |
| 62 return formatSource( | 64 return formatSource( |
| 63 new SourceCode(source, uri: uri, isCompilationUnit: true)) | 65 new SourceCode(source, uri: uri, isCompilationUnit: true)) |
| (...skipping 29 matching lines...) Expand all Loading... |
| 93 lineEnding = "\r\n"; | 95 lineEnding = "\r\n"; |
| 94 } else { | 96 } else { |
| 95 lineEnding = "\n"; | 97 lineEnding = "\n"; |
| 96 } | 98 } |
| 97 } | 99 } |
| 98 | 100 |
| 99 errorListener.throwIfErrors(); | 101 errorListener.throwIfErrors(); |
| 100 | 102 |
| 101 // Parse it. | 103 // Parse it. |
| 102 var parser = new Parser(stringSource, errorListener); | 104 var parser = new Parser(stringSource, errorListener); |
| 105 parser.parseGenericMethods = true; |
| 103 | 106 |
| 104 var node; | 107 AstNode node; |
| 105 if (source.isCompilationUnit) { | 108 if (source.isCompilationUnit) { |
| 106 node = parser.parseCompilationUnit(startToken); | 109 node = parser.parseCompilationUnit(startToken); |
| 107 } else { | 110 } else { |
| 108 node = parser.parseStatement(startToken); | 111 node = parser.parseStatement(startToken); |
| 109 | 112 |
| 110 // Make sure we consumed all of the source. | 113 // Make sure we consumed all of the source. |
| 111 var token = node.endToken.next; | 114 var token = node.endToken.next; |
| 112 if (token.type != TokenType.EOF) { | 115 if (token.type != TokenType.EOF) { |
| 113 var error = new AnalysisError( | 116 var error = new AnalysisError( |
| 114 stringSource, | 117 stringSource, |
| 115 token.offset, | 118 token.offset, |
| 116 math.max(token.length, 1), | 119 math.max(token.length, 1), |
| 117 ParserErrorCode.UNEXPECTED_TOKEN, | 120 ParserErrorCode.UNEXPECTED_TOKEN, |
| 118 [token.lexeme]); | 121 [token.lexeme]); |
| 119 | 122 |
| 120 throw new FormatterException([error]); | 123 throw new FormatterException([error]); |
| 121 } | 124 } |
| 122 } | 125 } |
| 123 | 126 |
| 124 errorListener.throwIfErrors(); | 127 errorListener.throwIfErrors(); |
| 125 | 128 |
| 126 // Format it. | 129 // Format it. |
| 127 var visitor = new SourceVisitor(this, lineInfo, source); | 130 var visitor = new SourceVisitor(this, lineInfo, source); |
| 128 return visitor.run(node); | 131 return visitor.run(node); |
| 129 } | 132 } |
| 130 } | 133 } |
| OLD | NEW |