Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1227)

Side by Side Diff: pkg/analysis_server/lib/src/services/correction/assist_internal.dart

Issue 2749283004: Add assist to convert child: to children: in Flutter new-exprs (Closed)
Patch Set: Fail fast for named args not in instance creation exprs Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 if (namedExp.parent?.parent is! InstanceCreationExpression) {
521 return;
522 }
523 InstanceCreationExpression newExpr = namedExp.parent.parent;
524 if (newExpr == null || !_isFlutterInstanceCreationExpression(newExpr)) {
525 return;
526 }
527 } else {
528 InstanceCreationExpression newExpr = _identifyNewExpression();
529 if (newExpr == null || !_isFlutterInstanceCreationExpression(newExpr)) {
530 _coverageMarker();
531 return;
532 }
533 namedExp = _findChildArgument(newExpr);
534 if (namedExp == null || namedExp.expression == null) {
535 _coverageMarker();
536 return;
537 }
538 }
539 InstanceCreationExpression childArg = _getChildWidget(namedExp, false);
540 if (childArg == null) {
541 _coverageMarker();
542 return;
543 }
544 int childLoc = namedExp.offset + 'child'.length;
545 _addInsertEdit(childLoc, 'ren');
546 int listLoc = childArg.offset;
547 String childArgSrc = utils.getNodeText(childArg);
548 if (!childArgSrc.contains(eol)) {
549 _addInsertEdit(listLoc, '<Widget>[');
550 _addInsertEdit(listLoc + childArg.length, ']');
551 } else {
552 int newlineLoc = childArgSrc.lastIndexOf(eol);
553 if (newlineLoc == childArgSrc.length) {
554 newlineLoc -= 1;
555 }
556 String indentOld = utils.getLinePrefix(childArg.offset + 1 + newlineLoc);
557 String indentNew = '$indentOld${utils.getIndent(1)}';
558 // The separator includes 'child:' but that has no newlines.
559 String separator =
560 utils.getText(namedExp.offset, childArg.offset - namedExp.offset);
561 String prefix = separator.contains(eol) ? "" : "$eol$indentNew";
562 if (prefix.isEmpty) {
563 _addInsertEdit(namedExp.offset + 'child:'.length, ' <Widget>[');
564 _addRemoveEdit(rangeStartLength(childArg.offset - 2, 2));
565 } else {
566 _addInsertEdit(listLoc, '<Widget>[');
567 }
568 String newChildArgSrc = childArgSrc.replaceAll(
569 new RegExp("^$indentOld", multiLine: true), "$indentNew");
570 newChildArgSrc = "$prefix$newChildArgSrc,$eol$indentOld]";
571 _addReplaceEdit(rangeNode(childArg), newChildArgSrc);
572 }
573 _addAssist(DartAssistKind.CONVERT_FLUTTER_CHILD, []);
574 }
575
508 void _addProposal_convertIntoFinalField() { 576 void _addProposal_convertIntoFinalField() {
509 // Find the enclosing getter. 577 // Find the enclosing getter.
510 MethodDeclaration getter; 578 MethodDeclaration getter;
511 for (AstNode n = node; n != null; n = n.parent) { 579 for (AstNode n = node; n != null; n = n.parent) {
512 if (n is MethodDeclaration) { 580 if (n is MethodDeclaration) {
513 getter = n; 581 getter = n;
514 break; 582 break;
515 } 583 }
516 if (n is SimpleIdentifier || 584 if (n is SimpleIdentifier ||
517 n is TypeAnnotation || 585 n is TypeAnnotation ||
(...skipping 1786 matching lines...) Expand 10 before | Expand all | Expand 10 after
2304 newExpr.argumentList.arguments.firstWhere( 2372 newExpr.argumentList.arguments.firstWhere(
2305 (arg) => arg is NamedExpression && arg.name.label.name == 'child', 2373 (arg) => arg is NamedExpression && arg.name.label.name == 'child',
2306 orElse: () => null); 2374 orElse: () => null);
2307 2375
2308 InstanceCreationExpression _findChildWidget( 2376 InstanceCreationExpression _findChildWidget(
2309 InstanceCreationExpression newExpr) { 2377 InstanceCreationExpression newExpr) {
2310 NamedExpression child = _findChildArgument(newExpr); 2378 NamedExpression child = _findChildArgument(newExpr);
2311 return _getChildWidget(child); 2379 return _getChildWidget(child);
2312 } 2380 }
2313 2381
2314 InstanceCreationExpression _getChildWidget(NamedExpression child) { 2382 InstanceCreationExpression _getChildWidget(NamedExpression child,
2383 [bool strict = false]) {
2315 if (child?.expression is InstanceCreationExpression) { 2384 if (child?.expression is InstanceCreationExpression) {
2316 InstanceCreationExpression childNewExpr = child.expression; 2385 InstanceCreationExpression childNewExpr = child.expression;
2317 if (_isFlutterInstanceCreationExpression(childNewExpr)) { 2386 if (_isFlutterInstanceCreationExpression(childNewExpr)) {
2318 if (_findChildArgument(childNewExpr) != null) { 2387 if (!strict || (_findChildArgument(childNewExpr) != null)) {
2319 return childNewExpr; 2388 return childNewExpr;
2320 } 2389 }
2321 } 2390 }
2322 } 2391 }
2323 return null; 2392 return null;
2324 } 2393 }
2325 2394
2326 /** 2395 /**
2327 * Returns an existing or just added [LinkedEditGroup] with [groupId]. 2396 * Returns an existing or just added [LinkedEditGroup] with [groupId].
2328 */ 2397 */
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
2566 class _SimpleIdentifierRecursiveAstVisitor extends RecursiveAstVisitor { 2635 class _SimpleIdentifierRecursiveAstVisitor extends RecursiveAstVisitor {
2567 final _SimpleIdentifierVisitor visitor; 2636 final _SimpleIdentifierVisitor visitor;
2568 2637
2569 _SimpleIdentifierRecursiveAstVisitor(this.visitor); 2638 _SimpleIdentifierRecursiveAstVisitor(this.visitor);
2570 2639
2571 @override 2640 @override
2572 visitSimpleIdentifier(SimpleIdentifier node) { 2641 visitSimpleIdentifier(SimpleIdentifier node) {
2573 visitor(node); 2642 visitor(node);
2574 } 2643 }
2575 } 2644 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698