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'; |
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 | 310 |
311 // Don't split right after a non-empty curly-bodied function. | 311 // Don't split right after a non-empty curly-bodied function. |
312 if (expression is FunctionExpression) { | 312 if (expression is FunctionExpression) { |
313 if (expression.body is! BlockFunctionBody) return false; | 313 if (expression.body is! BlockFunctionBody) return false; |
314 | 314 |
315 return (expression.body as BlockFunctionBody).block.statements.isEmpty; | 315 return (expression.body as BlockFunctionBody).block.statements.isEmpty; |
316 } | 316 } |
317 | 317 |
318 // If the expression ends in an argument list, base the splitting on the | 318 // If the expression ends in an argument list, base the splitting on the |
319 // last argument. | 319 // last argument. |
320 var argumentList; | 320 ArgumentList argumentList; |
321 if (expression is MethodInvocation) { | 321 if (expression is MethodInvocation) { |
322 argumentList = expression.argumentList; | 322 argumentList = expression.argumentList; |
323 } else if (expression is InstanceCreationExpression) { | 323 } else if (expression is InstanceCreationExpression) { |
324 argumentList = expression.argumentList; | 324 argumentList = expression.argumentList; |
325 } else if (expression is FunctionExpressionInvocation) { | 325 } else if (expression is FunctionExpressionInvocation) { |
326 argumentList = expression.argumentList; | 326 argumentList = expression.argumentList; |
327 } | 327 } |
328 | 328 |
329 // Any other kind of expression always splits. | 329 // Any other kind of expression always splits. |
330 if (argumentList == null) return true; | 330 if (argumentList == null) return true; |
331 if (argumentList.arguments.isEmpty) return true; | 331 if (argumentList.arguments.isEmpty) return true; |
332 | 332 |
333 var argument = argumentList.arguments.last; | 333 var argument = argumentList.arguments.last; |
334 if (argument is NamedExpression) argument = argument.expression; | 334 if (argument is NamedExpression) { |
| 335 argument = (argument as NamedExpression).expression; |
| 336 } |
335 | 337 |
336 // TODO(rnystrom): This logic is similar (but not identical) to | 338 // TODO(rnystrom): This logic is similar (but not identical) to |
337 // ArgumentListVisitor.hasBlockArguments. They overlap conceptually and | 339 // ArgumentListVisitor.hasBlockArguments. They overlap conceptually and |
338 // both have their own peculiar heuristics. It would be good to unify and | 340 // both have their own peculiar heuristics. It would be good to unify and |
339 // rationalize them. | 341 // rationalize them. |
340 | 342 |
341 return _forcesSplit(argument); | 343 return _forcesSplit(argument); |
342 } | 344 } |
343 | 345 |
344 /// Writes [call], which must be one of the supported expression types. | 346 /// Writes [call], which must be one of the supported expression types. |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 // "." when calling a single method on a single name. This is especially | 394 // "." when calling a single method on a single name. This is especially |
393 // important because the identifier is often a library prefix, and splitting | 395 // important because the identifier is often a library prefix, and splitting |
394 // there looks really odd. | 396 // there looks really odd. |
395 if (_properties.isEmpty && | 397 if (_properties.isEmpty && |
396 _calls.length == 1 && | 398 _calls.length == 1 && |
397 _blockCalls == null && | 399 _blockCalls == null && |
398 _target is SimpleIdentifier) { | 400 _target is SimpleIdentifier) { |
399 _endSpan(); | 401 _endSpan(); |
400 } | 402 } |
401 | 403 |
402 _visitor.visit(invocation.argumentList); | 404 _visitor.builder.nestExpression(); |
| 405 _visitor.visit(invocation.typeArguments); |
| 406 _visitor.visitArgumentList(invocation.argumentList, nestExpression: false); |
| 407 _visitor.builder.unnest(); |
403 } | 408 } |
404 | 409 |
405 void _writeBlockCall(MethodInvocation invocation) { | 410 void _writeBlockCall(MethodInvocation invocation) { |
406 _visitor.token(invocation.operator); | 411 _visitor.token(invocation.operator); |
407 _visitor.token(invocation.methodName.token); | 412 _visitor.token(invocation.methodName.token); |
408 _visitor.visit(invocation.argumentList); | 413 _visitor.visit(invocation.argumentList); |
409 } | 414 } |
410 | 415 |
411 /// If a [Rule] for the method chain is currently active, ends it. | 416 /// If a [Rule] for the method chain is currently active, ends it. |
412 void _disableRule() { | 417 void _disableRule() { |
(...skipping 21 matching lines...) Expand all Loading... |
434 } | 439 } |
435 | 440 |
436 /// Ends the span wrapping the call chain if it hasn't ended already. | 441 /// Ends the span wrapping the call chain if it hasn't ended already. |
437 void _endSpan() { | 442 void _endSpan() { |
438 if (_spanEnded) return; | 443 if (_spanEnded) return; |
439 | 444 |
440 _visitor.builder.endSpan(); | 445 _visitor.builder.endSpan(); |
441 _spanEnded = true; | 446 _spanEnded = true; |
442 } | 447 } |
443 } | 448 } |
OLD | NEW |