| 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 781 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 792 | 792 |
| 793 visitLibraryIdentifier(LibraryIdentifier node) { | 793 visitLibraryIdentifier(LibraryIdentifier node) { |
| 794 append(node.name); | 794 append(node.name); |
| 795 } | 795 } |
| 796 | 796 |
| 797 visitListLiteral(ListLiteral node) { | 797 visitListLiteral(ListLiteral node) { |
| 798 modifier(node.constKeyword); | 798 modifier(node.constKeyword); |
| 799 visit(node.typeArguments); | 799 visit(node.typeArguments); |
| 800 token(node.leftBracket); | 800 token(node.leftBracket); |
| 801 visitNodes(node.elements, separatedBy: commaSeperator); | 801 visitNodes(node.elements, separatedBy: commaSeperator); |
| 802 optionalTrailingComma(node.rightBracket); |
| 802 token(node.rightBracket); | 803 token(node.rightBracket); |
| 803 } | 804 } |
| 804 | 805 |
| 805 visitMapLiteral(MapLiteral node) { | 806 visitMapLiteral(MapLiteral node) { |
| 806 modifier(node.constKeyword); | 807 modifier(node.constKeyword); |
| 807 visitNode(node.typeArguments, followedBy: space); | 808 visitNode(node.typeArguments, followedBy: space); |
| 808 token(node.leftBracket); | 809 token(node.leftBracket); |
| 809 visitNodes(node.entries, separatedBy: commaSeperator); | 810 visitNodes(node.entries, separatedBy: commaSeperator); |
| 811 optionalTrailingComma(node.rightBracket); |
| 810 token(node.rightBracket); | 812 token(node.rightBracket); |
| 811 } | 813 } |
| 812 | 814 |
| 813 visitMapLiteralEntry(MapLiteralEntry node) { | 815 visitMapLiteralEntry(MapLiteralEntry node) { |
| 814 visit(node.key); | 816 visit(node.key); |
| 815 token(node.separator); | 817 token(node.separator); |
| 816 space(); | 818 space(); |
| 817 visit(node.value); | 819 visit(node.value); |
| 818 } | 820 } |
| 819 | 821 |
| 820 visitMethodDeclaration(MethodDeclaration node) { | 822 visitMethodDeclaration(MethodDeclaration node) { |
| 821 modifier(node.externalKeyword); | 823 modifier(node.externalKeyword); |
| 822 modifier(node.modifierKeyword); | 824 modifier(node.modifierKeyword); |
| (...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1146 } | 1148 } |
| 1147 } | 1149 } |
| 1148 | 1150 |
| 1149 | 1151 |
| 1150 /// Emit the given [modifier] if it's non null, followed by non-breaking | 1152 /// Emit the given [modifier] if it's non null, followed by non-breaking |
| 1151 /// whitespace. | 1153 /// whitespace. |
| 1152 modifier(Token modifier) { | 1154 modifier(Token modifier) { |
| 1153 token(modifier, followedBy: space); | 1155 token(modifier, followedBy: space); |
| 1154 } | 1156 } |
| 1155 | 1157 |
| 1156 | |
| 1157 /// Indicate that at least one newline should be emitted and possibly more | 1158 /// Indicate that at least one newline should be emitted and possibly more |
| 1158 /// if the source has them. | 1159 /// if the source has them. |
| 1159 newlines() { | 1160 newlines() { |
| 1160 needsNewline = true; | 1161 needsNewline = true; |
| 1161 } | 1162 } |
| 1162 | 1163 |
| 1164 /// Optionally emit a trailing comma. |
| 1165 optionalTrailingComma(Token rightBracket) { |
| 1166 if (rightBracket.previous.lexeme == ',') { |
| 1167 comma(); |
| 1168 } |
| 1169 } |
| 1170 |
| 1163 token(Token token, {precededBy(), followedBy(), int minNewlines: 0}) { | 1171 token(Token token, {precededBy(), followedBy(), int minNewlines: 0}) { |
| 1164 if (token != null) { | 1172 if (token != null) { |
| 1165 if (needsNewline) { | 1173 if (needsNewline) { |
| 1166 minNewlines = max(1, minNewlines); | 1174 minNewlines = max(1, minNewlines); |
| 1167 } | 1175 } |
| 1168 var emitted = emitPrecedingCommentsAndNewlines(token, min: minNewlines); | 1176 var emitted = emitPrecedingCommentsAndNewlines(token, min: minNewlines); |
| 1169 if (emitted > 0) { | 1177 if (emitted > 0) { |
| 1170 needsNewline = false; | 1178 needsNewline = false; |
| 1171 } | 1179 } |
| 1172 if (precededBy !=null) { | 1180 if (precededBy != null) { |
| 1173 precededBy(); | 1181 precededBy(); |
| 1174 } | 1182 } |
| 1175 append(token.lexeme); | 1183 append(token.lexeme); |
| 1176 if (followedBy != null) { | 1184 if (followedBy != null) { |
| 1177 followedBy(); | 1185 followedBy(); |
| 1178 } | 1186 } |
| 1179 previousToken = token; | 1187 previousToken = token; |
| 1180 } | 1188 } |
| 1181 } | 1189 } |
| 1182 | 1190 |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1340 var lastLine = | 1348 var lastLine = |
| 1341 lineInfo.getLocation(lastOffset).lineNumber; | 1349 lineInfo.getLocation(lastOffset).lineNumber; |
| 1342 var currentLine = | 1350 var currentLine = |
| 1343 lineInfo.getLocation(currentOffset).lineNumber; | 1351 lineInfo.getLocation(currentOffset).lineNumber; |
| 1344 return currentLine - lastLine; | 1352 return currentLine - lastLine; |
| 1345 } | 1353 } |
| 1346 | 1354 |
| 1347 String toString() => writer.toString(); | 1355 String toString() => writer.toString(); |
| 1348 | 1356 |
| 1349 } | 1357 } |
| OLD | NEW |