Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(138)

Side by Side Diff: lib/src/source_visitor.dart

Issue 2181743002: Always split collections and argument lists with trailing commas. (Closed) Base URL: https://github.com/dart-lang/dart_style.git@master
Patch Set: Revise. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « CHANGELOG.md ('k') | test/io_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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(
153 null, node.leftParenthesis, node.arguments, node.rightParenthesis);
154 return;
155 }
156
148 new ArgumentListVisitor(this, node).visit(); 157 new ArgumentListVisitor(this, node).visit();
149 } 158 }
150 159
151 visitAsExpression(AsExpression node) { 160 visitAsExpression(AsExpression node) {
152 builder.startSpan(); 161 builder.startSpan();
153 visit(node.expression); 162 visit(node.expression);
154 soloSplit(); 163 soloSplit();
155 token(node.asOperator); 164 token(node.asOperator);
156 space(); 165 space();
157 visit(node.type); 166 visit(node.type);
(...skipping 1724 matching lines...) Expand 10 before | Expand all | Expand 10 after
1882 1891
1883 visit(node); 1892 visit(node);
1884 1893
1885 // The comma after the node. 1894 // The comma after the node.
1886 if (node.endToken.next.lexeme == ",") token(node.endToken.next); 1895 if (node.endToken.next.lexeme == ",") token(node.endToken.next);
1887 } 1896 }
1888 } 1897 }
1889 1898
1890 /// Visits the collection literal [node] whose body starts with [leftBracket], 1899 /// Visits the collection literal [node] whose body starts with [leftBracket],
1891 /// ends with [rightBracket] and contains [elements]. 1900 /// ends with [rightBracket] and contains [elements].
1901 ///
1902 /// This is also used for argument lists with a trailing comma which are
1903 /// considered "collection-like". In that case, [node] is `null`.
1892 void _visitCollectionLiteral(TypedLiteral node, Token leftBracket, 1904 void _visitCollectionLiteral(TypedLiteral node, Token leftBracket,
1893 Iterable<AstNode> elements, Token rightBracket, 1905 Iterable<AstNode> elements, Token rightBracket,
1894 [int cost]) { 1906 [int cost]) {
1895 modifier(node.constKeyword); 1907 if (node != null) {
1896 visit(node.typeArguments); 1908 modifier(node.constKeyword);
1909 visit(node.typeArguments);
1910 }
1897 1911
1898 // Don't allow splitting in an empty collection. 1912 // Don't allow splitting in an empty collection.
1899 if (elements.isEmpty && rightBracket.precedingComments == null) { 1913 if (elements.isEmpty && rightBracket.precedingComments == null) {
1900 token(leftBracket); 1914 token(leftBracket);
1901 token(rightBracket); 1915 token(rightBracket);
1902 return; 1916 return;
1903 } 1917 }
1904 1918
1905 // Force all of the surrounding collections to split. 1919 // Force all of the surrounding collections to split.
1906 for (var i = 0; i < _collectionSplits.length; i++) { 1920 for (var i = 0; i < _collectionSplits.length; i++) {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1951 } 1965 }
1952 } else { 1966 } else {
1953 builder.split(nest: false, space: true); 1967 builder.split(nest: false, space: true);
1954 } 1968 }
1955 } 1969 }
1956 1970
1957 builder.nestExpression(); 1971 builder.nestExpression();
1958 visit(element); 1972 visit(element);
1959 1973
1960 // The comma after the element. 1974 // The comma after the element.
1961 if (element.endToken.next.lexeme == ",") token(element.endToken.next); 1975 if (element.endToken.next.type == TokenType.COMMA) {
1976 token(element.endToken.next);
1977 }
1962 1978
1963 builder.unnest(); 1979 builder.unnest();
1964 } 1980 }
1965 1981
1966 builder.endRule(); 1982 builder.endRule();
1967 1983
1968 // If there is a collection inside this one, it forces this one to split. 1984 // If there is a collection inside this one, it forces this one to split.
1969 var force = _collectionSplits.removeLast(); 1985 var force = _collectionSplits.removeLast();
1970 1986
1987 // If the collection has a trailing comma, the user must want it to split.
1988 if (elements.isNotEmpty &&
1989 elements.last.endToken.next.type == TokenType.COMMA) {
1990 force = true;
1991 }
1992
1971 _endLiteralBody(rightBracket, ignoredRule: rule, forceSplit: force); 1993 _endLiteralBody(rightBracket, ignoredRule: rule, forceSplit: force);
1972 } 1994 }
1973 1995
1974 /// Gets the cost to split at an assignment (or `:` in the case of a named 1996 /// Gets the cost to split at an assignment (or `:` in the case of a named
1975 /// default value) with the given [rightHandSide]. 1997 /// default value) with the given [rightHandSide].
1976 /// 1998 ///
1977 /// "Block-like" expressions (collections and cascades) bind a bit tighter 1999 /// "Block-like" expressions (collections and cascades) bind a bit tighter
1978 /// because it looks better to have code like: 2000 /// because it looks better to have code like:
1979 /// 2001 ///
1980 /// var list = [ 2002 /// var list = [
(...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after
2446 /// Gets the 1-based line number that the beginning of [token] lies on. 2468 /// Gets the 1-based line number that the beginning of [token] lies on.
2447 int _startLine(Token token) => _lineInfo.getLocation(token.offset).lineNumber; 2469 int _startLine(Token token) => _lineInfo.getLocation(token.offset).lineNumber;
2448 2470
2449 /// Gets the 1-based line number that the end of [token] lies on. 2471 /// Gets the 1-based line number that the end of [token] lies on.
2450 int _endLine(Token token) => _lineInfo.getLocation(token.end).lineNumber; 2472 int _endLine(Token token) => _lineInfo.getLocation(token.end).lineNumber;
2451 2473
2452 /// Gets the 1-based column number that the beginning of [token] lies on. 2474 /// Gets the 1-based column number that the beginning of [token] lies on.
2453 int _startColumn(Token token) => 2475 int _startColumn(Token token) =>
2454 _lineInfo.getLocation(token.offset).columnNumber; 2476 _lineInfo.getLocation(token.offset).columnNumber;
2455 } 2477 }
OLDNEW
« no previous file with comments | « CHANGELOG.md ('k') | test/io_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698