Chromium Code Reviews| 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 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 386 visit(node.arguments); | 386 visit(node.arguments); |
| 387 } | 387 } |
| 388 | 388 |
| 389 visitArgumentDefinitionTest(ArgumentDefinitionTest node) { | 389 visitArgumentDefinitionTest(ArgumentDefinitionTest node) { |
| 390 token(node.question); | 390 token(node.question); |
| 391 visit(node.identifier); | 391 visit(node.identifier); |
| 392 } | 392 } |
| 393 | 393 |
| 394 visitArgumentList(ArgumentList node) { | 394 visitArgumentList(ArgumentList node) { |
| 395 token(node.leftParenthesis); | 395 token(node.leftParenthesis); |
| 396 visitNodes(node.arguments, separatedBy: commaSeperator); | 396 visitCommaSeparatedNodes(node.arguments); |
| 397 token(node.rightParenthesis); | 397 token(node.rightParenthesis); |
| 398 } | 398 } |
| 399 | 399 |
| 400 visitAsExpression(AsExpression node) { | 400 visitAsExpression(AsExpression node) { |
| 401 visit(node.expression); | 401 visit(node.expression); |
| 402 space(); | 402 space(); |
| 403 token(node.asOperator); | 403 token(node.asOperator); |
| 404 space(); | 404 space(); |
| 405 visit(node.type); | 405 visit(node.type); |
| 406 } | 406 } |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 588 visitPrefixedBody(space, body); | 588 visitPrefixedBody(space, body); |
| 589 } | 589 } |
| 590 | 590 |
| 591 visitConstructorInitializers(ConstructorDeclaration node) { | 591 visitConstructorInitializers(ConstructorDeclaration node) { |
| 592 newlines(); | 592 newlines(); |
| 593 indent(2); | 593 indent(2); |
| 594 token(node.separator /* : */); | 594 token(node.separator /* : */); |
| 595 space(); | 595 space(); |
| 596 for (var i = 0; i < node.initializers.length; i++) { | 596 for (var i = 0; i < node.initializers.length; i++) { |
| 597 if (i > 0) { | 597 if (i > 0) { |
| 598 comma(); | 598 // preceding comma |
| 599 token(node.initializers[i].beginToken.previous); | |
| 599 newlines(); | 600 newlines(); |
| 600 space(2); | 601 space(2); |
| 601 } | 602 } |
| 602 node.initializers[i].accept(this); | 603 node.initializers[i].accept(this); |
| 603 } | 604 } |
| 604 unindent(2); | 605 unindent(2); |
| 605 } | 606 } |
| 606 | 607 |
| 607 visitConstructorRedirects(ConstructorDeclaration node) { | 608 visitConstructorRedirects(ConstructorDeclaration node) { |
| 608 token(node.separator /* = */, precededBy: space, followedBy: space); | 609 token(node.separator /* = */, precededBy: space, followedBy: space); |
| 609 visitNodes(node.initializers, separatedBy: commaSeperator); | 610 visitCommaSeparatedNodes(node.initializers); |
| 610 visit(node.redirectedConstructor); | 611 visit(node.redirectedConstructor); |
| 611 } | 612 } |
| 612 | 613 |
| 613 visitConstructorFieldInitializer(ConstructorFieldInitializer node) { | 614 visitConstructorFieldInitializer(ConstructorFieldInitializer node) { |
| 614 token(node.keyword); | 615 token(node.keyword); |
| 615 token(node.period); | 616 token(node.period); |
| 616 visit(node.fieldName); | 617 visit(node.fieldName); |
| 617 space(); | 618 space(); |
| 618 token(node.equals); | 619 token(node.equals); |
| 619 space(); | 620 space(); |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 757 token(node.rightParenthesis); | 758 token(node.rightParenthesis); |
| 758 } | 759 } |
| 759 | 760 |
| 760 visitForStatement(ForStatement node) { | 761 visitForStatement(ForStatement node) { |
| 761 token(node.forKeyword); | 762 token(node.forKeyword); |
| 762 space(); | 763 space(); |
| 763 token(node.leftParenthesis); | 764 token(node.leftParenthesis); |
| 764 if (node.initialization != null) { | 765 if (node.initialization != null) { |
| 765 visit(node.initialization); | 766 visit(node.initialization); |
| 766 } else { | 767 } else { |
| 767 visit(node.variables); | 768 if (node.variables == null) { |
| 769 space(); | |
| 770 } else { | |
| 771 visit(node.variables); | |
| 772 } | |
| 768 } | 773 } |
| 769 token(node.leftSeparator); | 774 token(node.leftSeparator); |
| 770 space(); | 775 space(); |
| 771 visit(node.condition); | 776 visit(node.condition); |
| 772 token(node.rightSeparator); | 777 token(node.rightSeparator); |
| 773 visitNodes(node.updaters, precededBy: space, separatedBy: space); | 778 if (node.updaters != null) { |
| 779 space(); | |
| 780 visitCommaSeparatedNodes(node.updaters); | |
| 781 } | |
| 774 token(node.rightParenthesis); | 782 token(node.rightParenthesis); |
| 775 space(); | 783 if (node.body is! EmptyStatement) { |
| 784 space(); | |
| 785 } | |
| 776 visit(node.body); | 786 visit(node.body); |
| 777 } | 787 } |
| 778 | 788 |
| 779 visitFunctionDeclaration(FunctionDeclaration node) { | 789 visitFunctionDeclaration(FunctionDeclaration node) { |
| 780 visitNode(node.returnType, followedBy: space); | 790 visitNode(node.returnType, followedBy: space); |
| 781 token(node.propertyKeyword, followedBy: space); | 791 token(node.propertyKeyword, followedBy: space); |
| 782 visit(node.name); | 792 visit(node.name); |
| 783 visit(node.functionExpression); | 793 visit(node.functionExpression); |
| 784 } | 794 } |
| 785 | 795 |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 810 | 820 |
| 811 visitFunctionTypedFormalParameter(FunctionTypedFormalParameter node) { | 821 visitFunctionTypedFormalParameter(FunctionTypedFormalParameter node) { |
| 812 visitNode(node.returnType, followedBy: space); | 822 visitNode(node.returnType, followedBy: space); |
| 813 visit(node.identifier); | 823 visit(node.identifier); |
| 814 visit(node.parameters); | 824 visit(node.parameters); |
| 815 } | 825 } |
| 816 | 826 |
| 817 visitHideCombinator(HideCombinator node) { | 827 visitHideCombinator(HideCombinator node) { |
| 818 token(node.keyword); | 828 token(node.keyword); |
| 819 space(); | 829 space(); |
| 820 visitNodes(node.hiddenNames, separatedBy: commaSeperator); | 830 visitCommaSeparatedNodes(node.hiddenNames); |
| 821 } | 831 } |
| 822 | 832 |
| 823 visitIfStatement(IfStatement node) { | 833 visitIfStatement(IfStatement node) { |
| 824 var hasElse = node.elseStatement != null; | 834 var hasElse = node.elseStatement != null; |
| 825 token(node.ifKeyword); | 835 token(node.ifKeyword); |
| 826 space(); | 836 space(); |
| 827 token(node.leftParenthesis); | 837 token(node.leftParenthesis); |
| 828 visit(node.condition); | 838 visit(node.condition); |
| 829 token(node.rightParenthesis); | 839 token(node.rightParenthesis); |
| 830 space(); | 840 space(); |
| 831 if (hasElse) { | 841 if (hasElse) { |
| 832 printAsBlock(node.thenStatement); | 842 printAsBlock(node.thenStatement); |
| 833 space(); | 843 space(); |
| 834 token(node.elseKeyword); | 844 token(node.elseKeyword); |
| 835 space(); | 845 space(); |
| 836 printAsBlock(node.elseStatement); | 846 printAsBlock(node.elseStatement); |
| 837 } else { | 847 } else { |
| 838 visit(node.thenStatement); | 848 visit(node.thenStatement); |
| 839 } | 849 } |
| 840 } | 850 } |
| 841 | 851 |
| 842 visitImplementsClause(ImplementsClause node) { | 852 visitImplementsClause(ImplementsClause node) { |
| 843 token(node.keyword); | 853 token(node.keyword); |
| 844 space(); | 854 space(); |
| 845 visitNodes(node.interfaces, separatedBy: commaSeperator); | 855 visitCommaSeparatedNodes(node.interfaces); |
| 846 } | 856 } |
| 847 | 857 |
| 848 visitImportDirective(ImportDirective node) { | 858 visitImportDirective(ImportDirective node) { |
| 849 token(node.keyword); | 859 token(node.keyword); |
| 850 space(); | 860 space(); |
| 851 visit(node.uri); | 861 visit(node.uri); |
| 852 token(node.asToken, precededBy: space, followedBy: space); | 862 token(node.asToken, precededBy: space, followedBy: space); |
| 853 visit(node.prefix); | 863 visit(node.prefix); |
| 854 visitNodes(node.combinators, precededBy: space, separatedBy: space); | 864 visitNodes(node.combinators, precededBy: space, separatedBy: space); |
| 855 token(node.semicolon); | 865 token(node.semicolon); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 919 } | 929 } |
| 920 | 930 |
| 921 visitLibraryIdentifier(LibraryIdentifier node) { | 931 visitLibraryIdentifier(LibraryIdentifier node) { |
| 922 append(node.name); | 932 append(node.name); |
| 923 } | 933 } |
| 924 | 934 |
| 925 visitListLiteral(ListLiteral node) { | 935 visitListLiteral(ListLiteral node) { |
| 926 modifier(node.constKeyword); | 936 modifier(node.constKeyword); |
| 927 visit(node.typeArguments); | 937 visit(node.typeArguments); |
| 928 token(node.leftBracket); | 938 token(node.leftBracket); |
| 929 visitNodes(node.elements, separatedBy: commaSeperator); | 939 visitCommaSeparatedNodes(node.elements); |
| 930 optionalTrailingComma(node.rightBracket); | 940 optionalTrailingComma(node.rightBracket); |
| 931 token(node.rightBracket); | 941 token(node.rightBracket); |
| 932 } | 942 } |
| 933 | 943 |
| 934 visitMapLiteral(MapLiteral node) { | 944 visitMapLiteral(MapLiteral node) { |
| 935 modifier(node.constKeyword); | 945 modifier(node.constKeyword); |
| 936 visitNode(node.typeArguments, followedBy: space); | 946 visitNode(node.typeArguments, followedBy: space); |
| 937 token(node.leftBracket); | 947 token(node.leftBracket); |
| 938 visitNodes(node.entries, separatedBy: commaSeperator); | 948 visitCommaSeparatedNodes(node.entries); |
| 939 optionalTrailingComma(node.rightBracket); | 949 optionalTrailingComma(node.rightBracket); |
| 940 token(node.rightBracket); | 950 token(node.rightBracket); |
| 941 } | 951 } |
| 942 | 952 |
| 943 visitMapLiteralEntry(MapLiteralEntry node) { | 953 visitMapLiteralEntry(MapLiteralEntry node) { |
| 944 visit(node.key); | 954 visit(node.key); |
| 945 token(node.separator); | 955 token(node.separator); |
| 946 space(); | 956 space(); |
| 947 visit(node.value); | 957 visit(node.value); |
| 948 } | 958 } |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1061 } | 1071 } |
| 1062 } | 1072 } |
| 1063 | 1073 |
| 1064 visitScriptTag(ScriptTag node) { | 1074 visitScriptTag(ScriptTag node) { |
| 1065 token(node.scriptTag); | 1075 token(node.scriptTag); |
| 1066 } | 1076 } |
| 1067 | 1077 |
| 1068 visitShowCombinator(ShowCombinator node) { | 1078 visitShowCombinator(ShowCombinator node) { |
| 1069 token(node.keyword); | 1079 token(node.keyword); |
| 1070 space(); | 1080 space(); |
| 1071 visitNodes(node.shownNames, separatedBy: commaSeperator); | 1081 visitCommaSeparatedNodes(node.shownNames); |
| 1072 } | 1082 } |
| 1073 | 1083 |
| 1074 visitSimpleFormalParameter(SimpleFormalParameter node) { | 1084 visitSimpleFormalParameter(SimpleFormalParameter node) { |
| 1075 modifier(node.keyword); | 1085 modifier(node.keyword); |
| 1076 visitNode(node.type, followedBy: space); | 1086 visitNode(node.type, followedBy: space); |
| 1077 visit(node.identifier); | 1087 visit(node.identifier); |
| 1078 } | 1088 } |
| 1079 | 1089 |
| 1080 visitSimpleIdentifier(SimpleIdentifier node) { | 1090 visitSimpleIdentifier(SimpleIdentifier node) { |
| 1081 token(node.token); | 1091 token(node.token); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1158 token(node.tryKeyword); | 1168 token(node.tryKeyword); |
| 1159 space(); | 1169 space(); |
| 1160 visit(node.body); | 1170 visit(node.body); |
| 1161 visitNodes(node.catchClauses, precededBy: space, separatedBy: space); | 1171 visitNodes(node.catchClauses, precededBy: space, separatedBy: space); |
| 1162 token(node.finallyKeyword, precededBy: space, followedBy: space); | 1172 token(node.finallyKeyword, precededBy: space, followedBy: space); |
| 1163 visit(node.finallyBlock); | 1173 visit(node.finallyBlock); |
| 1164 } | 1174 } |
| 1165 | 1175 |
| 1166 visitTypeArgumentList(TypeArgumentList node) { | 1176 visitTypeArgumentList(TypeArgumentList node) { |
| 1167 token(node.leftBracket); | 1177 token(node.leftBracket); |
| 1168 visitNodes(node.arguments, separatedBy: commaSeperator); | 1178 visitCommaSeparatedNodes(node.arguments); |
| 1169 token(node.rightBracket); | 1179 token(node.rightBracket); |
| 1170 } | 1180 } |
| 1171 | 1181 |
| 1172 visitTypeName(TypeName node) { | 1182 visitTypeName(TypeName node) { |
| 1173 visit(node.name); | 1183 visit(node.name); |
| 1174 visit(node.typeArguments); | 1184 visit(node.typeArguments); |
| 1175 } | 1185 } |
| 1176 | 1186 |
| 1177 visitTypeParameter(TypeParameter node) { | 1187 visitTypeParameter(TypeParameter node) { |
| 1178 visit(node.name); | 1188 visit(node.name); |
| 1179 token(node.keyword /* extends */, precededBy: space, followedBy: space); | 1189 token(node.keyword /* extends */, precededBy: space, followedBy: space); |
| 1180 visit(node.bound); | 1190 visit(node.bound); |
| 1181 } | 1191 } |
| 1182 | 1192 |
| 1183 visitTypeParameterList(TypeParameterList node) { | 1193 visitTypeParameterList(TypeParameterList node) { |
| 1184 token(node.leftBracket); | 1194 token(node.leftBracket); |
| 1185 visitNodes(node.typeParameters, separatedBy: commaSeperator); | 1195 visitCommaSeparatedNodes(node.typeParameters); |
| 1186 token(node.rightBracket); | 1196 token(node.rightBracket); |
| 1187 } | 1197 } |
| 1188 | 1198 |
| 1189 visitVariableDeclaration(VariableDeclaration node) { | 1199 visitVariableDeclaration(VariableDeclaration node) { |
| 1190 visit(node.name); | 1200 visit(node.name); |
| 1191 if (node.initializer != null) { | 1201 if (node.initializer != null) { |
| 1192 space(); | 1202 space(); |
| 1193 token(node.equals); | 1203 token(node.equals); |
| 1194 space(); | 1204 space(); |
| 1195 visit(node.initializer); | 1205 visit(node.initializer); |
| 1196 } | 1206 } |
| 1197 } | 1207 } |
| 1198 | 1208 |
| 1199 visitVariableDeclarationList(VariableDeclarationList node) { | 1209 visitVariableDeclarationList(VariableDeclarationList node) { |
| 1200 modifier(node.keyword); | 1210 modifier(node.keyword); |
| 1201 visitNode(node.type, followedBy: space); | 1211 visitNode(node.type, followedBy: space); |
| 1202 visitNodes(node.variables, separatedBy: commaSeperator); | 1212 visitCommaSeparatedNodes(node.variables); |
| 1203 } | 1213 } |
| 1204 | 1214 |
| 1205 visitVariableDeclarationStatement(VariableDeclarationStatement node) { | 1215 visitVariableDeclarationStatement(VariableDeclarationStatement node) { |
| 1206 visit(node.variables); | 1216 visit(node.variables); |
| 1207 token(node.semicolon); | 1217 token(node.semicolon); |
| 1208 } | 1218 } |
| 1209 | 1219 |
| 1210 visitWhileStatement(WhileStatement node) { | 1220 visitWhileStatement(WhileStatement node) { |
| 1211 token(node.keyword); | 1221 token(node.keyword); |
| 1212 space(); | 1222 space(); |
| 1213 token(node.leftParenthesis); | 1223 token(node.leftParenthesis); |
| 1214 visit(node.condition); | 1224 visit(node.condition); |
| 1215 token(node.rightParenthesis); | 1225 token(node.rightParenthesis); |
| 1216 space(); | 1226 if (node.body is! EmptyStatement) { |
| 1227 space(); | |
| 1228 } | |
| 1217 visit(node.body); | 1229 visit(node.body); |
| 1218 } | 1230 } |
| 1219 | 1231 |
| 1220 visitWithClause(WithClause node) { | 1232 visitWithClause(WithClause node) { |
| 1221 token(node.withKeyword); | 1233 token(node.withKeyword); |
| 1222 space(); | 1234 space(); |
| 1223 visitNodes(node.mixinTypes, separatedBy: commaSeperator); | 1235 visitCommaSeparatedNodes(node.mixinTypes); |
| 1224 } | 1236 } |
| 1225 | 1237 |
| 1226 /// Safely visit the given [node]. | 1238 /// Safely visit the given [node]. |
| 1227 visit(ASTNode node) { | 1239 visit(ASTNode node) { |
| 1228 if (node != null) { | 1240 if (node != null) { |
| 1229 node.accept(this); | 1241 node.accept(this); |
| 1230 } | 1242 } |
| 1231 } | 1243 } |
| 1232 | 1244 |
| 1233 /// Visit the given function [body], printing the [prefix] before if given | 1245 /// Visit the given function [body], printing the [prefix] before if given |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 1255 } | 1267 } |
| 1256 nodes[i].accept(this); | 1268 nodes[i].accept(this); |
| 1257 } | 1269 } |
| 1258 if (followedBy != null) { | 1270 if (followedBy != null) { |
| 1259 followedBy(); | 1271 followedBy(); |
| 1260 } | 1272 } |
| 1261 } | 1273 } |
| 1262 } | 1274 } |
| 1263 } | 1275 } |
| 1264 | 1276 |
| 1277 /// Visit a comma-separated list of [nodes] if not null. | |
| 1278 visitCommaSeparatedNodes(NodeList<ASTNode> nodes) { | |
| 1279 if (nodes != null) { | |
| 1280 var size = nodes.length; | |
| 1281 if (size > 0) { | |
| 1282 var node; | |
| 1283 for (var i = 0; i < size; i++) { | |
| 1284 node = nodes[i]; | |
| 1285 if (i > 0) { | |
| 1286 var comma = node.beginToken.previous; | |
| 1287 token(comma); | |
| 1288 space(); | |
| 1289 } | |
| 1290 node.accept(this); | |
| 1291 } | |
| 1292 } | |
| 1293 } | |
| 1294 } | |
| 1295 | |
| 1296 | |
| 1265 /// Visit a [node], and if not null, optionally preceded or followed by the | 1297 /// Visit a [node], and if not null, optionally preceded or followed by the |
| 1266 /// specified functions. | 1298 /// specified functions. |
| 1267 visitNode(ASTNode node, {precededBy(): null, followedBy(): null}) { | 1299 visitNode(ASTNode node, {precededBy(): null, followedBy(): null}) { |
| 1268 if (node != null) { | 1300 if (node != null) { |
| 1269 if (precededBy != null) { | 1301 if (precededBy != null) { |
| 1270 precededBy(); | 1302 precededBy(); |
| 1271 } | 1303 } |
| 1272 node.accept(this); | 1304 node.accept(this); |
| 1273 if (followedBy != null) { | 1305 if (followedBy != null) { |
| 1274 followedBy(); | 1306 followedBy(); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 1285 | 1317 |
| 1286 /// Indicate that at least one newline should be emitted and possibly more | 1318 /// Indicate that at least one newline should be emitted and possibly more |
| 1287 /// if the source has them. | 1319 /// if the source has them. |
| 1288 newlines() { | 1320 newlines() { |
| 1289 needsNewline = true; | 1321 needsNewline = true; |
| 1290 } | 1322 } |
| 1291 | 1323 |
| 1292 /// Optionally emit a trailing comma. | 1324 /// Optionally emit a trailing comma. |
| 1293 optionalTrailingComma(Token rightBracket) { | 1325 optionalTrailingComma(Token rightBracket) { |
| 1294 if (rightBracket.previous.lexeme == ',') { | 1326 if (rightBracket.previous.lexeme == ',') { |
| 1295 comma(); | 1327 token(rightBracket.previous); |
| 1296 } | 1328 } |
| 1297 } | 1329 } |
| 1298 | 1330 |
| 1299 token(Token token, {precededBy(), followedBy(), int minNewlines: 0}) { | 1331 token(Token token, {precededBy(), followedBy(), int minNewlines: 0}) { |
| 1300 if (token != null) { | 1332 if (token != null) { |
| 1301 if (needsNewline) { | 1333 if (needsNewline) { |
| 1302 minNewlines = max(1, minNewlines); | 1334 minNewlines = max(1, minNewlines); |
| 1303 } | 1335 } |
| 1304 var emitted = emitPrecedingCommentsAndNewlines(token, min: minNewlines); | 1336 var emitted = emitPrecedingCommentsAndNewlines(token, min: minNewlines); |
| 1305 if (emitted > 0) { | 1337 if (emitted > 0) { |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 1331 var overshot = token.offset - preSelection.offset; | 1363 var overshot = token.offset - preSelection.offset; |
| 1332 if (overshot >= 0) { | 1364 if (overshot >= 0) { |
| 1333 //TODO(pquitslund): update length (may need truncating) | 1365 //TODO(pquitslund): update length (may need truncating) |
| 1334 selection = new Selection( | 1366 selection = new Selection( |
| 1335 writer.toString().length + leadingSpaces - overshot, | 1367 writer.toString().length + leadingSpaces - overshot, |
| 1336 preSelection.length); | 1368 preSelection.length); |
| 1337 } | 1369 } |
| 1338 } | 1370 } |
| 1339 } | 1371 } |
| 1340 | 1372 |
| 1341 commaSeperator() { | |
| 1342 comma(); | |
| 1343 space(); | |
| 1344 } | |
| 1345 | |
| 1346 comma() { | |
| 1347 writer.print(','); | |
| 1348 } | |
| 1349 | |
| 1350 | |
| 1351 /// Emit a non-breakable space. | 1373 /// Emit a non-breakable space. |
| 1352 space([n = 1]) { | 1374 space([n = 1]) { |
| 1353 //TODO(pquitslund): replace with a proper space token | 1375 //TODO(pquitslund): replace with a proper space token |
| 1354 leadingSpaces+=n; | 1376 leadingSpaces+=n; |
| 1355 } | 1377 } |
| 1356 | 1378 |
| 1357 /// Emit a breakable space | 1379 /// Emit a breakable space |
| 1358 breakableSpace() { | 1380 breakableSpace() { |
| 1359 //Implement | 1381 //Implement |
| 1360 } | 1382 } |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1406 //Handle EOLs before newlines | 1428 //Handle EOLs before newlines |
| 1407 if (isAtEOL(comment)) { | 1429 if (isAtEOL(comment)) { |
| 1408 emitComment(comment, previousToken); | 1430 emitComment(comment, previousToken); |
| 1409 comment = comment.next; | 1431 comment = comment.next; |
| 1410 currentToken = comment != null ? comment : token; | 1432 currentToken = comment != null ? comment : token; |
| 1411 } | 1433 } |
| 1412 | 1434 |
| 1413 var lines = max(min, countNewlinesBetween(previousToken, currentToken)); | 1435 var lines = max(min, countNewlinesBetween(previousToken, currentToken)); |
| 1414 writer.newlines(lines); | 1436 writer.newlines(lines); |
| 1415 | 1437 |
| 1416 previousToken = currentToken.previous; | 1438 previousToken = |
| 1439 currentToken.previous != null ? currentToken.previous : token.previous; | |
|
Brian Wilkerson
2013/12/04 19:52:50
Unless you're creating your own tokens, "currentTo
pquitslund
2013/12/04 21:09:10
I believe I added this when I was creating token s
| |
| 1417 | 1440 |
| 1418 while (comment != null) { | 1441 while (comment != null) { |
| 1419 | 1442 |
| 1420 emitComment(comment, previousToken); | 1443 emitComment(comment, previousToken); |
| 1421 | 1444 |
| 1422 var nextToken = comment.next != null ? comment.next : token; | 1445 var nextToken = comment.next != null ? comment.next : token; |
| 1423 var newlines = calculateNewlinesBetweenComments(comment, nextToken); | 1446 var newlines = calculateNewlinesBetweenComments(comment, nextToken); |
| 1424 if (newlines > 0) { | 1447 if (newlines > 0) { |
| 1425 writer.newlines(newlines); | 1448 writer.newlines(newlines); |
| 1426 lines += newlines; | 1449 lines += newlines; |
| 1427 } else if (!isEOF(token)) { | 1450 } else { |
| 1428 append(' '); | 1451 var spaces = countSpacesBetween(comment, nextToken); |
| 1452 if (spaces > 0) { | |
| 1453 space(); | |
| 1454 } | |
| 1429 } | 1455 } |
| 1430 | 1456 |
| 1431 previousToken = comment; | 1457 previousToken = comment; |
| 1432 comment = comment.next; | 1458 comment = comment.next; |
| 1433 } | 1459 } |
| 1434 | 1460 |
| 1435 previousToken = token; | 1461 previousToken = token; |
| 1436 return lines; | 1462 return lines; |
| 1437 } | 1463 } |
| 1438 | 1464 |
| 1439 | |
| 1440 ensureTrailingNewline() { | 1465 ensureTrailingNewline() { |
| 1441 if (writer.lastToken is! NewlineToken) { | 1466 if (writer.lastToken is! NewlineToken) { |
| 1442 writer.newline(); | 1467 writer.newline(); |
| 1443 } | 1468 } |
| 1444 } | 1469 } |
| 1445 | 1470 |
| 1446 | 1471 |
| 1447 /// Test if this [comment] is at the end of a line. | 1472 /// Test if this [comment] is at the end of a line. |
| 1448 bool isAtEOL(Token comment) => | 1473 bool isAtEOL(Token comment) => |
| 1449 comment != null && comment.toString().trim().startsWith(twoSlashes) && | 1474 comment != null && comment.toString().trim().startsWith(twoSlashes) && |
| 1450 sameLine(comment, previousToken); | 1475 sameLine(comment, previousToken); |
| 1451 | 1476 |
| 1452 /// Emit this [comment], inserting leading whitespace if appropriate. | 1477 /// Emit this [comment], inserting leading whitespace if appropriate. |
| 1453 emitComment(Token comment, Token previousToken) { | 1478 emitComment(Token comment, Token previousToken) { |
| 1454 if (!writer.currentLine.isWhitespace() && !isBlock(comment)) { | 1479 if (!writer.currentLine.isWhitespace() && previousToken != null) { |
| 1455 var ws = countSpacesBetween(previousToken, comment); | 1480 var ws = countSpacesBetween(previousToken, comment); |
| 1456 // Preserve one space but no more | 1481 // Preserve one space but no more |
| 1457 if (ws > 0) { | 1482 if (ws > 0 && leadingSpaces == 0) { |
| 1458 append(' '); | 1483 space(); |
| 1459 } | 1484 } |
| 1460 } | 1485 } |
| 1461 | 1486 |
| 1462 append(comment.toString().trim()); | 1487 append(comment.toString().trim()); |
| 1463 } | 1488 } |
| 1464 | 1489 |
| 1465 /// Count spaces between these tokens. Tokens on different lines return 0. | 1490 /// Count spaces between these tokens. Tokens on different lines return 0. |
| 1466 int countSpacesBetween(Token last, Token current) => isEOF(last) || | 1491 int countSpacesBetween(Token last, Token current) => isEOF(last) || |
| 1467 countNewlinesBetween(last, current) > 0 ? 0 : current.offset - last.end; | 1492 countNewlinesBetween(last, current) > 0 ? 0 : current.offset - last.end; |
| 1468 | 1493 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1526 var lastLine = | 1551 var lastLine = |
| 1527 lineInfo.getLocation(lastOffset).lineNumber; | 1552 lineInfo.getLocation(lastOffset).lineNumber; |
| 1528 var currentLine = | 1553 var currentLine = |
| 1529 lineInfo.getLocation(currentOffset).lineNumber; | 1554 lineInfo.getLocation(currentOffset).lineNumber; |
| 1530 return currentLine - lastLine; | 1555 return currentLine - lastLine; |
| 1531 } | 1556 } |
| 1532 | 1557 |
| 1533 String toString() => writer.toString(); | 1558 String toString() => writer.toString(); |
| 1534 | 1559 |
| 1535 } | 1560 } |
| OLD | NEW |