| 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.type_argument;
|
| 6 | |
|
| 7 | | import '../chunk.dart';
|
| 8 | | import 'rule.dart';
|
| 9 | |
|
| 10 | | /// Rule for splitting a list of type arguments or type parameters. Type
|
| 11 | | /// parameters split a little differently from normal value argument lists. In
|
| 12 | | /// particular, this tries harder to avoid splitting before the first type
|
| 13 | | /// argument since that looks stranger with `<...>` than it does with `(...)`.
|
| 14 | | ///
|
| 15 | | /// The values for a rule for `n` arguments are:
|
| 16 | | ///
|
| 17 | | /// * `0`: No splits at all.
|
| 18 | | /// * `1 ... n`: Split before one argument, starting from the last.
|
| 19 | | /// * `n + 1`: Split before all arguments.
|
| 20 | | ///
|
| 21 | | /// If there is only one type argument, the last two cases collapse and there
|
| 22 | | /// are only two values.
|
| 23 | | class TypeArgumentRule extends Rule {
|
| 24 | | /// The chunks prior to each positional type argument.
|
| 25 | | final List<Chunk> _arguments = [];
|
| 26 | |
|
| 27 | 2 | int get cost => Cost.typeArgument;
|
| 28 | |
|
| 29 | 14 | int get numValues => _arguments.length == 1 ? 2 : _arguments.length + 2;
|
| 30 | |
|
| 31 | | /// Remembers [chunk] as containing the split that occurs right before a type
|
| 32 | | /// argument in the list.
|
| 33 | 2 | void beforeArgument(Chunk chunk) {
|
| 34 | 4 | _arguments.add(chunk);
|
| 35 | | }
|
| 36 | |
|
| 37 | 2 | bool isSplit(int value, Chunk chunk) {
|
| 38 | | // Don't split at all.
|
| 39 | 2 | if (value == Rule.unsplit) return false;
|
| 40 | |
|
| 41 | | // Split before every argument.
|
| 42 | 6 | if (value == numValues - 1) return true;
|
| 43 | |
|
| 44 | | // Split before a single argument. Try later arguments before earlier ones
|
| 45 | | // to try to keep as much on the first line as possible.
|
| 46 | 7 | return chunk == _arguments[_arguments.length - value];
|
| 47 | | }
|
| 48 | |
|
| 49 | 0 | String toString() => "TypeArg${super.toString()}";
|
| 50 | | }
|