| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 formatter_impl; | 5 library formatter_impl; |
| 6 | 6 |
| 7 import 'dart:math'; | 7 import 'dart:math'; |
| 8 | 8 |
| 9 import 'package:analyzer_experimental/analyzer.dart'; | 9 import 'package:analyzer_experimental/analyzer.dart'; |
| 10 import 'package:analyzer_experimental/src/generated/parser.dart'; | 10 import 'package:analyzer_experimental/src/generated/parser.dart'; |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 return true; | 258 return true; |
| 259 } | 259 } |
| 260 } | 260 } |
| 261 // '>' '>' => '>>' | 261 // '>' '>' => '>>' |
| 262 if (isGT(token1) && isGT(token1.next)) { | 262 if (isGT(token1) && isGT(token1.next)) { |
| 263 if (isGT_GT(token2)) { | 263 if (isGT_GT(token2)) { |
| 264 token1 = token1.next; | 264 token1 = token1.next; |
| 265 return true; | 265 return true; |
| 266 } | 266 } |
| 267 } | 267 } |
| 268 // Cons(){} => Cons(); |
| 269 if (isOPEN_CURLY_BRACKET(token1) && isCLOSE_CURLY_BRACKET(token1.next)) { |
| 270 if (isSEMICOLON(token2)) { |
| 271 token1 = token1.next; |
| 272 advance(); |
| 273 return true; |
| 274 } |
| 275 } |
| 268 // Advance past synthetic { } tokens | 276 // Advance past synthetic { } tokens |
| 269 if (isOPEN_CURLY_BRACKET(token2) || isCLOSE_CURLY_BRACKET(token2)) { | 277 if (isOPEN_CURLY_BRACKET(token2) || isCLOSE_CURLY_BRACKET(token2)) { |
| 270 token2 = token2.next; | 278 token2 = token2.next; |
| 271 return checkTokens(); | 279 return checkTokens(); |
| 272 } | 280 } |
| 273 | 281 |
| 274 return false; | 282 return false; |
| 275 } | 283 } |
| 276 | 284 |
| 277 } | 285 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 304 tokenIs(token, TokenType.CLOSE_CURLY_BRACKET); | 312 tokenIs(token, TokenType.CLOSE_CURLY_BRACKET); |
| 305 | 313 |
| 306 /// Test if this token is a OPEN_SQUARE_BRACKET token. | 314 /// Test if this token is a OPEN_SQUARE_BRACKET token. |
| 307 bool isOPEN_SQ_BRACKET(Token token) => | 315 bool isOPEN_SQ_BRACKET(Token token) => |
| 308 tokenIs(token, TokenType.OPEN_SQUARE_BRACKET); | 316 tokenIs(token, TokenType.OPEN_SQUARE_BRACKET); |
| 309 | 317 |
| 310 /// Test if this token is a CLOSE_SQUARE_BRACKET token. | 318 /// Test if this token is a CLOSE_SQUARE_BRACKET token. |
| 311 bool isCLOSE_SQUARE_BRACKET(Token token) => | 319 bool isCLOSE_SQUARE_BRACKET(Token token) => |
| 312 tokenIs(token, TokenType.CLOSE_SQUARE_BRACKET); | 320 tokenIs(token, TokenType.CLOSE_SQUARE_BRACKET); |
| 313 | 321 |
| 322 /// Test if this token is a SEMICOLON token. |
| 323 bool isSEMICOLON(Token token) => |
| 324 tokenIs(token, TokenType.SEMICOLON); |
| 325 |
| 314 | 326 |
| 315 /// An AST visitor that drives formatting heuristics. | 327 /// An AST visitor that drives formatting heuristics. |
| 316 class SourceVisitor implements ASTVisitor { | 328 class SourceVisitor implements ASTVisitor { |
| 317 | 329 |
| 318 static final OPEN_CURLY = syntheticToken(TokenType.OPEN_CURLY_BRACKET, '{'); | 330 static final OPEN_CURLY = syntheticToken(TokenType.OPEN_CURLY_BRACKET, '{'); |
| 319 static final CLOSE_CURLY = syntheticToken(TokenType.CLOSE_CURLY_BRACKET, '}'); | 331 static final CLOSE_CURLY = syntheticToken(TokenType.CLOSE_CURLY_BRACKET, '}'); |
| 332 static final SEMI_COLON = syntheticToken(TokenType.SEMICOLON, ';'); |
| 320 | 333 |
| 321 static const SYNTH_OFFSET = -13; | 334 static const SYNTH_OFFSET = -13; |
| 322 | 335 |
| 323 static StringToken syntheticToken(TokenType type, String value) => | 336 static StringToken syntheticToken(TokenType type, String value) => |
| 324 new StringToken(type, value, SYNTH_OFFSET); | 337 new StringToken(type, value, SYNTH_OFFSET); |
| 325 | 338 |
| 326 static bool isSynthetic(Token token) => token.offset == SYNTH_OFFSET; | 339 static bool isSynthetic(Token token) => token.offset == SYNTH_OFFSET; |
| 327 | 340 |
| 328 /// The writer to which the source is to be written. | 341 /// The writer to which the source is to be written. |
| 329 final SourceWriter writer; | 342 final SourceWriter writer; |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 552 | 565 |
| 553 // Check for redirects or initializer lists | 566 // Check for redirects or initializer lists |
| 554 if (node.separator != null) { | 567 if (node.separator != null) { |
| 555 if (node.redirectedConstructor != null) { | 568 if (node.redirectedConstructor != null) { |
| 556 visitConstructorRedirects(node); | 569 visitConstructorRedirects(node); |
| 557 } else { | 570 } else { |
| 558 visitConstructorInitializers(node); | 571 visitConstructorInitializers(node); |
| 559 } | 572 } |
| 560 } | 573 } |
| 561 | 574 |
| 562 visitPrefixedBody(space, node.body); | 575 var body = node.body; |
| 576 if (codeTransforms && body is BlockFunctionBody) { |
| 577 if (body.block.statements.isEmpty) { |
| 578 token(SEMI_COLON); |
| 579 newlines(); |
| 580 return; |
| 581 } |
| 582 } |
| 583 |
| 584 visitPrefixedBody(space, body); |
| 563 } | 585 } |
| 564 | 586 |
| 565 visitConstructorInitializers(ConstructorDeclaration node) { | 587 visitConstructorInitializers(ConstructorDeclaration node) { |
| 566 newlines(); | 588 newlines(); |
| 567 indent(2); | 589 indent(2); |
| 568 token(node.separator /* : */); | 590 token(node.separator /* : */); |
| 569 space(); | 591 space(); |
| 570 for (var i = 0; i < node.initializers.length; i++) { | 592 for (var i = 0; i < node.initializers.length; i++) { |
| 571 if (i > 0) { | 593 if (i > 0) { |
| 572 comma(); | 594 comma(); |
| (...skipping 927 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1500 var lastLine = | 1522 var lastLine = |
| 1501 lineInfo.getLocation(lastOffset).lineNumber; | 1523 lineInfo.getLocation(lastOffset).lineNumber; |
| 1502 var currentLine = | 1524 var currentLine = |
| 1503 lineInfo.getLocation(currentOffset).lineNumber; | 1525 lineInfo.getLocation(currentOffset).lineNumber; |
| 1504 return currentLine - lastLine; | 1526 return currentLine - lastLine; |
| 1505 } | 1527 } |
| 1506 | 1528 |
| 1507 String toString() => writer.toString(); | 1529 String toString() => writer.toString(); |
| 1508 | 1530 |
| 1509 } | 1531 } |
| OLD | NEW |