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

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: 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
Index: lib/src/source_visitor.dart
diff --git a/lib/src/source_visitor.dart b/lib/src/source_visitor.dart
index cfa79b7cafeb5a2fe4f3a1d3f33a527ebb87fa41..b236fcba40ef7ebe37ac67cd6bd38d0c06e95849 100644
--- a/lib/src/source_visitor.dart
+++ b/lib/src/source_visitor.dart
@@ -145,6 +145,14 @@ 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);
nweiz 2016/07/25 22:29:06 Long line.
Bob Nystrom 2016/07/26 21:22:07 Ironically, I forgot to format. Done.
+ return;
+ }
+
new ArgumentListVisitor(this, node).visit();
}
@@ -1889,11 +1897,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 +1971,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 +1983,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);
}
« CHANGELOG.md ('K') | « CHANGELOG.md ('k') | test/regression/0200/0223.stmt » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698