Chromium Code Reviews| Index: pkg/analysis_server/lib/src/services/correction/assist_internal.dart |
| diff --git a/pkg/analysis_server/lib/src/services/correction/assist_internal.dart b/pkg/analysis_server/lib/src/services/correction/assist_internal.dart |
| index c343b17c1352041c672b0917a48610e11227a0d2..b5aaf0b338c12c8ca70de88bdae4a436d93a3dfd 100644 |
| --- a/pkg/analysis_server/lib/src/services/correction/assist_internal.dart |
| +++ b/pkg/analysis_server/lib/src/services/correction/assist_internal.dart |
| @@ -121,6 +121,7 @@ class AssistProcessor { |
| _addProposal_convertDocumentationIntoLine(); |
| _addProposal_convertToBlockFunctionBody(); |
| _addProposal_convertToExpressionFunctionBody(); |
| + _addProposal_convertFlutterChild(); |
| _addProposal_convertToForIndexLoop(); |
| _addProposal_convertToIsNot_onIs(); |
| _addProposal_convertToIsNot_onNot(); |
| @@ -505,6 +506,70 @@ class AssistProcessor { |
| _addAssist(DartAssistKind.CONVERT_DOCUMENTATION_INTO_LINE, []); |
| } |
| + void _addProposal_convertFlutterChild() { |
| + NamedExpression namedExp; |
| + // Allow assist to activate from either the new-expr or the child: arg. |
| + if (node is SimpleIdentifier && |
| + node.parent is Label && |
| + node.parent.parent is NamedExpression) { |
| + namedExp = node.parent.parent as NamedExpression; |
| + if ((node as SimpleIdentifier).name != 'child' || |
| + namedExp.expression == null) { |
| + return; |
| + } |
| + InstanceCreationExpression newExpr = namedExp.parent?.parent; |
|
Brian Wilkerson
2017/03/15 21:54:23
The parent might not be an InstanceCreationExpress
|
| + if (newExpr == null || !_isFlutterInstanceCreationExpression(newExpr)) { |
| + return; |
| + } |
| + } else { |
| + InstanceCreationExpression newExpr = _identifyNewExpression(); |
| + if (newExpr == null || !_isFlutterInstanceCreationExpression(newExpr)) { |
| + _coverageMarker(); |
| + return; |
| + } |
| + namedExp = _findChildArgument(newExpr); |
| + if (namedExp == null || namedExp.expression == null) { |
| + _coverageMarker(); |
| + return; |
| + } |
| + } |
| + InstanceCreationExpression childArg = _getChildWidget(namedExp, false); |
| + if (childArg == null) { |
| + _coverageMarker(); |
| + return; |
| + } |
| + int childLoc = namedExp.offset + 'child'.length; |
| + _addInsertEdit(childLoc, 'ren'); |
| + int listLoc = childArg.offset; |
| + String childArgSrc = utils.getNodeText(childArg); |
| + if (!childArgSrc.contains(eol)) { |
| + _addInsertEdit(listLoc, '<Widget>['); |
| + _addInsertEdit(listLoc + childArg.length, ']'); |
| + } else { |
| + int newlineLoc = childArgSrc.lastIndexOf(eol); |
| + if (newlineLoc == childArgSrc.length) { |
| + newlineLoc -= 1; |
| + } |
| + String indentOld = utils.getLinePrefix(childArg.offset + 1 + newlineLoc); |
| + String indentNew = '$indentOld${utils.getIndent(1)}'; |
| + // The separator includes 'child:' but that has no newlines. |
| + String separator = |
| + utils.getText(namedExp.offset, childArg.offset - namedExp.offset); |
| + String prefix = separator.contains(eol) ? "" : "$eol$indentNew"; |
| + if (prefix.isEmpty) { |
| + _addInsertEdit(namedExp.offset + 'child:'.length, ' <Widget>['); |
| + _addRemoveEdit(rangeStartLength(childArg.offset - 2, 2)); |
| + } else { |
| + _addInsertEdit(listLoc, '<Widget>['); |
| + } |
| + String newChildArgSrc = childArgSrc.replaceAll( |
| + new RegExp("^$indentOld", multiLine: true), "$indentNew"); |
| + newChildArgSrc = "$prefix$newChildArgSrc,$eol$indentOld]"; |
| + _addReplaceEdit(rangeNode(childArg), newChildArgSrc); |
| + } |
| + _addAssist(DartAssistKind.CONVERT_FLUTTER_CHILD, []); |
| + } |
| + |
| void _addProposal_convertIntoFinalField() { |
| // Find the enclosing getter. |
| MethodDeclaration getter; |
| @@ -2311,11 +2376,12 @@ class AssistProcessor { |
| return _getChildWidget(child); |
| } |
| - InstanceCreationExpression _getChildWidget(NamedExpression child) { |
| + InstanceCreationExpression _getChildWidget(NamedExpression child, |
| + [bool strict = false]) { |
| if (child?.expression is InstanceCreationExpression) { |
| InstanceCreationExpression childNewExpr = child.expression; |
| if (_isFlutterInstanceCreationExpression(childNewExpr)) { |
| - if (_findChildArgument(childNewExpr) != null) { |
| + if (!strict || (_findChildArgument(childNewExpr) != null)) { |
| return childNewExpr; |
| } |
| } |