| 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 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 | 126 |
| 127 FormattedSource format(CodeKind kind, String source, {int offset, int end, | 127 FormattedSource format(CodeKind kind, String source, {int offset, int end, |
| 128 int indentationLevel: 0, Selection selection: null}) { | 128 int indentationLevel: 0, Selection selection: null}) { |
| 129 | 129 |
| 130 var startToken = tokenize(source); | 130 var startToken = tokenize(source); |
| 131 checkForErrors(); | 131 checkForErrors(); |
| 132 | 132 |
| 133 var node = parse(kind, startToken); | 133 var node = parse(kind, startToken); |
| 134 checkForErrors(); | 134 checkForErrors(); |
| 135 | 135 |
| 136 var formatter = new SourceVisitor(options, lineInfo, selection); | 136 var formatter = new SourceVisitor(options, lineInfo, source, selection); |
| 137 node.accept(formatter); | 137 node.accept(formatter); |
| 138 | 138 |
| 139 var formattedSource = formatter.writer.toString(); | 139 var formattedSource = formatter.writer.toString(); |
| 140 | 140 |
| 141 checkTokenStreams(startToken, tokenize(formattedSource)); | 141 checkTokenStreams(startToken, tokenize(formattedSource)); |
| 142 | 142 |
| 143 return new FormattedSource(formattedSource, formatter.selection); | 143 return new FormattedSource(formattedSource, formatter.selection); |
| 144 } | 144 } |
| 145 | 145 |
| 146 checkTokenStreams(Token t1, Token t2) => | 146 checkTokenStreams(Token t1, Token t2) => |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 final twoSlashes = new RegExp(r'//[^/]'); | 367 final twoSlashes = new RegExp(r'//[^/]'); |
| 368 | 368 |
| 369 /// A weight for potential breakpoints. | 369 /// A weight for potential breakpoints. |
| 370 int breakWeight = DEFAULT_SPACE_WEIGHT; | 370 int breakWeight = DEFAULT_SPACE_WEIGHT; |
| 371 | 371 |
| 372 /// Original pre-format selection information (may be null). | 372 /// Original pre-format selection information (may be null). |
| 373 final Selection preSelection; | 373 final Selection preSelection; |
| 374 | 374 |
| 375 final bool codeTransforms; | 375 final bool codeTransforms; |
| 376 | 376 |
| 377 |
| 378 /// The source being formatted (used in interpolation handling) |
| 379 final String source; |
| 380 |
| 377 /// Post format selection information. | 381 /// Post format selection information. |
| 378 Selection selection; | 382 Selection selection; |
| 379 | 383 |
| 380 | 384 |
| 381 /// Initialize a newly created visitor to write source code representing | 385 /// Initialize a newly created visitor to write source code representing |
| 382 /// the visited nodes to the given [writer]. | 386 /// the visited nodes to the given [writer]. |
| 383 SourceVisitor(FormatterOptions options, this.lineInfo, this.preSelection): | 387 SourceVisitor(FormatterOptions options, this.lineInfo, this.source, |
| 384 writer = new SourceWriter(indentCount: options.initialIndentationLevel, | 388 this.preSelection) |
| 389 : writer = new SourceWriter(indentCount: options.initialIndentationLevel, |
| 385 lineSeparator: options.lineSeparator, | 390 lineSeparator: options.lineSeparator, |
| 386 useTabs: options.tabsForIndent, | 391 useTabs: options.tabsForIndent, |
| 387 spacesPerIndent: options.spacesPerIndent), | 392 spacesPerIndent: options.spacesPerIndent), |
| 388 codeTransforms = options.codeTransforms; | 393 codeTransforms = options.codeTransforms; |
| 389 | 394 |
| 390 visitAdjacentStrings(AdjacentStrings node) { | 395 visitAdjacentStrings(AdjacentStrings node) { |
| 391 visitNodes(node.strings, separatedBy: space); | 396 visitNodes(node.strings, separatedBy: space); |
| 392 } | 397 } |
| 393 | 398 |
| 394 visitAnnotation(Annotation node) { | 399 visitAnnotation(Annotation node) { |
| 395 token(node.atSign); | 400 token(node.atSign); |
| 396 visit(node.name); | 401 visit(node.name); |
| 397 token(node.period); | 402 token(node.period); |
| 398 visit(node.constructorName); | 403 visit(node.constructorName); |
| (...skipping 763 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1162 | 1167 |
| 1163 visitSimpleIdentifier(SimpleIdentifier node) { | 1168 visitSimpleIdentifier(SimpleIdentifier node) { |
| 1164 token(node.token); | 1169 token(node.token); |
| 1165 } | 1170 } |
| 1166 | 1171 |
| 1167 visitSimpleStringLiteral(SimpleStringLiteral node) { | 1172 visitSimpleStringLiteral(SimpleStringLiteral node) { |
| 1168 token(node.literal); | 1173 token(node.literal); |
| 1169 } | 1174 } |
| 1170 | 1175 |
| 1171 visitStringInterpolation(StringInterpolation node) { | 1176 visitStringInterpolation(StringInterpolation node) { |
| 1172 visitNodes(node.elements); | 1177 // Ensure that interpolated strings don't get broken up by treating them as |
| 1178 // a single String token |
| 1179 // Process token (for comments etc. but don't print the lexeme) |
| 1180 token(node.beginToken, printToken: (tok) => null); |
| 1181 var start = node.beginToken.offset; |
| 1182 var end = node.endToken.end; |
| 1183 String string = source.substring(start, end); |
| 1184 append(string); |
| 1185 //visitNodes(node.elements); |
| 1173 } | 1186 } |
| 1174 | 1187 |
| 1175 visitSuperConstructorInvocation(SuperConstructorInvocation node) { | 1188 visitSuperConstructorInvocation(SuperConstructorInvocation node) { |
| 1176 token(node.keyword); | 1189 token(node.keyword); |
| 1177 token(node.period); | 1190 token(node.period); |
| 1178 visit(node.constructorName); | 1191 visit(node.constructorName); |
| 1179 visit(node.argumentList); | 1192 visit(node.argumentList); |
| 1180 } | 1193 } |
| 1181 | 1194 |
| 1182 visitSuperExpression(SuperExpression node) { | 1195 visitSuperExpression(SuperExpression node) { |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1431 token(rightBracket.previous); | 1444 token(rightBracket.previous); |
| 1432 } | 1445 } |
| 1433 } | 1446 } |
| 1434 | 1447 |
| 1435 /// Indicate that user introduced newlines should be emitted before the next | 1448 /// Indicate that user introduced newlines should be emitted before the next |
| 1436 /// token. | 1449 /// token. |
| 1437 preserveLeadingNewlines() { | 1450 preserveLeadingNewlines() { |
| 1438 preserveNewlines = true; | 1451 preserveNewlines = true; |
| 1439 } | 1452 } |
| 1440 | 1453 |
| 1441 token(Token token, {precededBy(), followedBy(), int minNewlines: 0}) { | 1454 token(Token token, {precededBy(), followedBy(), |
| 1455 printToken(tok), int minNewlines: 0}) { |
| 1442 if (token != null) { | 1456 if (token != null) { |
| 1443 if (needsNewline) { | 1457 if (needsNewline) { |
| 1444 minNewlines = max(1, minNewlines); | 1458 minNewlines = max(1, minNewlines); |
| 1445 } | 1459 } |
| 1446 var emitted = emitPrecedingCommentsAndNewlines(token, min: minNewlines); | 1460 var emitted = emitPrecedingCommentsAndNewlines(token, min: minNewlines); |
| 1447 if (emitted > 0) { | 1461 if (emitted > 0) { |
| 1448 needsNewline = false; | 1462 needsNewline = false; |
| 1449 } | 1463 } |
| 1450 if (precededBy != null) { | 1464 if (precededBy != null) { |
| 1451 precededBy(); | 1465 precededBy(); |
| 1452 } | 1466 } |
| 1453 checkForSelectionUpdate(token); | 1467 checkForSelectionUpdate(token); |
| 1454 append(token.lexeme); | 1468 if (printToken == null) { |
| 1469 append(token.lexeme); |
| 1470 } else { |
| 1471 printToken(token); |
| 1472 } |
| 1455 if (followedBy != null) { | 1473 if (followedBy != null) { |
| 1456 followedBy(); | 1474 followedBy(); |
| 1457 } | 1475 } |
| 1458 previousToken = token; | 1476 previousToken = token; |
| 1459 } | 1477 } |
| 1460 } | 1478 } |
| 1461 | 1479 |
| 1462 emitSpaces() { | 1480 emitSpaces() { |
| 1463 if (leadingSpaces > 0) { | 1481 if (leadingSpaces > 0) { |
| 1464 if (allowLineLeadingSpaces || !writer.currentLine.isWhitespace()) { | 1482 if (allowLineLeadingSpaces || !writer.currentLine.isWhitespace()) { |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1680 var lastLine = | 1698 var lastLine = |
| 1681 lineInfo.getLocation(lastOffset).lineNumber; | 1699 lineInfo.getLocation(lastOffset).lineNumber; |
| 1682 var currentLine = | 1700 var currentLine = |
| 1683 lineInfo.getLocation(currentOffset).lineNumber; | 1701 lineInfo.getLocation(currentOffset).lineNumber; |
| 1684 return currentLine - lastLine; | 1702 return currentLine - lastLine; |
| 1685 } | 1703 } |
| 1686 | 1704 |
| 1687 String toString() => writer.toString(); | 1705 String toString() => writer.toString(); |
| 1688 | 1706 |
| 1689 } | 1707 } |
| OLD | NEW |