| 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 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 | 200 |
| 201 visitBinaryExpression(BinaryExpression node) { | 201 visitBinaryExpression(BinaryExpression node) { |
| 202 visit(node.leftOperand); | 202 visit(node.leftOperand); |
| 203 writer.print(' '); | 203 writer.print(' '); |
| 204 writer.print(node.operator.lexeme); | 204 writer.print(node.operator.lexeme); |
| 205 writer.print(' '); | 205 writer.print(' '); |
| 206 visit(node.rightOperand); | 206 visit(node.rightOperand); |
| 207 } | 207 } |
| 208 | 208 |
| 209 visitBlock(Block node) { | 209 visitBlock(Block node) { |
| 210 writer.print('{'); | 210 // writer.print('{'); |
| 211 emitToken(node.leftBracket); |
| 211 writer.indent(); | 212 writer.indent(); |
| 212 | 213 |
| 213 for (var stmt in node.statements) { | 214 for (var stmt in node.statements) { |
| 214 writer.newline(); | 215 //writer.newline(); |
| 215 visit(stmt); | 216 visit(stmt); |
| 216 } | 217 } |
| 217 | 218 |
| 218 writer.unindent(); | 219 writer.unindent(); |
| 219 writer.newline(); | 220 writer.newline(); |
| 220 writer.print('}'); | 221 writer.print('}'); |
| 221 previousToken = node.rightBracket; | 222 previousToken = node.rightBracket; |
| 222 } | 223 } |
| 223 | 224 |
| 224 visitBlockFunctionBody(BlockFunctionBody node) { | 225 visitBlockFunctionBody(BlockFunctionBody node) { |
| 225 visit(node.block); | 226 visit(node.block); |
| 226 } | 227 } |
| 227 | 228 |
| 228 visitBooleanLiteral(BooleanLiteral node) { | 229 visitBooleanLiteral(BooleanLiteral node) { |
| 229 writer.print(node.literal.lexeme); | 230 writer.print(node.literal.lexeme); |
| 230 } | 231 } |
| 231 | 232 |
| 232 visitBreakStatement(BreakStatement node) { | 233 visitBreakStatement(BreakStatement node) { |
| 233 writer.print('break'); | 234 emitToken(node.keyword); |
| 234 visitPrefixed(' ', node.label); | 235 visitPrefixed(' ', node.label); |
| 235 writer.print(';'); | 236 writer.print(';'); |
| 236 } | 237 } |
| 237 | 238 |
| 238 visitCascadeExpression(CascadeExpression node) { | 239 visitCascadeExpression(CascadeExpression node) { |
| 239 visit(node.target); | 240 visit(node.target); |
| 240 visitList(node.cascadeSections); | 241 visitList(node.cascadeSections); |
| 241 } | 242 } |
| 242 | 243 |
| 243 visitCatchClause(CatchClause node) { | 244 visitCatchClause(CatchClause node) { |
| 244 visitPrefixed('on ', node.exceptionType); | 245 visitPrefixed('on ', node.exceptionType); |
| 245 if (node.catchKeyword != null) { | 246 if (node.catchKeyword != null) { |
| 246 if (node.exceptionType != null) { | 247 if (node.exceptionType != null) { |
| 247 writer.print(' '); | 248 writer.print(' '); |
| 248 } | 249 } |
| 249 writer.print('catch ('); | 250 writer.print('catch ('); |
| 250 visit(node.exceptionParameter); | 251 visit(node.exceptionParameter); |
| 251 visitPrefixed(', ', node.stackTraceParameter); | 252 visitPrefixed(', ', node.stackTraceParameter); |
| 252 writer.print(') '); | 253 writer.print(') '); |
| 253 } else { | 254 } else { |
| 254 writer.print(' '); | 255 writer.print(' '); |
| 255 } | 256 } |
| 256 visit(node.body); | 257 visit(node.body); |
| 258 writer.newline(); |
| 257 } | 259 } |
| 258 | 260 |
| 259 visitClassDeclaration(ClassDeclaration node) { | 261 visitClassDeclaration(ClassDeclaration node) { |
| 260 emitToken(node.abstractKeyword, ' '); | 262 emitToken(node.abstractKeyword, ' '); |
| 261 emitToken(node.classKeyword, ' '); | 263 emitToken(node.classKeyword, ' '); |
| 262 visit(node.name); | 264 visit(node.name); |
| 263 visit(node.typeParameters); | 265 visit(node.typeParameters); |
| 264 visitPrefixed(' ', node.extendsClause); | 266 visitPrefixed(' ', node.extendsClause); |
| 265 visitPrefixed(' ', node.withClause); | 267 visitPrefixed(' ', node.withClause); |
| 266 visitPrefixed(' ', node.implementsClause); | 268 visitPrefixed(' ', node.implementsClause); |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 visit(node.identifier); | 514 visit(node.identifier); |
| 513 visit(node.parameters); | 515 visit(node.parameters); |
| 514 } | 516 } |
| 515 | 517 |
| 516 visitHideCombinator(HideCombinator node) { | 518 visitHideCombinator(HideCombinator node) { |
| 517 writer.print('hide '); | 519 writer.print('hide '); |
| 518 visitList(node.hiddenNames, ', '); | 520 visitList(node.hiddenNames, ', '); |
| 519 } | 521 } |
| 520 | 522 |
| 521 visitIfStatement(IfStatement node) { | 523 visitIfStatement(IfStatement node) { |
| 522 writer.print('if ('); | 524 emitToken(node.ifKeyword); |
| 525 writer.print(' ('); |
| 523 visit(node.condition); | 526 visit(node.condition); |
| 524 writer.print(') '); | 527 writer.print(') '); |
| 525 visit(node.thenStatement); | 528 visit(node.thenStatement); |
| 526 visitPrefixed(' else ', node.elseStatement); | 529 visitPrefixed(' else ', node.elseStatement); |
| 527 } | 530 } |
| 528 | 531 |
| 529 visitImplementsClause(ImplementsClause node) { | 532 visitImplementsClause(ImplementsClause node) { |
| 530 writer.print('implements '); | 533 writer.print('implements '); |
| 531 visitList(node.interfaces, ', '); | 534 visitList(node.interfaces, ', '); |
| 532 } | 535 } |
| 533 | 536 |
| 534 visitImportDirective(ImportDirective node) { | 537 visitImportDirective(ImportDirective node) { |
| 535 writer.print('import '); | 538 writer.print('import '); |
| 536 visit(node.uri); | 539 visit(node.uri); |
| 537 visitPrefixed(' as ', node.prefix); | 540 visitPrefixed(' as ', node.prefix); |
| 538 visitPrefixedList(' ', node.combinators, ' '); | 541 visitPrefixedList(' ', node.combinators, ' '); |
| 539 // writer.print(';'); | 542 // writer.print(';'); |
| 540 emit(node.semicolon); | 543 emitToken(node.semicolon); |
| 541 // writer.newline(); | 544 // writer.newline(); |
| 542 } | 545 } |
| 543 | 546 |
| 544 visitIndexExpression(IndexExpression node) { | 547 visitIndexExpression(IndexExpression node) { |
| 545 if (node.isCascaded) { | 548 if (node.isCascaded) { |
| 546 writer.print('..'); | 549 writer.print('..'); |
| 547 } else { | 550 } else { |
| 548 visit(node.target); | 551 visit(node.target); |
| 549 } | 552 } |
| 550 writer.print('['); | 553 writer.print('['); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 605 | 608 |
| 606 visitLibraryIdentifier(LibraryIdentifier node) { | 609 visitLibraryIdentifier(LibraryIdentifier node) { |
| 607 writer.print(node.name); | 610 writer.print(node.name); |
| 608 } | 611 } |
| 609 | 612 |
| 610 visitListLiteral(ListLiteral node) { | 613 visitListLiteral(ListLiteral node) { |
| 611 if (node.modifier != null) { | 614 if (node.modifier != null) { |
| 612 writer.print(node.modifier.lexeme); | 615 writer.print(node.modifier.lexeme); |
| 613 writer.print(' '); | 616 writer.print(' '); |
| 614 } | 617 } |
| 615 visitSuffixed(node.typeArguments, ' '); | 618 visit(node.typeArguments); |
| 616 writer.print('['); | 619 writer.print('['); |
| 617 visitList(node.elements, ', '); | 620 visitList(node.elements, ', '); |
| 618 writer.print(']'); | 621 writer.print(']'); |
| 619 } | 622 } |
| 620 | 623 |
| 621 visitMapLiteral(MapLiteral node) { | 624 visitMapLiteral(MapLiteral node) { |
| 622 if (node.modifier != null) { | 625 if (node.modifier != null) { |
| 623 writer.print(node.modifier.lexeme); | 626 writer.print(node.modifier.lexeme); |
| 624 writer.print(' '); | 627 writer.print(' '); |
| 625 } | 628 } |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 728 visit(node.argumentList); | 731 visit(node.argumentList); |
| 729 } | 732 } |
| 730 | 733 |
| 731 visitRethrowExpression(RethrowExpression node) { | 734 visitRethrowExpression(RethrowExpression node) { |
| 732 writer.print('rethrow'); | 735 writer.print('rethrow'); |
| 733 } | 736 } |
| 734 | 737 |
| 735 visitReturnStatement(ReturnStatement node) { | 738 visitReturnStatement(ReturnStatement node) { |
| 736 var expression = node.expression; | 739 var expression = node.expression; |
| 737 if (expression == null) { | 740 if (expression == null) { |
| 738 writer.print('return;'); | 741 emit(node.keyword, min: 1); |
| 742 writer.print(';'); |
| 739 } else { | 743 } else { |
| 740 writer.print('return '); | 744 emit(node.keyword, min: 1); |
| 745 writer.print(' '); |
| 741 expression.accept(this); | 746 expression.accept(this); |
| 742 writer.print(';'); | 747 writer.print(';'); |
| 743 } | 748 } |
| 744 } | 749 } |
| 745 | 750 |
| 746 visitScriptTag(ScriptTag node) { | 751 visitScriptTag(ScriptTag node) { |
| 747 writer.print(node.scriptTag.lexeme); | 752 writer.print(node.scriptTag.lexeme); |
| 748 } | 753 } |
| 749 | 754 |
| 750 visitShowCombinator(ShowCombinator node) { | 755 visitShowCombinator(ShowCombinator node) { |
| 751 writer.print('show '); | 756 writer.print('show '); |
| 752 visitList(node.shownNames, ', '); | 757 visitList(node.shownNames, ', '); |
| 753 } | 758 } |
| 754 | 759 |
| 755 visitSimpleFormalParameter(SimpleFormalParameter node) { | 760 visitSimpleFormalParameter(SimpleFormalParameter node) { |
| 756 emitToken(node.keyword, ' '); | 761 emitToken(node.keyword, ' '); |
| 757 visitSuffixed(node.type, ' '); | 762 visitSuffixed(node.type, ' '); |
| 758 visit(node.identifier); | 763 visit(node.identifier); |
| 759 } | 764 } |
| 760 | 765 |
| 761 visitSimpleIdentifier(SimpleIdentifier node) { | 766 visitSimpleIdentifier(SimpleIdentifier node) { |
| 762 emit(node.token); | 767 emitToken(node.token); |
| 763 // writer.print(node.token.lexeme); | |
| 764 } | 768 } |
| 765 | 769 |
| 766 visitSimpleStringLiteral(SimpleStringLiteral node) { | 770 visitSimpleStringLiteral(SimpleStringLiteral node) { |
| 767 writer.print(node.literal.lexeme); | 771 writer.print(node.literal.lexeme); |
| 768 } | 772 } |
| 769 | 773 |
| 770 visitStringInterpolation(StringInterpolation node) { | 774 visitStringInterpolation(StringInterpolation node) { |
| 771 visitList(node.elements); | 775 visitList(node.elements); |
| 772 } | 776 } |
| 773 | 777 |
| 774 visitSuperConstructorInvocation(SuperConstructorInvocation node) { | 778 visitSuperConstructorInvocation(SuperConstructorInvocation node) { |
| 775 writer.print('super'); | 779 writer.print('super'); |
| 776 visitPrefixed('.', node.constructorName); | 780 visitPrefixed('.', node.constructorName); |
| 777 visit(node.argumentList); | 781 visit(node.argumentList); |
| 778 } | 782 } |
| 779 | 783 |
| 780 visitSuperExpression(SuperExpression node) { | 784 visitSuperExpression(SuperExpression node) { |
| 781 writer.print('super'); | 785 writer.print('super'); |
| 782 } | 786 } |
| 783 | 787 |
| 784 visitSwitchCase(SwitchCase node) { | 788 visitSwitchCase(SwitchCase node) { |
| 785 visitSuffixedList(node.labels, ' ', ' '); | 789 visitSuffixedList(node.labels, ' ', ' '); |
| 786 writer.print('case '); | 790 emitToken(node.keyword); |
| 791 writer.print(' '); |
| 787 visit(node.expression); | 792 visit(node.expression); |
| 788 writer.print(': '); | 793 writer.print(':'); |
| 789 visitList(node.statements, ' '); | 794 writer.indent(); |
| 795 visitList(node.statements); |
| 796 writer.unindent(); |
| 790 } | 797 } |
| 791 | 798 |
| 792 visitSwitchDefault(SwitchDefault node) { | 799 visitSwitchDefault(SwitchDefault node) { |
| 793 visitSuffixedList(node.labels, ' ', ' '); | 800 visitSuffixedList(node.labels, ' ', ' '); |
| 794 writer.print('default: '); | 801 writer.print('default: '); |
| 795 visitList(node.statements, ' '); | 802 visitList(node.statements, ' '); |
| 796 } | 803 } |
| 797 | 804 |
| 798 visitSwitchStatement(SwitchStatement node) { | 805 visitSwitchStatement(SwitchStatement node) { |
| 799 writer.print('switch ('); | 806 emitToken(node.keyword); |
| 807 writer.print(' ('); |
| 800 visit(node.expression); | 808 visit(node.expression); |
| 801 writer.print(') {'); | 809 writer.print(') '); |
| 802 visitList(node.members, ' '); | 810 emitToken(node.leftBracket); |
| 803 writer.print('}'); | 811 writer.indent(); |
| 812 visitList(node.members); |
| 813 writer.unindent(); |
| 814 emitToken(node.rightBracket); |
| 815 writer.newline(); |
| 804 } | 816 } |
| 805 | 817 |
| 806 visitSymbolLiteral(SymbolLiteral node) { | 818 visitSymbolLiteral(SymbolLiteral node) { |
| 807 // No-op ? | 819 // No-op ? |
| 808 } | 820 } |
| 809 | 821 |
| 810 visitThisExpression(ThisExpression node) { | 822 visitThisExpression(ThisExpression node) { |
| 811 writer.print('this'); | 823 writer.print('this'); |
| 812 } | 824 } |
| 813 | 825 |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 909 writer.print(prefix); | 921 writer.print(prefix); |
| 910 } | 922 } |
| 911 visit(body); | 923 visit(body); |
| 912 } | 924 } |
| 913 | 925 |
| 914 /// Emit the given [token], printing the prefix before the [token] | 926 /// Emit the given [token], printing the prefix before the [token] |
| 915 /// if it is non-null. | 927 /// if it is non-null. |
| 916 emitPrefixedToken(String prefix, Token token) { | 928 emitPrefixedToken(String prefix, Token token) { |
| 917 if (token != null) { | 929 if (token != null) { |
| 918 writer.print(prefix); | 930 writer.print(prefix); |
| 919 emit(token); | 931 emitToken(token); |
| 920 } | 932 } |
| 921 } | 933 } |
| 922 | 934 |
| 923 /// Emit the given [token], printing the suffix after the [token] | 935 /// Emit the given [token], printing the suffix after the [token] |
| 924 /// node if it is non-null. | 936 /// node if it is non-null. |
| 925 emitToken(Token token, String suffix) { | 937 emitToken(Token token, [String suffix]) { |
| 926 if (token != null) { | 938 if (token != null) { |
| 927 emit(token); | 939 emit(token); |
| 928 writer.print(suffix); | 940 if (suffix != null) { |
| 941 writer.print(suffix); |
| 942 } |
| 929 } | 943 } |
| 930 } | 944 } |
| 931 | 945 |
| 932 /// Print a list of [nodes], separated by the given [separator]. | 946 /// Print a list of [nodes], separated by the given [separator]. |
| 933 visitList(NodeList<ASTNode> nodes, [String separator = '']) { | 947 visitList(NodeList<ASTNode> nodes, [String separator = '']) { |
| 934 if (nodes != null) { | 948 if (nodes != null) { |
| 935 var size = nodes.length; | 949 var size = nodes.length; |
| 936 for (var i = 0; i < size; i++) { | 950 for (var i = 0; i < size; i++) { |
| 937 if (i > 0) { | 951 if (i > 0) { |
| 938 writer.print(separator); | 952 writer.print(separator); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1009 if (last == null || current == null) { | 1023 if (last == null || current == null) { |
| 1010 return 0; | 1024 return 0; |
| 1011 } | 1025 } |
| 1012 var lastLine = | 1026 var lastLine = |
| 1013 lineInfo.getLocation(last.offset).lineNumber; | 1027 lineInfo.getLocation(last.offset).lineNumber; |
| 1014 var currentLine = | 1028 var currentLine = |
| 1015 lineInfo.getLocation(current.offset).lineNumber; | 1029 lineInfo.getLocation(current.offset).lineNumber; |
| 1016 return currentLine - lastLine; | 1030 return currentLine - lastLine; |
| 1017 } | 1031 } |
| 1018 | 1032 |
| 1033 String toString() => writer.toString(); |
| 1034 |
| 1019 } | 1035 } |
| OLD | NEW |