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

Unified Diff: lib/src/argument_list_visitor.dart

Issue 1493553002: Smarter splitting around named collection arguments. (Closed) Base URL: https://github.com/dart-lang/dart_style.git@master
Patch Set: Created 5 years 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') | lib/src/call_chain_visitor.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/argument_list_visitor.dart
diff --git a/lib/src/argument_list_visitor.dart b/lib/src/argument_list_visitor.dart
index 2fb8e97c2cd5a35480827906136e3bb083fba090..b07198722b2ffd86b87e2ab8c10496de29ccee75 100644
--- a/lib/src/argument_list_visitor.dart
+++ b/lib/src/argument_list_visitor.dart
@@ -4,6 +4,8 @@
library dart_style.src.argument_list_visitor;
+import 'dart:math' as math;
+
import 'package:analyzer/analyzer.dart';
import 'package:analyzer/src/generated/scanner.dart';
@@ -261,16 +263,6 @@ class ArgumentSublist {
}
}
- // If only some of the named arguments are collections, treat none of them
- // specially. Avoids cases like:
- //
- // function(
- // a: arg,
- // b: [
- // ...
- // ]);
- if (trailingCollections < named.length) trailingCollections = 0;
-
// Collections must all be a prefix or suffix of the argument list (and not
// both).
if (leadingCollections != collections.length) leadingCollections = 0;
@@ -308,69 +300,64 @@ class ArgumentSublist {
splitsOnInnerRules: _allArguments.length > 1 &&
!_collections.containsKey(_positional.first));
} else {
- // Only count the positional bodies in the positional rule.
- var leadingPositional = _leadingCollections;
- if (_leadingCollections == _positional.length + _named.length) {
- leadingPositional -= _named.length;
- }
-
- var trailingPositional = _trailingCollections - _named.length;
+ // Only count the collections in the positional rule.
+ var leadingCollections =
+ math.min(_leadingCollections, _positional.length);
+ var trailingCollections =
+ math.max(_trailingCollections - _named.length, 0);
rule = new MultiplePositionalRule(
- _collectionRule, leadingPositional, trailingPositional);
- }
-
- visitor.builder.startRule(rule);
-
- _previousSplit =
- visitor.builder.split(space: !_isFirstArgument(_positional.first));
- rule.beforeArgument(_previousSplit);
-
- // Try to not split the arguments.
- visitor.builder.startSpan(Cost.positionalArguments);
-
- for (var argument in _positional) {
- _visitArgument(visitor, rule, argument);
-
- // Positional arguments split independently.
- if (argument != _positional.last) {
- _previousSplit = visitor.split();
- rule.beforeArgument(_previousSplit);
- }
+ _collectionRule, leadingCollections, trailingCollections);
}
- visitor.builder.endSpan();
- visitor.builder.endRule();
-
+ _visitArguments(visitor, _positional, rule);
return rule;
}
/// Writes the named arguments, if any.
- void _visitNamed(SourceVisitor visitor, PositionalRule rule) {
+ void _visitNamed(SourceVisitor visitor, PositionalRule positionalRule) {
if (_named.isEmpty) return;
- var positionalRule = rule;
- var namedRule = new NamedRule(
- containsCollections: _leadingCollections > _positional.length ||
- _trailingCollections > 0);
- visitor.builder.startRule(namedRule);
+ // Only count the collections in the named rule.
+ var leadingCollections =
+ math.max(_leadingCollections - _positional.length, 0);
+ var trailingCollections = math.min(_trailingCollections, _named.length);
+ var namedRule =
+ new NamedRule(_collectionRule, leadingCollections, trailingCollections);
// Let the positional args force the named ones to split.
if (positionalRule != null) {
positionalRule.setNamedArgsRule(namedRule);
}
- // Split before the first named argument.
+ _visitArguments(visitor, _named, namedRule);
+ }
+
+ void _visitArguments(
+ SourceVisitor visitor, List<Expression> arguments, ArgumentRule rule) {
+ visitor.builder.startRule(rule);
+
+ // Split before the first argument.
_previousSplit =
- visitor.builder.split(space: !_isFirstArgument(_named.first));
- namedRule.beforeArguments(_previousSplit);
+ visitor.builder.split(space: !_isFirstArgument(arguments.first));
+ rule.beforeArgument(_previousSplit);
- for (var argument in _named) {
- _visitArgument(visitor, namedRule, argument);
+ // Try to not split the positional arguments.
+ if (arguments == _positional) {
+ visitor.builder.startSpan(Cost.positionalArguments);
+ }
+
+ for (var argument in arguments) {
+ _visitArgument(visitor, rule, argument);
// Write the split.
- if (argument != _named.last) _previousSplit = visitor.split();
+ if (argument != arguments.last) {
+ _previousSplit = visitor.split();
+ rule.beforeArgument(_previousSplit);
+ }
}
+ if (arguments == _positional) visitor.builder.endSpan();
+
visitor.builder.endRule();
}
« no previous file with comments | « CHANGELOG.md ('k') | lib/src/call_chain_visitor.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698