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.call_chain_visitor; | 5 library dart_style.src.call_chain_visitor; |
6 | 6 |
7 import 'package:analyzer/analyzer.dart'; | 7 import 'package:analyzer/analyzer.dart'; |
8 | 8 |
9 import 'argument_list_visitor.dart'; | 9 import 'argument_list_visitor.dart'; |
10 import 'rule/argument.dart'; | 10 import 'rule/argument.dart'; |
| 11 import 'rule/rule.dart'; |
11 import 'source_visitor.dart'; | 12 import 'source_visitor.dart'; |
12 | 13 |
13 /// Helper class for [SourceVisitor] that handles visiting and writing a | 14 /// Helper class for [SourceVisitor] that handles visiting and writing a |
14 /// chained series of method invocations, property accesses, and/or prefix | 15 /// chained series of method invocations, property accesses, and/or prefix |
15 /// expressions. In other words, anything using the "." operator. | 16 /// expressions. In other words, anything using the "." operator. |
16 class CallChainVisitor { | 17 class CallChainVisitor { |
17 final SourceVisitor _visitor; | 18 final SourceVisitor _visitor; |
18 | 19 |
19 /// The initial target of the call chain. | 20 /// The initial target of the call chain. |
20 /// | 21 /// |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 // * ParenthesizedExpression. | 301 // * ParenthesizedExpression. |
301 // * The right operand in an infix operator call. | 302 // * The right operand in an infix operator call. |
302 // * The body of a `=>` function. | 303 // * The body of a `=>` function. |
303 | 304 |
304 // Don't split right after a collection literal. | 305 // Don't split right after a collection literal. |
305 if (expression is ListLiteral) return false; | 306 if (expression is ListLiteral) return false; |
306 if (expression is MapLiteral) return false; | 307 if (expression is MapLiteral) return false; |
307 | 308 |
308 // Don't split right after a non-empty curly-bodied function. | 309 // Don't split right after a non-empty curly-bodied function. |
309 if (expression is FunctionExpression) { | 310 if (expression is FunctionExpression) { |
310 var function = expression as FunctionExpression; | 311 if (expression.body is! BlockFunctionBody) return false; |
311 | 312 |
312 if (function.body is! BlockFunctionBody) return false; | 313 return (expression.body as BlockFunctionBody).block.statements.isEmpty; |
313 | |
314 return (function.body as BlockFunctionBody).block.statements.isEmpty; | |
315 } | 314 } |
316 | 315 |
317 // If the expression ends in an argument list, base the splitting on the | 316 // If the expression ends in an argument list, base the splitting on the |
318 // last argument. | 317 // last argument. |
319 var argumentList; | 318 var argumentList; |
320 if (expression is MethodInvocation) { | 319 if (expression is MethodInvocation) { |
321 argumentList = (expression as MethodInvocation).argumentList; | 320 argumentList = expression.argumentList; |
322 } else if (expression is InstanceCreationExpression) { | 321 } else if (expression is InstanceCreationExpression) { |
323 argumentList = (expression as InstanceCreationExpression).argumentList; | 322 argumentList = expression.argumentList; |
324 } else if (expression is FunctionExpressionInvocation) { | 323 } else if (expression is FunctionExpressionInvocation) { |
325 argumentList = (expression as FunctionExpressionInvocation).argumentList; | 324 argumentList = expression.argumentList; |
326 } | 325 } |
327 | 326 |
328 // Any other kind of expression always splits. | 327 // Any other kind of expression always splits. |
329 if (argumentList == null) return true; | 328 if (argumentList == null) return true; |
330 if (argumentList.arguments.isEmpty) return true; | 329 if (argumentList.arguments.isEmpty) return true; |
331 | 330 |
332 var argument = argumentList.arguments.last; | 331 var argument = argumentList.arguments.last; |
333 if (argument is NamedExpression) argument = argument.expression; | 332 if (argument is NamedExpression) argument = argument.expression; |
334 | 333 |
335 // TODO(rnystrom): This logic is similar (but not identical) to | 334 // TODO(rnystrom): This logic is similar (but not identical) to |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 | 412 |
414 _visitor.builder.endRule(); | 413 _visitor.builder.endRule(); |
415 _ruleEnabled = false; | 414 _ruleEnabled = false; |
416 } | 415 } |
417 | 416 |
418 /// Creates a new method chain [Rule] if one is not already active. | 417 /// Creates a new method chain [Rule] if one is not already active. |
419 void _enableRule({bool lazy: false}) { | 418 void _enableRule({bool lazy: false}) { |
420 if (_ruleEnabled) return; | 419 if (_ruleEnabled) return; |
421 | 420 |
422 // If the properties split, force the calls to split too. | 421 // If the properties split, force the calls to split too. |
423 var rule = new NamedRule(); | 422 var rule = new Rule(); |
424 if (_propertyRule != null) _propertyRule.setNamedArgsRule(rule); | 423 if (_propertyRule != null) _propertyRule.setNamedArgsRule(rule); |
425 | 424 |
426 if (lazy) { | 425 if (lazy) { |
427 _visitor.builder.startLazyRule(rule); | 426 _visitor.builder.startLazyRule(rule); |
428 } else { | 427 } else { |
429 _visitor.builder.startRule(rule); | 428 _visitor.builder.startRule(rule); |
430 } | 429 } |
431 | 430 |
432 _ruleEnabled = true; | 431 _ruleEnabled = true; |
433 } | 432 } |
434 | 433 |
435 /// Ends the span wrapping the call chain if it hasn't ended already. | 434 /// Ends the span wrapping the call chain if it hasn't ended already. |
436 void _endSpan() { | 435 void _endSpan() { |
437 if (_spanEnded) return; | 436 if (_spanEnded) return; |
438 | 437 |
439 _visitor.builder.endSpan(); | 438 _visitor.builder.endSpan(); |
440 _spanEnded = true; | 439 _spanEnded = true; |
441 } | 440 } |
442 } | 441 } |
OLD | NEW |