OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library dart_style.src.argument_list_visitor; | 5 library dart_style.src.argument_list_visitor; |
6 | 6 |
7 import 'package:analyzer/analyzer.dart'; | 7 import 'package:analyzer/analyzer.dart'; |
8 | 8 |
9 import 'chunk.dart'; | 9 import 'chunk.dart'; |
10 import 'rule/argument.dart'; | 10 import 'rule/argument.dart'; |
(...skipping 29 matching lines...) Expand all Loading... |
40 | 40 |
41 /// The rule used to split the bodies of all of the block arguments. | 41 /// The rule used to split the bodies of all of the block arguments. |
42 Rule get _blockArgumentRule { | 42 Rule get _blockArgumentRule { |
43 // Lazy initialize. | 43 // Lazy initialize. |
44 if (_blockRule == null && _blockArguments.isNotEmpty) { | 44 if (_blockRule == null && _blockArguments.isNotEmpty) { |
45 _blockRule = new SimpleRule(cost: Cost.splitBlocks); | 45 _blockRule = new SimpleRule(cost: Cost.splitBlocks); |
46 } | 46 } |
47 | 47 |
48 return _blockRule; | 48 return _blockRule; |
49 } | 49 } |
| 50 |
50 Rule _blockRule; | 51 Rule _blockRule; |
51 | 52 |
52 /// Returns `true` if there is only a single positional argument. | 53 /// Returns `true` if there is only a single positional argument. |
53 bool get _isSingle => _positional.length == 1 && _named.isEmpty; | 54 bool get _isSingle => _positional.length == 1 && _named.isEmpty; |
54 | 55 |
55 /// Whether this argument list has any block arguments that are functions. | 56 /// Whether this argument list has any block arguments that are functions. |
56 bool get hasFunctionBlockArguments => _blockArguments.any(_isBlockFunction); | 57 bool get hasFunctionBlockArguments => _blockArguments.any(_isBlockFunction); |
57 | 58 |
58 bool get hasBlockArguments => _blockArguments.isNotEmpty; | 59 bool get hasBlockArguments => _blockArguments.isNotEmpty; |
59 | 60 |
60 /// Whether this argument list should force the containing method chain to | 61 /// Whether this argument list should force the containing method chain to |
61 /// add a level of block nesting. | 62 /// add a level of block nesting. |
62 bool get nestMethodArguments { | 63 bool get nestMethodArguments { |
63 // If there are block arguments, we don't want the method to force them to | 64 // If there are block arguments, we don't want the method to force them to |
64 // the right. | 65 // the right. |
65 if (_blockArguments.isNotEmpty) return false; | 66 if (_blockArguments.isNotEmpty) return false; |
66 | 67 |
67 // Corner case: If there is just a single argument, don't bump the nesting. | 68 // Corner case: If there is just a single argument, don't bump the nesting. |
68 // This lets us avoid spurious indentation in cases like: | 69 // This lets us avoid spurious indentation in cases like: |
69 // | 70 // |
70 // object.method(function(() { | 71 // object.method(function(() { |
71 // body; | 72 // body; |
72 // })); | 73 // })); |
73 return _node.arguments.length > 1; | 74 return _node.arguments.length > 1; |
74 } | 75 } |
75 | 76 |
76 factory ArgumentListVisitor(SourceVisitor visitor, ArgumentList node) { | 77 factory ArgumentListVisitor(SourceVisitor visitor, ArgumentList node) { |
77 // Assumes named arguments follow all positional ones. | 78 // Assumes named arguments follow all positional ones. |
78 var positional = node.arguments | 79 var positional = |
79 .takeWhile((arg) => arg is! NamedExpression).toList(); | 80 node.arguments.takeWhile((arg) => arg is! NamedExpression).toList(); |
80 var named = node.arguments.skip(positional.length).toList(); | 81 var named = node.arguments.skip(positional.length).toList(); |
81 | 82 |
82 var blocks = node.arguments.where(_isBlockArgument).toSet(); | 83 var blocks = node.arguments.where(_isBlockArgument).toSet(); |
83 | 84 |
84 // Count the leading arguments that are block literals. | 85 // Count the leading arguments that are block literals. |
85 var leadingBlocks = 0; | 86 var leadingBlocks = 0; |
86 for (var argument in node.arguments) { | 87 for (var argument in node.arguments) { |
87 if (!blocks.contains(argument)) break; | 88 if (!blocks.contains(argument)) break; |
88 leadingBlocks++; | 89 leadingBlocks++; |
89 } | 90 } |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 var positionalRule = rule; | 202 var positionalRule = rule; |
202 var namedRule = new NamedRule(_blockArgumentRule); | 203 var namedRule = new NamedRule(_blockArgumentRule); |
203 _visitor.builder.startRule(namedRule); | 204 _visitor.builder.startRule(namedRule); |
204 | 205 |
205 // Let the positional args force the named ones to split. | 206 // Let the positional args force the named ones to split. |
206 if (positionalRule != null) { | 207 if (positionalRule != null) { |
207 positionalRule.setNamedArgsRule(namedRule); | 208 positionalRule.setNamedArgsRule(namedRule); |
208 } | 209 } |
209 | 210 |
210 // Split before the first named argument. | 211 // Split before the first named argument. |
211 namedRule.beforeArguments( | 212 namedRule |
212 _visitor.builder.split(space: _positional.isNotEmpty)); | 213 .beforeArguments(_visitor.builder.split(space: _positional.isNotEmpty)); |
213 | 214 |
214 for (var argument in _named) { | 215 for (var argument in _named) { |
215 _writeArgument(namedRule, argument); | 216 _writeArgument(namedRule, argument); |
216 | 217 |
217 // Write the split. | 218 // Write the split. |
218 if (argument != _named.last) _visitor.split(); | 219 if (argument != _named.last) _visitor.split(); |
219 } | 220 } |
220 | 221 |
221 _visitor.builder.endRule(); | 222 _visitor.builder.endRule(); |
222 } | 223 } |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 if (expression is NamedExpression) { | 281 if (expression is NamedExpression) { |
281 expression = (expression as NamedExpression).expression; | 282 expression = (expression as NamedExpression).expression; |
282 } | 283 } |
283 | 284 |
284 // Curly body functions are. | 285 // Curly body functions are. |
285 if (expression is! FunctionExpression) return false; | 286 if (expression is! FunctionExpression) return false; |
286 var function = expression as FunctionExpression; | 287 var function = expression as FunctionExpression; |
287 return function.body is BlockFunctionBody; | 288 return function.body is BlockFunctionBody; |
288 } | 289 } |
289 } | 290 } |
OLD | NEW |