| 1 | | // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
|
| 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.
|
| 4 | |
|
| 5 | | library dart_style.src.rule.metadata;
|
| 6 | |
|
| 7 | | import 'argument.dart';
|
| 8 | | import 'rule.dart';
|
| 9 | |
|
| 10 | | /// Rule for handling splits between parameter metadata annotations and the
|
| 11 | | /// following parameter.
|
| 12 | | ///
|
| 13 | | /// Metadata annotations for parameters (and type parameters) get some special
|
| 14 | | /// handling. We use a single rule for all annotations in the parameter list.
|
| 15 | | /// If any of the annotations split, they all do.
|
| 16 | | ///
|
| 17 | | /// Also, if the annotations split, we force the entire parameter list to fully
|
| 18 | | /// split, both named and positional.
|
| 19 | | class MetadataRule extends Rule {
|
| 20 | | Rule _positionalRule;
|
| 21 | | Rule _namedRule;
|
| 22 | |
|
| 23 | | /// Remembers that [rule] is the [PositionalRule] used by the argument list
|
| 24 | | /// containing the parameter metadata using this rule.
|
| 25 | 2 | void bindPositionalRule(PositionalRule rule) {
|
| 26 | 2 | _positionalRule = rule;
|
| 27 | | }
|
| 28 | |
|
| 29 | | /// Remembers that [rule] is the [NamedRule] used by the argument list
|
| 30 | | /// containing the parameter metadata using this rule.
|
| 31 | 2 | void bindNamedRule(NamedRule rule) {
|
| 32 | 2 | _namedRule = rule;
|
| 33 | | }
|
| 34 | |
|
| 35 | | /// Constrains the surrounding argument list rules to fully split if the
|
| 36 | | /// metadata does.
|
| 37 | 2 | int constrain(int value, Rule other) {
|
| 38 | 2 | var constrained = super.constrain(value, other);
|
| 39 | | if (constrained != null) return constrained;
|
| 40 | |
|
| 41 | | // If the metadata doesn't split, we don't care what the arguments do.
|
| 42 | 2 | if (value == Rule.unsplit) return null;
|
| 43 | |
|
| 44 | | // Otherwise, they have to split.
|
| 45 | 0 | if (other == _positionalRule) return _positionalRule.fullySplitValue;
|
| 46 | 0 | if (other == _namedRule) return _namedRule.fullySplitValue;
|
| 47 | |
|
| 48 | | return null;
|
| 49 | | }
|
| 50 | |
|
| 51 | 2 | void addConstrainedRules(Set<Rule> rules) {
|
| 52 | 6 | if (_positionalRule != null) rules.add(_positionalRule);
|
| 53 | 6 | if (_namedRule != null) rules.add(_namedRule);
|
| 54 | | }
|
| 55 | |
|
| 56 | 2 | void forgetUnusedRules() {
|
| 57 | 2 | super.forgetUnusedRules();
|
| 58 | 6 | if (_positionalRule != null && _positionalRule.index == null) {
|
| 59 | 0 | _positionalRule = null;
|
| 60 | | }
|
| 61 | |
|
| 62 | 6 | if (_namedRule != null && _namedRule.index == null) {
|
| 63 | 0 | _namedRule = null;
|
| 64 | | }
|
| 65 | | }
|
| 66 | | }
|