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

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

Issue 2701563003: Add assist to reparent Flutter widget (Closed)
Patch Set: Address review comments Created 3 years, 10 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 side-by-side diff with in-line comments
Download patch
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 28d6f8feda14c4d0240cf2356997d5c5f7e9cd65..acbb98ae2a067f167a19d14987fd641b3a6a6bab 100644
--- a/pkg/analysis_server/lib/src/services/correction/assist_internal.dart
+++ b/pkg/analysis_server/lib/src/services/correction/assist_internal.dart
@@ -36,6 +36,10 @@ typedef _SimpleIdentifierVisitor(SimpleIdentifier node);
* The computer for Dart assists.
*/
class AssistProcessor {
+ static const FLUTTER_WIDGET_NAME = "Widget";
+ static const FLUTTER_WIDGET_URI =
+ "package:flutter/src/widgets/framework.dart";
+
AnalysisContext analysisContext;
Source source;
@@ -133,6 +137,7 @@ class AssistProcessor {
_addProposal_joinVariableDeclaration_onAssignment();
_addProposal_joinVariableDeclaration_onDeclaration();
_addProposal_removeTypeAnnotation();
+ _addProposal_reparentFlutterWidget();
_addProposal_replaceConditionalWithIfElse();
_addProposal_replaceIfElseWithConditional();
_addProposal_splitAndCondition();
@@ -1595,6 +1600,66 @@ class AssistProcessor {
_addAssist(DartAssistKind.REMOVE_TYPE_ANNOTATION, []);
}
+ void _addProposal_reparentFlutterWidget() {
+ InstanceCreationExpression newExpr;
+ if (node is SimpleIdentifier) {
+ newExpr = node.getAncestor((node) => node is InstanceCreationExpression);
Brian Wilkerson 2017/02/16 23:23:20 I suspect Konstantin was suggesting that we not lo
scheglov 2017/02/16 23:59:22 Acknowledged.
+ var args =
+ node.getAncestor((node) => node == newExpr || node is ArgumentList);
+ if (args != newExpr) {
+ _coverageMarker();
+ return;
+ }
+ } else if (node is InstanceCreationExpression) {
+ newExpr = node;
+ }
+ if (newExpr == null) {
+ _coverageMarker();
+ return;
+ }
+ ClassElement classElement = newExpr.staticElement?.enclosingElement;
+ InterfaceType superType = classElement?.allSupertypes
+ ?.firstWhere((InterfaceType type) => FLUTTER_WIDGET_NAME == type.name);
+ if (superType == null) {
+ _coverageMarker();
+ return;
+ }
+ Uri uri = superType.element?.source?.uri;
+ if (uri.toString() != FLUTTER_WIDGET_URI) {
+ _coverageMarker();
+ return;
+ }
+ String newExprSrc = utils.getNodeText(newExpr);
+ SourceBuilder sb = new SourceBuilder(file, newExpr.offset);
+ sb.append('new ');
+ sb.startPosition('WIDGET');
+ sb.append('widget');
+ sb.endPosition();
+ sb.append('(');
+ if (newExprSrc.contains(eol)) {
+ int newlineIdx = newExprSrc.lastIndexOf(eol);
+ if (newlineIdx == newExprSrc.length - 1) {
+ newlineIdx -= 1;
+ }
+ String indentOld = utils.getLinePrefix(newExpr.offset + 1 + newlineIdx);
+ String indentNew = '$indentOld${utils.getIndent(1)}';
+ sb.append(eol);
+ sb.append(indentNew);
+ newExprSrc = newExprSrc.replaceAll(
+ new RegExp("^$indentOld", multiLine: true), "$indentNew");
+ newExprSrc += ",$eol$indentOld";
+ }
+ sb.startPosition('CHILD');
+ sb.append('child');
+ sb.endPosition();
+ sb.append(': ');
+ sb.append(newExprSrc);
+ sb.append(')');
+ exitPosition = _newPosition(sb.offset + sb.length);
+ _insertBuilder(sb, newExpr.length);
+ _addAssist(DartAssistKind.REPARENT_FLUTTER_WIDGET, []);
+ }
+
void _addProposal_replaceConditionalWithIfElse() {
ConditionalExpression conditional = null;
// may be on Statement with Conditional

Powered by Google App Engine
This is Rietveld 408576698