| 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/analyzer.dart'; | 9 import 'package:analyzer/analyzer.dart'; |
| 10 import 'package:analyzer/src/generated/java_core.dart' show CharSequence; | 10 import 'package:analyzer/src/generated/java_core.dart' show CharSequence; |
| (...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 space(); | 447 space(); |
| 448 token(node.operator); | 448 token(node.operator); |
| 449 space(); | 449 space(); |
| 450 visit(node.rightOperand); | 450 visit(node.rightOperand); |
| 451 } | 451 } |
| 452 | 452 |
| 453 visitBlock(Block node) { | 453 visitBlock(Block node) { |
| 454 token(node.leftBracket); | 454 token(node.leftBracket); |
| 455 indent(); | 455 indent(); |
| 456 visitNodes(node.statements, precededBy: newlines, separatedBy: newlines); | 456 visitNodes(node.statements, precededBy: newlines, separatedBy: newlines); |
| 457 unindent(); | |
| 458 newlines(); | 457 newlines(); |
| 459 token(node.rightBracket); | 458 token(node.rightBracket, precededBy: unindent); |
| 460 } | 459 } |
| 461 | 460 |
| 462 visitBlockFunctionBody(BlockFunctionBody node) { | 461 visitBlockFunctionBody(BlockFunctionBody node) { |
| 463 visit(node.block); | 462 visit(node.block); |
| 464 } | 463 } |
| 465 | 464 |
| 466 visitBooleanLiteral(BooleanLiteral node) { | 465 visitBooleanLiteral(BooleanLiteral node) { |
| 467 token(node.literal); | 466 token(node.literal); |
| 468 } | 467 } |
| 469 | 468 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 518 visit(node.typeParameters); | 517 visit(node.typeParameters); |
| 519 visitNode(node.extendsClause, precededBy: space); | 518 visitNode(node.extendsClause, precededBy: space); |
| 520 visitNode(node.withClause, precededBy: space); | 519 visitNode(node.withClause, precededBy: space); |
| 521 visitNode(node.implementsClause, precededBy: space); | 520 visitNode(node.implementsClause, precededBy: space); |
| 522 visitNode(node.nativeClause, precededBy: space); | 521 visitNode(node.nativeClause, precededBy: space); |
| 523 space(); | 522 space(); |
| 524 }); | 523 }); |
| 525 token(node.leftBracket); | 524 token(node.leftBracket); |
| 526 indent(); | 525 indent(); |
| 527 visitNodes(node.members, precededBy: newlines, separatedBy: newlines); | 526 visitNodes(node.members, precededBy: newlines, separatedBy: newlines); |
| 528 unindent(); | |
| 529 newlines(); | 527 newlines(); |
| 530 token(node.rightBracket); | 528 token(node.rightBracket, precededBy: unindent); |
| 531 } | 529 } |
| 532 | 530 |
| 533 visitClassTypeAlias(ClassTypeAlias node) { | 531 visitClassTypeAlias(ClassTypeAlias node) { |
| 534 preserveLeadingNewlines(); | 532 preserveLeadingNewlines(); |
| 535 visitNodes(node.metadata, followedBy: newlines); | 533 visitNodes(node.metadata, followedBy: newlines); |
| 536 modifier(node.abstractKeyword); | 534 modifier(node.abstractKeyword); |
| 537 token(node.keyword); | 535 token(node.keyword); |
| 538 space(); | 536 space(); |
| 539 visit(node.name); | 537 visit(node.name); |
| 540 visit(node.typeParameters); | 538 visit(node.typeParameters); |
| (...skipping 446 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 987 append(node.name); | 985 append(node.name); |
| 988 } | 986 } |
| 989 | 987 |
| 990 visitListLiteral(ListLiteral node) { | 988 visitListLiteral(ListLiteral node) { |
| 991 modifier(node.constKeyword); | 989 modifier(node.constKeyword); |
| 992 visit(node.typeArguments); | 990 visit(node.typeArguments); |
| 993 token(node.leftBracket); | 991 token(node.leftBracket); |
| 994 indent(); | 992 indent(); |
| 995 visitCommaSeparatedNodes(node.elements /*, followedBy: breakableSpace*/); | 993 visitCommaSeparatedNodes(node.elements /*, followedBy: breakableSpace*/); |
| 996 optionalTrailingComma(node.rightBracket); | 994 optionalTrailingComma(node.rightBracket); |
| 997 unindent(); | 995 token(node.rightBracket, precededBy: unindent); |
| 998 token(node.rightBracket); | |
| 999 } | 996 } |
| 1000 | 997 |
| 1001 visitMapLiteral(MapLiteral node) { | 998 visitMapLiteral(MapLiteral node) { |
| 1002 modifier(node.constKeyword); | 999 modifier(node.constKeyword); |
| 1003 visitNode(node.typeArguments, followedBy: space); | 1000 visitNode(node.typeArguments, followedBy: space); |
| 1004 token(node.leftBracket); | 1001 token(node.leftBracket); |
| 1005 if (!node.entries.isEmpty) { | 1002 if (!node.entries.isEmpty) { |
| 1006 newlines(); | 1003 newlines(); |
| 1007 indent(); | 1004 indent(); |
| 1008 visitCommaSeparatedNodes(node.entries, followedBy: newlines); | 1005 visitCommaSeparatedNodes(node.entries, followedBy: newlines); |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1211 token(node.keyword); | 1208 token(node.keyword); |
| 1212 space(); | 1209 space(); |
| 1213 token(node.leftParenthesis); | 1210 token(node.leftParenthesis); |
| 1214 visit(node.expression); | 1211 visit(node.expression); |
| 1215 token(node.rightParenthesis); | 1212 token(node.rightParenthesis); |
| 1216 space(); | 1213 space(); |
| 1217 token(node.leftBracket); | 1214 token(node.leftBracket); |
| 1218 indent(); | 1215 indent(); |
| 1219 newlines(); | 1216 newlines(); |
| 1220 visitNodes(node.members, separatedBy: newlines, followedBy: newlines); | 1217 visitNodes(node.members, separatedBy: newlines, followedBy: newlines); |
| 1221 unindent(); | 1218 token(node.rightBracket, precededBy: unindent); |
| 1222 token(node.rightBracket); | 1219 |
| 1223 } | 1220 } |
| 1224 | 1221 |
| 1225 visitSymbolLiteral(SymbolLiteral node) { | 1222 visitSymbolLiteral(SymbolLiteral node) { |
| 1226 token(node.poundSign); | 1223 token(node.poundSign); |
| 1227 var components = node.components; | 1224 var components = node.components; |
| 1228 var size = components.length; | 1225 var size = components.length; |
| 1229 for (var component in components) { | 1226 for (var component in components) { |
| 1230 // The '.' separator | 1227 // The '.' separator |
| 1231 if (component.previous.lexeme == '.') { | 1228 if (component.previous.lexeme == '.') { |
| 1232 token(component.previous); | 1229 token(component.previous); |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1435 token(rightBracket.previous); | 1432 token(rightBracket.previous); |
| 1436 } | 1433 } |
| 1437 } | 1434 } |
| 1438 | 1435 |
| 1439 /// Indicate that user introduced newlines should be emitted before the next | 1436 /// Indicate that user introduced newlines should be emitted before the next |
| 1440 /// token. | 1437 /// token. |
| 1441 preserveLeadingNewlines() { | 1438 preserveLeadingNewlines() { |
| 1442 preserveNewlines = true; | 1439 preserveNewlines = true; |
| 1443 } | 1440 } |
| 1444 | 1441 |
| 1445 token(Token token, {precededBy(), followedBy(), | 1442 token(Token token, {precededBy(), followedBy(), printToken(tok), |
| 1446 printToken(tok), int minNewlines: 0}) { | 1443 int minNewlines: 0}) { |
| 1447 if (token != null) { | 1444 if (token != null) { |
| 1448 if (needsNewline) { | 1445 if (needsNewline) { |
| 1449 minNewlines = max(1, minNewlines); | 1446 minNewlines = max(1, minNewlines); |
| 1450 } | 1447 } |
| 1451 var emitted = emitPrecedingCommentsAndNewlines(token, min: minNewlines); | 1448 var emitted = emitPrecedingCommentsAndNewlines(token, min: minNewlines); |
| 1452 if (emitted > 0) { | 1449 if (emitted > 0) { |
| 1453 needsNewline = false; | 1450 needsNewline = false; |
| 1454 } | 1451 } |
| 1455 if (precededBy != null) { | 1452 if (precededBy != null) { |
| 1456 precededBy(); | 1453 precededBy(); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1505 //TODO(pquitslund): replace with a proper space token | 1502 //TODO(pquitslund): replace with a proper space token |
| 1506 leadingSpaces+=n; | 1503 leadingSpaces+=n; |
| 1507 allowLineLeadingSpaces = allowLineLeading; | 1504 allowLineLeadingSpaces = allowLineLeading; |
| 1508 currentBreakWeight = breakWeight; | 1505 currentBreakWeight = breakWeight; |
| 1509 } | 1506 } |
| 1510 | 1507 |
| 1511 /// Append the given [string] to the source writer if it's non-null. | 1508 /// Append the given [string] to the source writer if it's non-null. |
| 1512 append(String string) { | 1509 append(String string) { |
| 1513 if (string != null && !string.isEmpty) { | 1510 if (string != null && !string.isEmpty) { |
| 1514 emitSpaces(); | 1511 emitSpaces(); |
| 1515 writer.print(string); | 1512 writer.write(string); |
| 1516 } | 1513 } |
| 1517 } | 1514 } |
| 1518 | 1515 |
| 1519 /// Indent. | 1516 /// Indent. |
| 1520 indent([n = 1]) { | 1517 indent([n = 1]) { |
| 1521 while (n-- > 0) { | 1518 while (n-- > 0) { |
| 1522 writer.indent(); | 1519 writer.indent(); |
| 1523 } | 1520 } |
| 1524 } | 1521 } |
| 1525 | 1522 |
| 1526 /// Unindent | 1523 /// Unindent |
| 1527 unindent([n = 1]) { | 1524 unindent([n = 1]) { |
| 1528 while (n-- > 0) { | 1525 while (n-- > 0) { |
| 1529 writer.unindent(); | 1526 writer.unindent(); |
| 1530 } | 1527 } |
| 1531 } | 1528 } |
| 1532 | 1529 |
| 1533 /// Print this statement as if it were a block (e.g., surrounded by braces). | 1530 /// Print this statement as if it were a block (e.g., surrounded by braces). |
| 1534 printAsBlock(Statement statement) { | 1531 printAsBlock(Statement statement) { |
| 1535 if (codeTransforms && statement is! Block) { | 1532 if (codeTransforms && statement is! Block) { |
| 1536 token(OPEN_CURLY); | 1533 token(OPEN_CURLY); |
| 1537 indent(); | 1534 indent(); |
| 1538 newlines(); | 1535 newlines(); |
| 1539 visit(statement); | 1536 visit(statement); |
| 1540 newlines(); | 1537 newlines(); |
| 1541 unindent(); | 1538 token(CLOSE_CURLY, precededBy: unindent); |
| 1542 token(CLOSE_CURLY); | |
| 1543 } else { | 1539 } else { |
| 1544 visit(statement); | 1540 visit(statement); |
| 1545 } | 1541 } |
| 1546 } | 1542 } |
| 1547 | 1543 |
| 1548 /// Emit any detected comments and newlines or a minimum as specified | 1544 /// Emit any detected comments and newlines or a minimum as specified |
| 1549 /// by [min]. | 1545 /// by [min]. |
| 1550 int emitPrecedingCommentsAndNewlines(Token token, {min: 0}) { | 1546 int emitPrecedingCommentsAndNewlines(Token token, {min: 0}) { |
| 1551 | 1547 |
| 1552 var comment = token.precedingComments; | 1548 var comment = token.precedingComments; |
| 1553 var currentToken = comment != null ? comment : token; | 1549 var currentToken = comment != null ? comment : token; |
| 1554 | 1550 |
| 1555 //Handle EOLs before newlines | 1551 //Handle EOLs before newlines |
| 1556 if (isAtEOL(comment)) { | 1552 if (isAtEOL(comment)) { |
| 1557 emitComment(comment, previousToken); | 1553 emitComment(comment, previousToken); |
| 1558 comment = comment.next; | 1554 comment = comment.next; |
| 1559 currentToken = comment != null ? comment : token; | 1555 currentToken = comment != null ? comment : token; |
| 1560 // Ensure EOL comments force a linebreak | 1556 // Ensure EOL comments force a linebreak |
| 1561 needsNewline = true; | 1557 needsNewline = true; |
| 1562 } | 1558 } |
| 1563 | 1559 |
| 1564 var lines = 0; | 1560 var lines = 0; |
| 1565 if (needsNewline || preserveNewlines) { | 1561 if (needsNewline || preserveNewlines) { |
| 1566 lines = max(min, countNewlinesBetween(previousToken, currentToken)); | 1562 lines = max(min, countNewlinesBetween(previousToken, currentToken)); |
| 1567 preserveNewlines = false; | 1563 preserveNewlines = false; |
| 1568 } | 1564 } |
| 1565 |
| 1569 emitNewlines(lines); | 1566 emitNewlines(lines); |
| 1570 | 1567 |
| 1571 previousToken = | 1568 previousToken = |
| 1572 currentToken.previous != null ? currentToken.previous : token.previous; | 1569 currentToken.previous != null ? currentToken.previous : token.previous; |
| 1573 | 1570 |
| 1574 while (comment != null) { | 1571 while (comment != null) { |
| 1575 | 1572 |
| 1576 emitComment(comment, previousToken); | 1573 emitComment(comment, previousToken); |
| 1577 | 1574 |
| 1578 var nextToken = comment.next != null ? comment.next : token; | 1575 var nextToken = comment.next != null ? comment.next : token; |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1688 var lastLine = | 1685 var lastLine = |
| 1689 lineInfo.getLocation(lastOffset).lineNumber; | 1686 lineInfo.getLocation(lastOffset).lineNumber; |
| 1690 var currentLine = | 1687 var currentLine = |
| 1691 lineInfo.getLocation(currentOffset).lineNumber; | 1688 lineInfo.getLocation(currentOffset).lineNumber; |
| 1692 return currentLine - lastLine; | 1689 return currentLine - lastLine; |
| 1693 } | 1690 } |
| 1694 | 1691 |
| 1695 String toString() => writer.toString(); | 1692 String toString() => writer.toString(); |
| 1696 | 1693 |
| 1697 } | 1694 } |
| OLD | NEW |