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 services.src.correction.assist; | 5 library services.src.correction.assist; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 | 9 |
10 import 'package:analysis_server/plugin/edit/assist/assist_core.dart'; | 10 import 'package:analysis_server/plugin/edit/assist/assist_core.dart'; |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
114 _addProposal_addTypeAnnotation_DeclaredIdentifier(); | 114 _addProposal_addTypeAnnotation_DeclaredIdentifier(); |
115 _addProposal_addTypeAnnotation_SimpleFormalParameter(); | 115 _addProposal_addTypeAnnotation_SimpleFormalParameter(); |
116 _addProposal_addTypeAnnotation_VariableDeclaration(); | 116 _addProposal_addTypeAnnotation_VariableDeclaration(); |
117 _addProposal_assignToLocalVariable(); | 117 _addProposal_assignToLocalVariable(); |
118 _addProposal_convertIntoFinalField(); | 118 _addProposal_convertIntoFinalField(); |
119 _addProposal_convertIntoGetter(); | 119 _addProposal_convertIntoGetter(); |
120 _addProposal_convertDocumentationIntoBlock(); | 120 _addProposal_convertDocumentationIntoBlock(); |
121 _addProposal_convertDocumentationIntoLine(); | 121 _addProposal_convertDocumentationIntoLine(); |
122 _addProposal_convertToBlockFunctionBody(); | 122 _addProposal_convertToBlockFunctionBody(); |
123 _addProposal_convertToExpressionFunctionBody(); | 123 _addProposal_convertToExpressionFunctionBody(); |
124 _addProposal_convertFlutterChild(); | |
124 _addProposal_convertToForIndexLoop(); | 125 _addProposal_convertToForIndexLoop(); |
125 _addProposal_convertToIsNot_onIs(); | 126 _addProposal_convertToIsNot_onIs(); |
126 _addProposal_convertToIsNot_onNot(); | 127 _addProposal_convertToIsNot_onNot(); |
127 _addProposal_convertToIsNotEmpty(); | 128 _addProposal_convertToIsNotEmpty(); |
128 _addProposal_convertToFieldParameter(); | 129 _addProposal_convertToFieldParameter(); |
129 _addProposal_convertToNormalParameter(); | 130 _addProposal_convertToNormalParameter(); |
130 _addProposal_encapsulateField(); | 131 _addProposal_encapsulateField(); |
131 _addProposal_exchangeOperands(); | 132 _addProposal_exchangeOperands(); |
132 _addProposal_importAddShow(); | 133 _addProposal_importAddShow(); |
133 _addProposal_introduceLocalTestedType(); | 134 _addProposal_introduceLocalTestedType(); |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
498 } | 499 } |
499 } | 500 } |
500 _insertBuilder(sb, comment.length); | 501 _insertBuilder(sb, comment.length); |
501 } | 502 } |
502 } | 503 } |
503 } | 504 } |
504 // add proposal | 505 // add proposal |
505 _addAssist(DartAssistKind.CONVERT_DOCUMENTATION_INTO_LINE, []); | 506 _addAssist(DartAssistKind.CONVERT_DOCUMENTATION_INTO_LINE, []); |
506 } | 507 } |
507 | 508 |
509 void _addProposal_convertFlutterChild() { | |
510 NamedExpression namedExp; | |
511 // Allow assist to activate from either the new-expr or the child: arg. | |
512 if (node is SimpleIdentifier && | |
513 node.parent is Label && | |
514 node.parent.parent is NamedExpression) { | |
515 namedExp = node.parent.parent as NamedExpression; | |
516 if ((node as SimpleIdentifier).name != 'child' || | |
517 namedExp.expression == null) { | |
518 return; | |
519 } | |
520 InstanceCreationExpression newExpr = namedExp.parent?.parent; | |
Brian Wilkerson
2017/03/15 21:54:23
The parent might not be an InstanceCreationExpress
| |
521 if (newExpr == null || !_isFlutterInstanceCreationExpression(newExpr)) { | |
522 return; | |
523 } | |
524 } else { | |
525 InstanceCreationExpression newExpr = _identifyNewExpression(); | |
526 if (newExpr == null || !_isFlutterInstanceCreationExpression(newExpr)) { | |
527 _coverageMarker(); | |
528 return; | |
529 } | |
530 namedExp = _findChildArgument(newExpr); | |
531 if (namedExp == null || namedExp.expression == null) { | |
532 _coverageMarker(); | |
533 return; | |
534 } | |
535 } | |
536 InstanceCreationExpression childArg = _getChildWidget(namedExp, false); | |
537 if (childArg == null) { | |
538 _coverageMarker(); | |
539 return; | |
540 } | |
541 int childLoc = namedExp.offset + 'child'.length; | |
542 _addInsertEdit(childLoc, 'ren'); | |
543 int listLoc = childArg.offset; | |
544 String childArgSrc = utils.getNodeText(childArg); | |
545 if (!childArgSrc.contains(eol)) { | |
546 _addInsertEdit(listLoc, '<Widget>['); | |
547 _addInsertEdit(listLoc + childArg.length, ']'); | |
548 } else { | |
549 int newlineLoc = childArgSrc.lastIndexOf(eol); | |
550 if (newlineLoc == childArgSrc.length) { | |
551 newlineLoc -= 1; | |
552 } | |
553 String indentOld = utils.getLinePrefix(childArg.offset + 1 + newlineLoc); | |
554 String indentNew = '$indentOld${utils.getIndent(1)}'; | |
555 // The separator includes 'child:' but that has no newlines. | |
556 String separator = | |
557 utils.getText(namedExp.offset, childArg.offset - namedExp.offset); | |
558 String prefix = separator.contains(eol) ? "" : "$eol$indentNew"; | |
559 if (prefix.isEmpty) { | |
560 _addInsertEdit(namedExp.offset + 'child:'.length, ' <Widget>['); | |
561 _addRemoveEdit(rangeStartLength(childArg.offset - 2, 2)); | |
562 } else { | |
563 _addInsertEdit(listLoc, '<Widget>['); | |
564 } | |
565 String newChildArgSrc = childArgSrc.replaceAll( | |
566 new RegExp("^$indentOld", multiLine: true), "$indentNew"); | |
567 newChildArgSrc = "$prefix$newChildArgSrc,$eol$indentOld]"; | |
568 _addReplaceEdit(rangeNode(childArg), newChildArgSrc); | |
569 } | |
570 _addAssist(DartAssistKind.CONVERT_FLUTTER_CHILD, []); | |
571 } | |
572 | |
508 void _addProposal_convertIntoFinalField() { | 573 void _addProposal_convertIntoFinalField() { |
509 // Find the enclosing getter. | 574 // Find the enclosing getter. |
510 MethodDeclaration getter; | 575 MethodDeclaration getter; |
511 for (AstNode n = node; n != null; n = n.parent) { | 576 for (AstNode n = node; n != null; n = n.parent) { |
512 if (n is MethodDeclaration) { | 577 if (n is MethodDeclaration) { |
513 getter = n; | 578 getter = n; |
514 break; | 579 break; |
515 } | 580 } |
516 if (n is SimpleIdentifier || | 581 if (n is SimpleIdentifier || |
517 n is TypeAnnotation || | 582 n is TypeAnnotation || |
(...skipping 1786 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2304 newExpr.argumentList.arguments.firstWhere( | 2369 newExpr.argumentList.arguments.firstWhere( |
2305 (arg) => arg is NamedExpression && arg.name.label.name == 'child', | 2370 (arg) => arg is NamedExpression && arg.name.label.name == 'child', |
2306 orElse: () => null); | 2371 orElse: () => null); |
2307 | 2372 |
2308 InstanceCreationExpression _findChildWidget( | 2373 InstanceCreationExpression _findChildWidget( |
2309 InstanceCreationExpression newExpr) { | 2374 InstanceCreationExpression newExpr) { |
2310 NamedExpression child = _findChildArgument(newExpr); | 2375 NamedExpression child = _findChildArgument(newExpr); |
2311 return _getChildWidget(child); | 2376 return _getChildWidget(child); |
2312 } | 2377 } |
2313 | 2378 |
2314 InstanceCreationExpression _getChildWidget(NamedExpression child) { | 2379 InstanceCreationExpression _getChildWidget(NamedExpression child, |
2380 [bool strict = false]) { | |
2315 if (child?.expression is InstanceCreationExpression) { | 2381 if (child?.expression is InstanceCreationExpression) { |
2316 InstanceCreationExpression childNewExpr = child.expression; | 2382 InstanceCreationExpression childNewExpr = child.expression; |
2317 if (_isFlutterInstanceCreationExpression(childNewExpr)) { | 2383 if (_isFlutterInstanceCreationExpression(childNewExpr)) { |
2318 if (_findChildArgument(childNewExpr) != null) { | 2384 if (!strict || (_findChildArgument(childNewExpr) != null)) { |
2319 return childNewExpr; | 2385 return childNewExpr; |
2320 } | 2386 } |
2321 } | 2387 } |
2322 } | 2388 } |
2323 return null; | 2389 return null; |
2324 } | 2390 } |
2325 | 2391 |
2326 /** | 2392 /** |
2327 * Returns an existing or just added [LinkedEditGroup] with [groupId]. | 2393 * Returns an existing or just added [LinkedEditGroup] with [groupId]. |
2328 */ | 2394 */ |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2566 class _SimpleIdentifierRecursiveAstVisitor extends RecursiveAstVisitor { | 2632 class _SimpleIdentifierRecursiveAstVisitor extends RecursiveAstVisitor { |
2567 final _SimpleIdentifierVisitor visitor; | 2633 final _SimpleIdentifierVisitor visitor; |
2568 | 2634 |
2569 _SimpleIdentifierRecursiveAstVisitor(this.visitor); | 2635 _SimpleIdentifierRecursiveAstVisitor(this.visitor); |
2570 | 2636 |
2571 @override | 2637 @override |
2572 visitSimpleIdentifier(SimpleIdentifier node) { | 2638 visitSimpleIdentifier(SimpleIdentifier node) { |
2573 visitor(node); | 2639 visitor(node); |
2574 } | 2640 } |
2575 } | 2641 } |
OLD | NEW |