| 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 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 /// Used for matching EOL comments | 366 /// Used for matching EOL comments |
| 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 currentBreakWeight = 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 | 377 |
| 378 /// The source being formatted (used in interpolation handling) | 378 /// The source being formatted (used in interpolation handling) |
| 379 final String source; | 379 final String source; |
| 380 | 380 |
| (...skipping 535 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 916 | 916 |
| 917 visitImplementsClause(ImplementsClause node) { | 917 visitImplementsClause(ImplementsClause node) { |
| 918 token(node.keyword); | 918 token(node.keyword); |
| 919 space(); | 919 space(); |
| 920 visitCommaSeparatedNodes(node.interfaces); | 920 visitCommaSeparatedNodes(node.interfaces); |
| 921 } | 921 } |
| 922 | 922 |
| 923 visitImportDirective(ImportDirective node) { | 923 visitImportDirective(ImportDirective node) { |
| 924 visitNodes(node.metadata, followedBy: newlines); | 924 visitNodes(node.metadata, followedBy: newlines); |
| 925 token(node.keyword); | 925 token(node.keyword); |
| 926 space(); | 926 nonBreakingSpace(); |
| 927 visit(node.uri); | 927 visit(node.uri); |
| 928 token(node.asToken, precededBy: space, followedBy: space); | 928 token(node.asToken, precededBy: space, followedBy: space); |
| 929 allowContinuedLines((){ | 929 allowContinuedLines((){ |
| 930 visit(node.prefix); | 930 visit(node.prefix); |
| 931 visitNodes(node.combinators, precededBy: space, separatedBy: space); | 931 visitNodes(node.combinators, precededBy: space, separatedBy: space); |
| 932 }); | 932 }); |
| 933 token(node.semicolon); | 933 token(node.semicolon); |
| 934 } | 934 } |
| 935 | 935 |
| 936 visitIndexExpression(IndexExpression node) { | 936 visitIndexExpression(IndexExpression node) { |
| (...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1474 if (followedBy != null) { | 1474 if (followedBy != null) { |
| 1475 followedBy(); | 1475 followedBy(); |
| 1476 } | 1476 } |
| 1477 previousToken = token; | 1477 previousToken = token; |
| 1478 } | 1478 } |
| 1479 } | 1479 } |
| 1480 | 1480 |
| 1481 emitSpaces() { | 1481 emitSpaces() { |
| 1482 if (leadingSpaces > 0) { | 1482 if (leadingSpaces > 0) { |
| 1483 if (allowLineLeadingSpaces || !writer.currentLine.isWhitespace()) { | 1483 if (allowLineLeadingSpaces || !writer.currentLine.isWhitespace()) { |
| 1484 writer.spaces(leadingSpaces, breakWeight: breakWeight); | 1484 writer.spaces(leadingSpaces, breakWeight: currentBreakWeight); |
| 1485 } | 1485 } |
| 1486 leadingSpaces = 0; | 1486 leadingSpaces = 0; |
| 1487 allowLineLeadingSpaces = false; | 1487 allowLineLeadingSpaces = false; |
| 1488 breakWeight = DEFAULT_SPACE_WEIGHT; | 1488 currentBreakWeight = DEFAULT_SPACE_WEIGHT; |
| 1489 } | 1489 } |
| 1490 } | 1490 } |
| 1491 | 1491 |
| 1492 checkForSelectionUpdate(Token token) { | 1492 checkForSelectionUpdate(Token token) { |
| 1493 // Cache the first token on or AFTER the selection offset | 1493 // Cache the first token on or AFTER the selection offset |
| 1494 if (preSelection != null && selection == null) { | 1494 if (preSelection != null && selection == null) { |
| 1495 // Check for overshots | 1495 // Check for overshots |
| 1496 var overshot = token.offset - preSelection.offset; | 1496 var overshot = token.offset - preSelection.offset; |
| 1497 if (overshot >= 0) { | 1497 if (overshot >= 0) { |
| 1498 //TODO(pquitslund): update length (may need truncating) | 1498 //TODO(pquitslund): update length (may need truncating) |
| 1499 selection = new Selection( | 1499 selection = new Selection( |
| 1500 writer.toString().length + leadingSpaces - overshot, | 1500 writer.toString().length + leadingSpaces - overshot, |
| 1501 preSelection.length); | 1501 preSelection.length); |
| 1502 } | 1502 } |
| 1503 } | 1503 } |
| 1504 } | 1504 } |
| 1505 | 1505 |
| 1506 /// Emit a non-breakable space. If [allowLineLeading] is specified, spaces | 1506 /// Emit a non-breakable space. |
| 1507 nonBreakingSpace() { |
| 1508 space(breakWeight: UNBREAKABLE_SPACE_WEIGHT); |
| 1509 } |
| 1510 |
| 1511 /// Emit a space. If [allowLineLeading] is specified, spaces |
| 1507 /// will be preserved at the start of a line (in addition to the | 1512 /// will be preserved at the start of a line (in addition to the |
| 1508 /// indent-level), otherwise line-leading spaces will be ignored. | 1513 /// indent-level), otherwise line-leading spaces will be ignored. |
| 1509 space({n: 1, allowLineLeading: false}) { | 1514 space({n: 1, allowLineLeading: false, breakWeight: DEFAULT_SPACE_WEIGHT}) { |
| 1510 //TODO(pquitslund): replace with a proper space token | 1515 //TODO(pquitslund): replace with a proper space token |
| 1511 leadingSpaces+=n; | 1516 leadingSpaces+=n; |
| 1512 allowLineLeadingSpaces = allowLineLeading; | 1517 allowLineLeadingSpaces = allowLineLeading; |
| 1513 } | 1518 currentBreakWeight = breakWeight; |
| 1514 | |
| 1515 /// Mark a breakable space | |
| 1516 breakableSpace() { | |
| 1517 space(); | |
| 1518 breakWeight = | |
| 1519 countNewlinesBetween(previousToken, previousToken.next) > 0 ? 1 : 0; | |
| 1520 } | 1519 } |
| 1521 | 1520 |
| 1522 /// Append the given [string] to the source writer if it's non-null. | 1521 /// Append the given [string] to the source writer if it's non-null. |
| 1523 append(String string) { | 1522 append(String string) { |
| 1524 if (string != null && !string.isEmpty) { | 1523 if (string != null && !string.isEmpty) { |
| 1525 emitSpaces(); | 1524 emitSpaces(); |
| 1526 writer.print(string); | 1525 writer.print(string); |
| 1527 } | 1526 } |
| 1528 } | 1527 } |
| 1529 | 1528 |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1699 var lastLine = | 1698 var lastLine = |
| 1700 lineInfo.getLocation(lastOffset).lineNumber; | 1699 lineInfo.getLocation(lastOffset).lineNumber; |
| 1701 var currentLine = | 1700 var currentLine = |
| 1702 lineInfo.getLocation(currentOffset).lineNumber; | 1701 lineInfo.getLocation(currentOffset).lineNumber; |
| 1703 return currentLine - lastLine; | 1702 return currentLine - lastLine; |
| 1704 } | 1703 } |
| 1705 | 1704 |
| 1706 String toString() => writer.toString(); | 1705 String toString() => writer.toString(); |
| 1707 | 1706 |
| 1708 } | 1707 } |
| OLD | NEW |