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

Unified Diff: lib/src/rule/argument.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 | « lib/src/line_splitting/solve_state.dart ('k') | lib/src/source_visitor.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/rule/argument.dart
diff --git a/lib/src/rule/argument.dart b/lib/src/rule/argument.dart
index 046ff10ca27f6ee55300c037063719f50541d79a..ed7758a3e216377e42a5c02dc7bb417a19bf33a8 100644
--- a/lib/src/rule/argument.dart
+++ b/lib/src/rule/argument.dart
@@ -9,6 +9,24 @@ import 'rule.dart';
/// Base class for a rule that handles argument or parameter lists.
abstract class ArgumentRule extends Rule {
+ /// The chunks prior to each positional argument.
+ final List<Chunk> _arguments = [];
+
+ /// The rule used to split collections in the argument list, if any.
+ Rule _collectionRule;
+
+ /// The number of leading collection arguments.
+ ///
+ /// This and [_trailingCollections] cannot both be positive. If every
+ /// argument is a collection, this will be [_arguments.length] and
+ /// [_trailingCollections] will be 0.
+ final int _leadingCollections;
+
+ /// The number of trailing collections.
+ ///
+ /// This and [_leadingCollections] cannot both be positive.
+ final int _trailingCollections;
+
/// If true, then inner rules that are written will force this rule to split.
///
/// Temporarily disabled while writing collectio arguments so that they can be
@@ -18,6 +36,27 @@ abstract class ArgumentRule extends Rule {
/// Don't split when an inner collection rule splits.
bool get splitsOnInnerRules => _trackInnerRules;
+ ArgumentRule(this._collectionRule, this._leadingCollections,
+ this._trailingCollections);
+
+ void addConstrainedRules(Set<Rule> rules) {
+ super.addConstrainedRules(rules);
+ if (_collectionRule != null) rules.add(_collectionRule);
+ }
+
+ void forgetUnusedRules() {
+ super.forgetUnusedRules();
+ if (_collectionRule != null && _collectionRule.index == null) {
+ _collectionRule = null;
+ }
+ }
+
+ /// Remembers [chunk] as containing the split that occurs right before an
+ /// argument in the list.
+ void beforeArgument(Chunk chunk) {
+ _arguments.add(chunk);
+ }
+
/// Called before a collection argument is written.
///
/// Disables tracking inner rules while a collection argument is written.
@@ -37,12 +76,6 @@ abstract class ArgumentRule extends Rule {
/// Base class for a rule for handling positional argument lists.
abstract class PositionalRule extends ArgumentRule {
- /// The chunks prior to each positional argument.
- final List<Chunk> _arguments = [];
-
- /// The rule used to split collections in the argument list, if any.
- Rule _collectionRule;
-
/// If there are named arguments following these positional ones, this will
/// be their rule.
Rule _namedArgsRule;
@@ -51,10 +84,12 @@ abstract class PositionalRule extends ArgumentRule {
///
/// If [_collectionRule] is given, it is the rule used to split the collection
/// arguments in the list.
- PositionalRule(this._collectionRule);
+ PositionalRule(
+ Rule collectionRule, int leadingCollections, int trailingCollections)
+ : super(collectionRule, leadingCollections, trailingCollections);
void addConstrainedRules(Set<Rule> rules) {
- if (_collectionRule != null) rules.add(_collectionRule);
+ super.addConstrainedRules(rules);
if (_namedArgsRule != null) rules.add(_namedArgsRule);
}
@@ -63,21 +98,15 @@ abstract class PositionalRule extends ArgumentRule {
if (_namedArgsRule != null && _namedArgsRule.index == null) {
_namedArgsRule = null;
}
-
- if (_collectionRule != null && _collectionRule.index == null) {
- _collectionRule = null;
- }
- }
-
- /// Remembers [chunk] as containing the split that occurs right before an
- /// argument in the list.
- void beforeArgument(Chunk chunk) {
- _arguments.add(chunk);
}
- /// Remembers that [rule] is the [NamedArgsRule] immediately following this
+ /// Remembers that [rule] is the [Rule] immediately following this positional
/// positional argument list.
- void setNamedArgsRule(NamedRule rule) {
+ ///
+ /// This is normally a [NamedRule] but [PositionalRule] is also used for the
+ /// property accesses at the beginning of a call chain, in which case this
+ /// is just a [SimpleRule].
+ void setNamedArgsRule(Rule rule) {
_namedArgsRule = rule;
}
@@ -120,7 +149,7 @@ class SinglePositionalRule extends PositionalRule {
/// collections in the list. If [splitsOnInnerRules] is `true`, then we will
/// split before the argument if the argument itself contains a split.
SinglePositionalRule(Rule collectionRule, {bool splitsOnInnerRules})
- : super(collectionRule),
+ : super(collectionRule, 0, 0),
splitsOnInnerRules =
splitsOnInnerRules != null ? splitsOnInnerRules : false;
@@ -158,18 +187,6 @@ class SinglePositionalRule extends PositionalRule {
/// splits before all of the non-collection arguments, but does not split
/// before the collections, so that they can split internally.
class MultiplePositionalRule extends PositionalRule {
- /// The number of leading collection arguments.
- ///
- /// This and [_trailingCollections] cannot both be positive. If every
- /// argument is a collection, this will be [_arguments.length] and
- /// [_trailingCollections] will be 0.
- final int _leadingCollections;
-
- /// The number of trailing collections.
- ///
- /// This and [_leadingCollections] cannot both be positive.
- final int _trailingCollections;
-
int get numValues {
// Can split before any one argument, none, or all.
var result = 2 + _arguments.length;
@@ -187,8 +204,8 @@ class MultiplePositionalRule extends PositionalRule {
}
MultiplePositionalRule(
- Rule collectionRule, this._leadingCollections, this._trailingCollections)
- : super(collectionRule);
+ Rule collectionRule, int leadingCollections, int trailingCollections)
+ : super(collectionRule, leadingCollections, trailingCollections);
String toString() => "*Pos${super.toString()}";
@@ -273,33 +290,46 @@ class MultiplePositionalRule extends PositionalRule {
/// Splitting rule for a list of named arguments or parameters. Its values mean:
///
-/// * 0: Do not split at all.
-/// * 1: Split only before first argument.
-/// * 2: Split before all arguments, including the first.
+/// * Do not split at all.
+/// * Split only before first argument.
+/// * Split before all arguments.
class NamedRule extends ArgumentRule {
- /// The chunk prior to the first named argument.
- Chunk _first;
-
- /// Whether any of the named arguments are collection literals.
- final bool _containsCollections;
-
int get numValues => 3;
- NamedRule({bool containsCollections})
- : _containsCollections = containsCollections ?? false;
-
- void beforeArguments(Chunk chunk) {
- assert(_first == null);
- _first = chunk;
- }
+ NamedRule(
+ Rule collectionRule, int leadingCollections, int trailingCollections)
+ : super(collectionRule, leadingCollections, trailingCollections);
bool isSplitAtValue(int value, Chunk chunk) {
- // Only split before the first argument.
- if (value == 1) return chunk == _first;
+ // Move all arguments to the second line as a unit.
+ if (value == 1) return chunk == _arguments.first;
- // Split before every argument.
+ // Otherwise, split before all arguments.
return true;
}
+ int constrain(int value, Rule other) {
+ var constrained = super.constrain(value, other);
+ if (constrained != null) return constrained;
+
+ // Decide how to constrain the collection rule.
+ if (other != _collectionRule) return null;
+
+ // If all of the collections are in the named arguments, [_collectionRule]
+ // will not be null, but we don't have to handle it.
+ if (_leadingCollections == 0 && _trailingCollections == 0) return null;
+
+ // If we aren't splitting any args, we can split the collection.
+ if (value == Rule.unsplit) return null;
+
+ // Split only before the first argument. Don't allow the collections to
+ // split.
+ if (value == 1) return Rule.unsplit;
+
+ // Split before all of the arguments, even the collections. We'll allow
+ // them to split but indent their bodies if they do.
+ return null;
+ }
+
String toString() => "Named${super.toString()}";
}
« no previous file with comments | « lib/src/line_splitting/solve_state.dart ('k') | lib/src/source_visitor.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698