| 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/src/generated/parser.dart'; | 10 import 'package:analyzer/src/generated/parser.dart'; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 uri = "<unknown>"; | 53 uri = "<unknown>"; |
| 54 } else if (uri is Uri) { | 54 } else if (uri is Uri) { |
| 55 uri = uri.toString(); | 55 uri = uri.toString(); |
| 56 } else if (uri is String) { | 56 } else if (uri is String) { |
| 57 // Do nothing. | 57 // Do nothing. |
| 58 } else { | 58 } else { |
| 59 throw new ArgumentError("uri must be `null`, a Uri, or a String."); | 59 throw new ArgumentError("uri must be `null`, a Uri, or a String."); |
| 60 } | 60 } |
| 61 | 61 |
| 62 return formatSource( | 62 return formatSource( |
| 63 new SourceCode(source, uri: uri, isCompilationUnit: true)).text; | 63 new SourceCode(source, uri: uri, isCompilationUnit: true)) |
| 64 .text; |
| 64 } | 65 } |
| 65 | 66 |
| 66 /// Formats the given [source] string containing a single Dart statement. | 67 /// Formats the given [source] string containing a single Dart statement. |
| 67 String formatStatement(String source) { | 68 String formatStatement(String source) { |
| 68 return formatSource(new SourceCode(source, isCompilationUnit: false)).text; | 69 return formatSource(new SourceCode(source, isCompilationUnit: false)).text; |
| 69 } | 70 } |
| 70 | 71 |
| 71 /// Formats the given [source]. | 72 /// Formats the given [source]. |
| 72 /// | 73 /// |
| 73 /// Returns a new [SourceCode] containing the formatted code and the resulting | 74 /// Returns a new [SourceCode] containing the formatted code and the resulting |
| (...skipping 28 matching lines...) Expand all Loading... |
| 102 | 103 |
| 103 var node; | 104 var node; |
| 104 if (source.isCompilationUnit) { | 105 if (source.isCompilationUnit) { |
| 105 node = parser.parseCompilationUnit(startToken); | 106 node = parser.parseCompilationUnit(startToken); |
| 106 } else { | 107 } else { |
| 107 node = parser.parseStatement(startToken); | 108 node = parser.parseStatement(startToken); |
| 108 | 109 |
| 109 // Make sure we consumed all of the source. | 110 // Make sure we consumed all of the source. |
| 110 var token = node.endToken.next; | 111 var token = node.endToken.next; |
| 111 if (token.type != TokenType.EOF) { | 112 if (token.type != TokenType.EOF) { |
| 112 var error = new AnalysisError.con2( | 113 var error = new AnalysisError( |
| 113 stringSource, | 114 stringSource, |
| 114 token.offset, | 115 token.offset, |
| 115 math.max(token.length, 1), | 116 math.max(token.length, 1), |
| 116 ParserErrorCode.UNEXPECTED_TOKEN, | 117 ParserErrorCode.UNEXPECTED_TOKEN, |
| 117 [token.lexeme]); | 118 [token.lexeme]); |
| 118 | 119 |
| 119 throw new FormatterException([error]); | 120 throw new FormatterException([error]); |
| 120 } | 121 } |
| 121 } | 122 } |
| 122 | 123 |
| 123 errorListener.throwIfErrors(); | 124 errorListener.throwIfErrors(); |
| 124 | 125 |
| 125 // Format it. | 126 // Format it. |
| 126 var visitor = new SourceVisitor(this, lineInfo, source); | 127 var visitor = new SourceVisitor(this, lineInfo, source); |
| 127 return visitor.run(node); | 128 return visitor.run(node); |
| 128 } | 129 } |
| 129 } | 130 } |
| OLD | NEW |