Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 dart_style.src.source_visitor; | 5 library dart_style.src.source_visitor; |
| 6 | 6 |
| 7 import 'package:analyzer/analyzer.dart'; | 7 import 'package:analyzer/analyzer.dart'; |
| 8 import 'package:analyzer/dart/ast/token.dart'; | 8 import 'package:analyzer/dart/ast/token.dart'; |
| 9 import 'package:analyzer/src/generated/source.dart'; | 9 import 'package:analyzer/src/generated/source.dart'; |
| 10 | 10 |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 138 if (node.arguments.isEmpty) { | 138 if (node.arguments.isEmpty) { |
| 139 token(node.leftParenthesis); | 139 token(node.leftParenthesis); |
| 140 | 140 |
| 141 // If there is a comment inside the parens, do allow splitting before it. | 141 // If there is a comment inside the parens, do allow splitting before it. |
| 142 if (node.rightParenthesis.precedingComments != null) soloZeroSplit(); | 142 if (node.rightParenthesis.precedingComments != null) soloZeroSplit(); |
| 143 | 143 |
| 144 token(node.rightParenthesis); | 144 token(node.rightParenthesis); |
| 145 return; | 145 return; |
| 146 } | 146 } |
| 147 | 147 |
| 148 // If the argument list has a trailing comma, format it like a collection | |
| 149 // literal where each argument goes on its own line, they are indented +2, | |
| 150 // and the ")" ends up on its own line. | |
| 151 if (node.arguments.last.endToken.next.type == TokenType.COMMA) { | |
| 152 _visitCollectionLiteral(null, node.leftParenthesis, node.arguments, node.r ightParenthesis); | |
|
nweiz
2016/07/25 22:29:06
Long line.
Bob Nystrom
2016/07/26 21:22:07
Ironically, I forgot to format. Done.
| |
| 153 return; | |
| 154 } | |
| 155 | |
| 148 new ArgumentListVisitor(this, node).visit(); | 156 new ArgumentListVisitor(this, node).visit(); |
| 149 } | 157 } |
| 150 | 158 |
| 151 visitAsExpression(AsExpression node) { | 159 visitAsExpression(AsExpression node) { |
| 152 builder.startSpan(); | 160 builder.startSpan(); |
| 153 visit(node.expression); | 161 visit(node.expression); |
| 154 soloSplit(); | 162 soloSplit(); |
| 155 token(node.asOperator); | 163 token(node.asOperator); |
| 156 space(); | 164 space(); |
| 157 visit(node.type); | 165 visit(node.type); |
| (...skipping 1724 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1882 | 1890 |
| 1883 visit(node); | 1891 visit(node); |
| 1884 | 1892 |
| 1885 // The comma after the node. | 1893 // The comma after the node. |
| 1886 if (node.endToken.next.lexeme == ",") token(node.endToken.next); | 1894 if (node.endToken.next.lexeme == ",") token(node.endToken.next); |
| 1887 } | 1895 } |
| 1888 } | 1896 } |
| 1889 | 1897 |
| 1890 /// Visits the collection literal [node] whose body starts with [leftBracket], | 1898 /// Visits the collection literal [node] whose body starts with [leftBracket], |
| 1891 /// ends with [rightBracket] and contains [elements]. | 1899 /// ends with [rightBracket] and contains [elements]. |
| 1900 /// | |
| 1901 /// This is also used for argument lists with a trailing comma which are | |
| 1902 /// considered "collection-like". In that case, [node] is `null`. | |
| 1892 void _visitCollectionLiteral(TypedLiteral node, Token leftBracket, | 1903 void _visitCollectionLiteral(TypedLiteral node, Token leftBracket, |
| 1893 Iterable<AstNode> elements, Token rightBracket, | 1904 Iterable<AstNode> elements, Token rightBracket, |
| 1894 [int cost]) { | 1905 [int cost]) { |
| 1895 modifier(node.constKeyword); | 1906 if (node != null) { |
| 1896 visit(node.typeArguments); | 1907 modifier(node.constKeyword); |
| 1908 visit(node.typeArguments); | |
| 1909 } | |
| 1897 | 1910 |
| 1898 // Don't allow splitting in an empty collection. | 1911 // Don't allow splitting in an empty collection. |
| 1899 if (elements.isEmpty && rightBracket.precedingComments == null) { | 1912 if (elements.isEmpty && rightBracket.precedingComments == null) { |
| 1900 token(leftBracket); | 1913 token(leftBracket); |
| 1901 token(rightBracket); | 1914 token(rightBracket); |
| 1902 return; | 1915 return; |
| 1903 } | 1916 } |
| 1904 | 1917 |
| 1905 // Force all of the surrounding collections to split. | 1918 // Force all of the surrounding collections to split. |
| 1906 for (var i = 0; i < _collectionSplits.length; i++) { | 1919 for (var i = 0; i < _collectionSplits.length; i++) { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1951 } | 1964 } |
| 1952 } else { | 1965 } else { |
| 1953 builder.split(nest: false, space: true); | 1966 builder.split(nest: false, space: true); |
| 1954 } | 1967 } |
| 1955 } | 1968 } |
| 1956 | 1969 |
| 1957 builder.nestExpression(); | 1970 builder.nestExpression(); |
| 1958 visit(element); | 1971 visit(element); |
| 1959 | 1972 |
| 1960 // The comma after the element. | 1973 // The comma after the element. |
| 1961 if (element.endToken.next.lexeme == ",") token(element.endToken.next); | 1974 if (element.endToken.next.type == TokenType.COMMA) { |
| 1975 token(element.endToken.next); | |
| 1976 } | |
| 1962 | 1977 |
| 1963 builder.unnest(); | 1978 builder.unnest(); |
| 1964 } | 1979 } |
| 1965 | 1980 |
| 1966 builder.endRule(); | 1981 builder.endRule(); |
| 1967 | 1982 |
| 1968 // If there is a collection inside this one, it forces this one to split. | 1983 // If there is a collection inside this one, it forces this one to split. |
| 1969 var force = _collectionSplits.removeLast(); | 1984 var force = _collectionSplits.removeLast(); |
| 1970 | 1985 |
| 1986 // If the collection has a trailing comma, the user must want it to split. | |
| 1987 if (elements.isNotEmpty && | |
| 1988 elements.last.endToken.next.type == TokenType.COMMA) { | |
| 1989 force = true; | |
| 1990 } | |
| 1991 | |
| 1971 _endLiteralBody(rightBracket, ignoredRule: rule, forceSplit: force); | 1992 _endLiteralBody(rightBracket, ignoredRule: rule, forceSplit: force); |
| 1972 } | 1993 } |
| 1973 | 1994 |
| 1974 /// Gets the cost to split at an assignment (or `:` in the case of a named | 1995 /// Gets the cost to split at an assignment (or `:` in the case of a named |
| 1975 /// default value) with the given [rightHandSide]. | 1996 /// default value) with the given [rightHandSide]. |
| 1976 /// | 1997 /// |
| 1977 /// "Block-like" expressions (collections and cascades) bind a bit tighter | 1998 /// "Block-like" expressions (collections and cascades) bind a bit tighter |
| 1978 /// because it looks better to have code like: | 1999 /// because it looks better to have code like: |
| 1979 /// | 2000 /// |
| 1980 /// var list = [ | 2001 /// var list = [ |
| (...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2446 /// Gets the 1-based line number that the beginning of [token] lies on. | 2467 /// Gets the 1-based line number that the beginning of [token] lies on. |
| 2447 int _startLine(Token token) => _lineInfo.getLocation(token.offset).lineNumber; | 2468 int _startLine(Token token) => _lineInfo.getLocation(token.offset).lineNumber; |
| 2448 | 2469 |
| 2449 /// Gets the 1-based line number that the end of [token] lies on. | 2470 /// Gets the 1-based line number that the end of [token] lies on. |
| 2450 int _endLine(Token token) => _lineInfo.getLocation(token.end).lineNumber; | 2471 int _endLine(Token token) => _lineInfo.getLocation(token.end).lineNumber; |
| 2451 | 2472 |
| 2452 /// Gets the 1-based column number that the beginning of [token] lies on. | 2473 /// Gets the 1-based column number that the beginning of [token] lies on. |
| 2453 int _startColumn(Token token) => | 2474 int _startColumn(Token token) => |
| 2454 _lineInfo.getLocation(token.offset).columnNumber; | 2475 _lineInfo.getLocation(token.offset).columnNumber; |
| 2455 } | 2476 } |
| OLD | NEW |