| 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/parser.dart'; | 10 import 'package:analyzer/src/generated/parser.dart'; |
| (...skipping 1668 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1679 writer.newlines(lines); | 1679 writer.newlines(lines); |
| 1680 } | 1680 } |
| 1681 | 1681 |
| 1682 ensureTrailingNewline() { | 1682 ensureTrailingNewline() { |
| 1683 if (writer.lastToken is! NewlineToken) { | 1683 if (writer.lastToken is! NewlineToken) { |
| 1684 writer.newline(); | 1684 writer.newline(); |
| 1685 } | 1685 } |
| 1686 } | 1686 } |
| 1687 | 1687 |
| 1688 | 1688 |
| 1689 /// Test if this EOL [comment] is at the beginning of a line. |
| 1690 bool isAtBOL(Token comment) => |
| 1691 lineInfo.getLocation(comment.offset).columnNumber == 1; |
| 1692 |
| 1689 /// Test if this [comment] is at the end of a line. | 1693 /// Test if this [comment] is at the end of a line. |
| 1690 bool isAtEOL(Token comment) => | 1694 bool isAtEOL(Token comment) => |
| 1691 comment != null && comment.toString().trim().startsWith(twoSlashes) && | 1695 comment != null && comment.toString().trim().startsWith(twoSlashes) && |
| 1692 sameLine(comment, previousToken); | 1696 sameLine(comment, previousToken); |
| 1693 | 1697 |
| 1694 /// Emit this [comment], inserting leading whitespace if appropriate. | 1698 /// Emit this [comment], inserting leading whitespace if appropriate. |
| 1695 emitComment(Token comment, Token previousToken) { | 1699 emitComment(Token comment, Token previousToken) { |
| 1696 if (!writer.currentLine.isWhitespace() && previousToken != null) { | 1700 if (!writer.currentLine.isWhitespace() && previousToken != null) { |
| 1697 var ws = countSpacesBetween(previousToken, comment); | 1701 var ws = countSpacesBetween(previousToken, comment); |
| 1698 // Preserve one space but no more | 1702 // Preserve one space but no more |
| 1699 if (ws > 0 && leadingSpaces == 0) { | 1703 if (ws > 0 && leadingSpaces == 0) { |
| 1700 space(); | 1704 space(); |
| 1701 } | 1705 } |
| 1702 } | 1706 } |
| 1703 | 1707 |
| 1708 // Don't indent commented-out lines |
| 1709 if (isAtBOL(comment)) { |
| 1710 writer.currentLine.clear(); |
| 1711 } |
| 1712 |
| 1704 append(comment.toString().trim()); | 1713 append(comment.toString().trim()); |
| 1705 } | 1714 } |
| 1706 | 1715 |
| 1707 /// Count spaces between these tokens. Tokens on different lines return 0. | 1716 /// Count spaces between these tokens. Tokens on different lines return 0. |
| 1708 int countSpacesBetween(Token last, Token current) => isEOF(last) || | 1717 int countSpacesBetween(Token last, Token current) => isEOF(last) || |
| 1709 countNewlinesBetween(last, current) > 0 ? 0 : current.offset - last.end; | 1718 countNewlinesBetween(last, current) > 0 ? 0 : current.offset - last.end; |
| 1710 | 1719 |
| 1711 /// Count the blanks between these two nodes. | 1720 /// Count the blanks between these two nodes. |
| 1712 int countBlankLinesBetween(AstNode lastNode, AstNode currentNode) => | 1721 int countBlankLinesBetween(AstNode lastNode, AstNode currentNode) => |
| 1713 countNewlinesBetween(lastNode.endToken, currentNode.beginToken); | 1722 countNewlinesBetween(lastNode.endToken, currentNode.beginToken); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1768 var lastLine = | 1777 var lastLine = |
| 1769 lineInfo.getLocation(lastOffset).lineNumber; | 1778 lineInfo.getLocation(lastOffset).lineNumber; |
| 1770 var currentLine = | 1779 var currentLine = |
| 1771 lineInfo.getLocation(currentOffset).lineNumber; | 1780 lineInfo.getLocation(currentOffset).lineNumber; |
| 1772 return currentLine - lastLine; | 1781 return currentLine - lastLine; |
| 1773 } | 1782 } |
| 1774 | 1783 |
| 1775 String toString() => writer.toString(); | 1784 String toString() => writer.toString(); |
| 1776 | 1785 |
| 1777 } | 1786 } |
| OLD | NEW |