| 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 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 /// the next token. | 356 /// the next token. |
| 357 bool preserveNewlines = false; | 357 bool preserveNewlines = false; |
| 358 | 358 |
| 359 /// A counter for spaces that should be emitted preceding the next token. | 359 /// A counter for spaces that should be emitted preceding the next token. |
| 360 int leadingSpaces = 0; | 360 int leadingSpaces = 0; |
| 361 | 361 |
| 362 /// A flag to specify whether line-leading spaces should be preserved (and | 362 /// A flag to specify whether line-leading spaces should be preserved (and |
| 363 /// addded to the indent level). | 363 /// addded to the indent level). |
| 364 bool allowLineLeadingSpaces; | 364 bool allowLineLeadingSpaces; |
| 365 | 365 |
| 366 /// A flag to specify whether zero-length spaces should be emmitted. |
| 367 bool emitEmptySpaces = false; |
| 368 |
| 366 /// Used for matching EOL comments | 369 /// Used for matching EOL comments |
| 367 final twoSlashes = new RegExp(r'//[^/]'); | 370 final twoSlashes = new RegExp(r'//[^/]'); |
| 368 | 371 |
| 369 /// A weight for potential breakpoints. | 372 /// A weight for potential breakpoints. |
| 370 int currentBreakWeight = DEFAULT_SPACE_WEIGHT; | 373 int currentBreakWeight = DEFAULT_SPACE_WEIGHT; |
| 371 | 374 |
| 372 /// Original pre-format selection information (may be null). | 375 /// Original pre-format selection information (may be null). |
| 373 final Selection preSelection; | 376 final Selection preSelection; |
| 374 | 377 |
| 375 final bool codeTransforms; | 378 final bool codeTransforms; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 405 visit(node.arguments); | 408 visit(node.arguments); |
| 406 } | 409 } |
| 407 | 410 |
| 408 visitArgumentDefinitionTest(ArgumentDefinitionTest node) { | 411 visitArgumentDefinitionTest(ArgumentDefinitionTest node) { |
| 409 token(node.question); | 412 token(node.question); |
| 410 visit(node.identifier); | 413 visit(node.identifier); |
| 411 } | 414 } |
| 412 | 415 |
| 413 visitArgumentList(ArgumentList node) { | 416 visitArgumentList(ArgumentList node) { |
| 414 token(node.leftParenthesis); | 417 token(node.leftParenthesis); |
| 418 breakableNonSpace(); |
| 415 visitCommaSeparatedNodes(node.arguments); | 419 visitCommaSeparatedNodes(node.arguments); |
| 420 breakableNonSpace(); |
| 416 token(node.rightParenthesis); | 421 token(node.rightParenthesis); |
| 417 } | 422 } |
| 418 | 423 |
| 419 visitAsExpression(AsExpression node) { | 424 visitAsExpression(AsExpression node) { |
| 420 visit(node.expression); | 425 visit(node.expression); |
| 421 space(); | 426 space(); |
| 422 token(node.asOperator); | 427 token(node.asOperator); |
| 423 space(); | 428 space(); |
| 424 visit(node.type); | 429 visit(node.type); |
| 425 } | 430 } |
| (...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 923 } else { | 928 } else { |
| 924 visit(node.target); | 929 visit(node.target); |
| 925 } | 930 } |
| 926 token(node.leftBracket); | 931 token(node.leftBracket); |
| 927 visit(node.index); | 932 visit(node.index); |
| 928 token(node.rightBracket); | 933 token(node.rightBracket); |
| 929 } | 934 } |
| 930 | 935 |
| 931 visitInstanceCreationExpression(InstanceCreationExpression node) { | 936 visitInstanceCreationExpression(InstanceCreationExpression node) { |
| 932 token(node.keyword); | 937 token(node.keyword); |
| 933 space(); | 938 nonBreakingSpace(); |
| 934 visit(node.constructorName); | 939 visit(node.constructorName); |
| 935 visit(node.argumentList); | 940 visit(node.argumentList); |
| 936 } | 941 } |
| 937 | 942 |
| 938 visitIntegerLiteral(IntegerLiteral node) { | 943 visitIntegerLiteral(IntegerLiteral node) { |
| 939 token(node.literal); | 944 token(node.literal); |
| 940 } | 945 } |
| 941 | 946 |
| 942 visitInterpolationExpression(InterpolationExpression node) { | 947 visitInterpolationExpression(InterpolationExpression node) { |
| 943 if (node.rightBracket != null) { | 948 if (node.rightBracket != null) { |
| (...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1459 printToken(token); | 1464 printToken(token); |
| 1460 } | 1465 } |
| 1461 if (followedBy != null) { | 1466 if (followedBy != null) { |
| 1462 followedBy(); | 1467 followedBy(); |
| 1463 } | 1468 } |
| 1464 previousToken = token; | 1469 previousToken = token; |
| 1465 } | 1470 } |
| 1466 } | 1471 } |
| 1467 | 1472 |
| 1468 emitSpaces() { | 1473 emitSpaces() { |
| 1469 if (leadingSpaces > 0) { | 1474 if (leadingSpaces > 0 || emitEmptySpaces) { |
| 1470 if (allowLineLeadingSpaces || !writer.currentLine.isWhitespace()) { | 1475 if (allowLineLeadingSpaces || !writer.currentLine.isWhitespace()) { |
| 1471 writer.spaces(leadingSpaces, breakWeight: currentBreakWeight); | 1476 writer.spaces(leadingSpaces, breakWeight: currentBreakWeight); |
| 1472 } | 1477 } |
| 1473 leadingSpaces = 0; | 1478 leadingSpaces = 0; |
| 1474 allowLineLeadingSpaces = false; | 1479 allowLineLeadingSpaces = false; |
| 1480 emitEmptySpaces = false; |
| 1475 currentBreakWeight = DEFAULT_SPACE_WEIGHT; | 1481 currentBreakWeight = DEFAULT_SPACE_WEIGHT; |
| 1476 } | 1482 } |
| 1477 } | 1483 } |
| 1478 | 1484 |
| 1479 checkForSelectionUpdate(Token token) { | 1485 checkForSelectionUpdate(Token token) { |
| 1480 // Cache the first token on or AFTER the selection offset | 1486 // Cache the first token on or AFTER the selection offset |
| 1481 if (preSelection != null && selection == null) { | 1487 if (preSelection != null && selection == null) { |
| 1482 // Check for overshots | 1488 // Check for overshots |
| 1483 var overshot = token.offset - preSelection.offset; | 1489 var overshot = token.offset - preSelection.offset; |
| 1484 if (overshot >= 0) { | 1490 if (overshot >= 0) { |
| 1485 //TODO(pquitslund): update length (may need truncating) | 1491 //TODO(pquitslund): update length (may need truncating) |
| 1486 selection = new Selection( | 1492 selection = new Selection( |
| 1487 writer.toString().length + leadingSpaces - overshot, | 1493 writer.toString().length + leadingSpaces - overshot, |
| 1488 preSelection.length); | 1494 preSelection.length); |
| 1489 } | 1495 } |
| 1490 } | 1496 } |
| 1491 } | 1497 } |
| 1492 | 1498 |
| 1499 /// Emit a breakable 'non' (zero-length) space |
| 1500 breakableNonSpace() { |
| 1501 space(n: 0); |
| 1502 emitEmptySpaces = true; |
| 1503 } |
| 1504 |
| 1493 /// Emit a non-breakable space. | 1505 /// Emit a non-breakable space. |
| 1494 nonBreakingSpace() { | 1506 nonBreakingSpace() { |
| 1495 space(breakWeight: UNBREAKABLE_SPACE_WEIGHT); | 1507 space(breakWeight: UNBREAKABLE_SPACE_WEIGHT); |
| 1496 } | 1508 } |
| 1497 | 1509 |
| 1498 /// Emit a space. If [allowLineLeading] is specified, spaces | 1510 /// Emit a space. If [allowLineLeading] is specified, spaces |
| 1499 /// will be preserved at the start of a line (in addition to the | 1511 /// will be preserved at the start of a line (in addition to the |
| 1500 /// indent-level), otherwise line-leading spaces will be ignored. | 1512 /// indent-level), otherwise line-leading spaces will be ignored. |
| 1501 space({n: 1, allowLineLeading: false, breakWeight: DEFAULT_SPACE_WEIGHT}) { | 1513 space({n: 1, allowLineLeading: false, breakWeight: DEFAULT_SPACE_WEIGHT}) { |
| 1502 //TODO(pquitslund): replace with a proper space token | 1514 //TODO(pquitslund): replace with a proper space token |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1685 var lastLine = | 1697 var lastLine = |
| 1686 lineInfo.getLocation(lastOffset).lineNumber; | 1698 lineInfo.getLocation(lastOffset).lineNumber; |
| 1687 var currentLine = | 1699 var currentLine = |
| 1688 lineInfo.getLocation(currentOffset).lineNumber; | 1700 lineInfo.getLocation(currentOffset).lineNumber; |
| 1689 return currentLine - lastLine; | 1701 return currentLine - lastLine; |
| 1690 } | 1702 } |
| 1691 | 1703 |
| 1692 String toString() => writer.toString(); | 1704 String toString() => writer.toString(); |
| 1693 | 1705 |
| 1694 } | 1706 } |
| OLD | NEW |