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

Unified 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, 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « CHANGELOG.md ('k') | test/io_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/source_visitor.dart
diff --git a/lib/src/source_visitor.dart b/lib/src/source_visitor.dart
index cfa79b7cafeb5a2fe4f3a1d3f33a527ebb87fa41..557fdab5e374846a5350c61d177156690825ad18 100644
--- a/lib/src/source_visitor.dart
+++ b/lib/src/source_visitor.dart
@@ -145,6 +145,15 @@ class SourceVisitor implements AstVisitor {
return;
}
+ // If the argument list has a trailing comma, format it like a collection
+ // literal where each argument goes on its own line, they are indented +2,
+ // and the ")" ends up on its own line.
+ if (node.arguments.last.endToken.next.type == TokenType.COMMA) {
+ _visitCollectionLiteral(
+ null, node.leftParenthesis, node.arguments, node.rightParenthesis);
+ return;
+ }
+
new ArgumentListVisitor(this, node).visit();
}
@@ -1889,11 +1898,16 @@ class SourceVisitor implements AstVisitor {
/// Visits the collection literal [node] whose body starts with [leftBracket],
/// ends with [rightBracket] and contains [elements].
+ ///
+ /// This is also used for argument lists with a trailing comma which are
+ /// considered "collection-like". In that case, [node] is `null`.
void _visitCollectionLiteral(TypedLiteral node, Token leftBracket,
Iterable<AstNode> elements, Token rightBracket,
[int cost]) {
- modifier(node.constKeyword);
- visit(node.typeArguments);
+ if (node != null) {
+ modifier(node.constKeyword);
+ visit(node.typeArguments);
+ }
// Don't allow splitting in an empty collection.
if (elements.isEmpty && rightBracket.precedingComments == null) {
@@ -1958,7 +1972,9 @@ class SourceVisitor implements AstVisitor {
visit(element);
// The comma after the element.
- if (element.endToken.next.lexeme == ",") token(element.endToken.next);
+ if (element.endToken.next.type == TokenType.COMMA) {
+ token(element.endToken.next);
+ }
builder.unnest();
}
@@ -1968,6 +1984,12 @@ class SourceVisitor implements AstVisitor {
// If there is a collection inside this one, it forces this one to split.
var force = _collectionSplits.removeLast();
+ // If the collection has a trailing comma, the user must want it to split.
+ if (elements.isNotEmpty &&
+ elements.last.endToken.next.type == TokenType.COMMA) {
+ force = true;
+ }
+
_endLiteralBody(rightBracket, ignoredRule: rule, forceSplit: force);
}
« 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