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_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 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 316 var start = node.beginToken.previous; | 316 var start = node.beginToken.previous; |
| 317 if (start != null && start.type is TokenType_EOF) { | 317 if (start != null && start.type is TokenType_EOF) { |
| 318 previousToken = start; | 318 previousToken = start; |
| 319 } | 319 } |
| 320 | 320 |
| 321 var scriptTag = node.scriptTag; | 321 var scriptTag = node.scriptTag; |
| 322 var directives = node.directives; | 322 var directives = node.directives; |
| 323 visit(scriptTag); | 323 visit(scriptTag); |
| 324 | 324 |
| 325 preservePrecedingNewlines = true; | 325 preservePrecedingNewlines = true; |
| 326 visitNodes(directives, separatedBy: newlines); | 326 visitNodes(directives, separatedBy: newlines, followedBy: newlines); |
| 327 | 327 |
| 328 preservePrecedingNewlines = true; | 328 preservePrecedingNewlines = true; |
| 329 visitNodes(node.declarations, separatedBy: newlines); | 329 visitNodes(node.declarations, separatedBy: newlines); |
| 330 | 330 |
| 331 // Handle trailing whitespace | 331 // Handle trailing whitespace |
| 332 preservePrecedingNewlines = true; | 332 preservePrecedingNewlines = true; |
| 333 token(node.endToken /* EOF */); | 333 token(node.endToken /* EOF */); |
| 334 } | 334 } |
| 335 | 335 |
| 336 visitConditionalExpression(ConditionalExpression node) { | 336 visitConditionalExpression(ConditionalExpression node) { |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 528 | 528 |
| 529 visitFunctionDeclaration(FunctionDeclaration node) { | 529 visitFunctionDeclaration(FunctionDeclaration node) { |
| 530 visitNode(node.returnType, followedBy: space); | 530 visitNode(node.returnType, followedBy: space); |
| 531 token(node.propertyKeyword, followedBy: space); | 531 token(node.propertyKeyword, followedBy: space); |
| 532 visit(node.name); | 532 visit(node.name); |
| 533 visit(node.functionExpression); | 533 visit(node.functionExpression); |
| 534 } | 534 } |
| 535 | 535 |
| 536 visitFunctionDeclarationStatement(FunctionDeclarationStatement node) { | 536 visitFunctionDeclarationStatement(FunctionDeclarationStatement node) { |
| 537 visit(node.functionDeclaration); | 537 visit(node.functionDeclaration); |
| 538 // TODO(pquitslund): fix and handle in function body | |
| 539 append(';'); | |
| 540 } | 538 } |
| 541 | 539 |
| 542 visitFunctionExpression(FunctionExpression node) { | 540 visitFunctionExpression(FunctionExpression node) { |
| 543 visit(node.parameters); | 541 visit(node.parameters); |
| 544 space(); | 542 space(); |
| 545 visit(node.body); | 543 visit(node.body); |
| 546 } | 544 } |
| 547 | 545 |
| 548 visitFunctionExpressionInvocation(FunctionExpressionInvocation node) { | 546 visitFunctionExpressionInvocation(FunctionExpressionInvocation node) { |
| 549 visit(node.function); | 547 visit(node.function); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 683 visitMapLiteral(MapLiteral node) { | 681 visitMapLiteral(MapLiteral node) { |
| 684 modifier(node.modifier); | 682 modifier(node.modifier); |
| 685 visitNode(node.typeArguments, followedBy: space); | 683 visitNode(node.typeArguments, followedBy: space); |
| 686 token(node.leftBracket); | 684 token(node.leftBracket); |
| 687 visitNodes(node.entries, separatedBy: commaSeperator); | 685 visitNodes(node.entries, separatedBy: commaSeperator); |
| 688 token(node.rightBracket); | 686 token(node.rightBracket); |
| 689 } | 687 } |
| 690 | 688 |
| 691 visitMapLiteralEntry(MapLiteralEntry node) { | 689 visitMapLiteralEntry(MapLiteralEntry node) { |
| 692 visit(node.key); | 690 visit(node.key); |
| 693 space(); | |
| 694 token(node.separator); | 691 token(node.separator); |
| 695 space(); | 692 space(); |
| 696 visit(node.value); | 693 visit(node.value); |
| 697 } | 694 } |
| 698 | 695 |
| 699 visitMethodDeclaration(MethodDeclaration node) { | 696 visitMethodDeclaration(MethodDeclaration node) { |
| 700 modifier(node.externalKeyword); | 697 modifier(node.externalKeyword); |
| 701 modifier(node.modifierKeyword); | 698 modifier(node.modifierKeyword); |
| 702 visitNode(node.returnType, followedBy: space); | 699 visitNode(node.returnType, followedBy: space); |
| 703 modifier(node.propertyKeyword); | 700 modifier(node.propertyKeyword); |
| (...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1048 if (preservePrecedingNewlines || minNewlines > 0) { | 1045 if (preservePrecedingNewlines || minNewlines > 0) { |
| 1049 var emitted = emitPrecedingNewlines(token, min: minNewlines); | 1046 var emitted = emitPrecedingNewlines(token, min: minNewlines); |
| 1050 preservePrecedingNewlines = false; | 1047 preservePrecedingNewlines = false; |
| 1051 if (emitted > 0) { | 1048 if (emitted > 0) { |
| 1052 needsNewline = false; | 1049 needsNewline = false; |
| 1053 } | 1050 } |
| 1054 } | 1051 } |
| 1055 if (precededBy !=null) { | 1052 if (precededBy !=null) { |
| 1056 precededBy(); | 1053 precededBy(); |
| 1057 } | 1054 } |
| 1055 emitBlockComments(token); | |
| 1058 append(token.lexeme); | 1056 append(token.lexeme); |
| 1059 if (followedBy != null) { | 1057 if (followedBy != null) { |
| 1060 followedBy(); | 1058 followedBy(); |
| 1061 } | 1059 } |
| 1062 previousToken = token; | 1060 previousToken = token; |
| 1063 } | 1061 } |
| 1064 } | 1062 } |
| 1065 | 1063 |
| 1066 commaSeperator() { | 1064 commaSeperator() { |
| 1067 comma(); | 1065 comma(); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 1093 /// Indent. | 1091 /// Indent. |
| 1094 indent() { | 1092 indent() { |
| 1095 writer.indent(); | 1093 writer.indent(); |
| 1096 } | 1094 } |
| 1097 | 1095 |
| 1098 /// Unindent | 1096 /// Unindent |
| 1099 unindent() { | 1097 unindent() { |
| 1100 writer.unindent(); | 1098 writer.unindent(); |
| 1101 } | 1099 } |
| 1102 | 1100 |
| 1103 /// Emit any detected newlines or a minimum as specified by [minNewlines]. | 1101 emitBlockComments(Token token) { |
| 1102 var comment = token.precedingComments; | |
| 1103 while (comment != null) { | |
| 1104 if (isBlock(comment)) { | |
| 1105 append(comment.toString().trim()); | |
| 1106 if (linesBetween(comment.end, token.offset) >= 1) { | |
| 1107 writer.newline(); | |
| 1108 } else { | |
| 1109 space(); | |
| 1110 } | |
| 1111 } | |
| 1112 comment = comment.next; | |
| 1113 } | |
| 1114 } | |
| 1115 | |
| 1116 /// Emit any detected newlines or a minimum as specified by [min]. | |
| 1104 int emitPrecedingNewlines(Token token, {min: 0}) { | 1117 int emitPrecedingNewlines(Token token, {min: 0}) { |
| 1105 var comment = token.precedingComments; | 1118 var comment = token.precedingComments; |
| 1106 var currentToken = comment != null ? comment : token; | 1119 var currentToken = comment != null ? comment : token; |
| 1107 var lines = max(min, countNewlinesBetween(previousToken, currentToken)); | 1120 var lines = max(min, countNewlinesBetween(previousToken, currentToken)); |
| 1108 writer.newlines(lines); | 1121 writer.newlines(lines); |
| 1122 | |
| 1109 while (comment != null) { | 1123 while (comment != null) { |
| 1110 append(comment.toString().trim()); | 1124 if (!isBlock(comment)) { |
| 1111 writer.newline(); | 1125 append(comment.toString().trim()); |
| 1126 var nextToken = comment.next != null ? comment.next : token; | |
| 1127 var postCommentNewlines = | |
| 1128 max(1, countNewlinesBetween(comment, nextToken)); | |
| 1129 writer.newlines(postCommentNewlines); | |
| 1130 lines += postCommentNewlines; | |
| 1131 } | |
| 1112 comment = comment.next; | 1132 comment = comment.next; |
| 1113 } | 1133 } |
| 1114 | 1134 |
| 1115 previousToken = token; | 1135 previousToken = token; |
| 1116 return lines; | 1136 return lines; |
| 1117 } | 1137 } |
| 1118 | 1138 |
| 1119 /// Count the blanks between these two nodes. | 1139 /// Count the blanks between these two nodes. |
| 1120 int countBlankLinesBetween(ASTNode lastNode, ASTNode currentNode) => | 1140 int countBlankLinesBetween(ASTNode lastNode, ASTNode currentNode) => |
| 1121 countNewlinesBetween(lastNode.endToken, currentNode.beginToken); | 1141 countNewlinesBetween(lastNode.endToken, currentNode.beginToken); |
| 1122 | 1142 |
| 1123 /// Count newlines preceeding this [node]. | 1143 /// Count newlines preceeding this [node]. |
| 1124 int countPrecedingNewlines(ASTNode node) => | 1144 int countPrecedingNewlines(ASTNode node) => |
| 1125 countNewlinesBetween(node.beginToken.previous, node.beginToken); | 1145 countNewlinesBetween(node.beginToken.previous, node.beginToken); |
| 1126 | 1146 |
| 1127 /// Count newlines succeeding this [node]. | 1147 /// Count newlines succeeding this [node]. |
| 1128 int countSucceedingNewlines(ASTNode node) => node == null ? 0 : | 1148 int countSucceedingNewlines(ASTNode node) => node == null ? 0 : |
| 1129 countNewlinesBetween(node.endToken, node.endToken.next); | 1149 countNewlinesBetween(node.endToken, node.endToken.next); |
| 1130 | 1150 |
| 1131 /// Count the blanks between these two nodes. | 1151 /// Count the blanks between these two nodes. |
| 1132 int countNewlinesBetween(Token last, Token current) { | 1152 int countNewlinesBetween(Token last, Token current) { |
| 1133 if (last == null || current == null) { | 1153 if (last == null || current == null) { |
| 1134 return 0; | 1154 return 0; |
| 1135 } | 1155 } |
| 1156 | |
| 1157 return linesBetween(last.end - 1, current.offset); | |
| 1158 } | |
| 1159 | |
| 1160 /// Test if this [comment] is a block comment. | |
| 1161 bool isBlock(Token comment) => | |
| 1162 comment.type != TokenType.SINGLE_LINE_COMMENT && | |
| 1163 linesBetween(comment.offset, comment.end) <= 1; | |
|
Brian Wilkerson
2013/08/27 21:12:45
Can this ever legitimately be 1, or should this te
pquitslund
2013/08/27 21:55:38
I think not. Fixed!
| |
| 1164 | |
| 1165 /// Count the lines between two offsets. | |
| 1166 int linesBetween(int lastOffset, int currentOffset) { | |
| 1136 var lastLine = | 1167 var lastLine = |
| 1137 lineInfo.getLocation(last.offset).lineNumber; | 1168 lineInfo.getLocation(lastOffset).lineNumber; |
| 1138 var currentLine = | 1169 var currentLine = |
| 1139 lineInfo.getLocation(current.offset).lineNumber; | 1170 lineInfo.getLocation(currentOffset).lineNumber; |
| 1140 return currentLine - lastLine; | 1171 return currentLine - lastLine; |
| 1141 } | 1172 } |
| 1142 | 1173 |
| 1143 String toString() => writer.toString(); | 1174 String toString() => writer.toString(); |
| 1144 | 1175 |
| 1145 } | 1176 } |
| OLD | NEW |