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

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

Issue 2144763002: Issue 26249. Quick Assist to convert getters into final fields. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Don't suggest when there is the corresponding setter in the class. Created 4 years, 5 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 f58469d036c2709916a1b8704125cde9e7601300..6059cbb60fa6ce6ea8084431f31999caedb04bc7 100644
--- a/pkg/analysis_server/lib/src/services/correction/assist_internal.dart
+++ b/pkg/analysis_server/lib/src/services/correction/assist_internal.dart
@@ -97,6 +97,7 @@ class AssistProcessor {
_addProposal_addTypeAnnotation_SimpleFormalParameter();
_addProposal_addTypeAnnotation_VariableDeclaration();
_addProposal_assignToLocalVariable();
+ _addProposal_convertIntoFinalField();
_addProposal_convertDocumentationIntoBlock();
_addProposal_convertDocumentationIntoLine();
_addProposal_convertToBlockFunctionBody();
@@ -481,6 +482,69 @@ class AssistProcessor {
_addAssist(DartAssistKind.CONVERT_DOCUMENTATION_INTO_LINE, []);
}
+ void _addProposal_convertIntoFinalField() {
+ // Find the enclosing getter.
+ MethodDeclaration getter;
+ for (AstNode n = node; n != null; n = n.parent) {
+ if (n is MethodDeclaration) {
+ getter = n;
+ break;
+ }
+ if (n is SimpleIdentifier || n is TypeName || n is TypeArgumentList) {
+ continue;
+ }
+ break;
+ }
+ if (getter == null || !getter.isGetter) {
+ return;
+ }
+ // Check that there is no corresponding setter.
+ {
+ ExecutableElement element = getter.element;
+ if (element == null) {
+ return;
+ }
+ Element enclosing = element.enclosingElement;
+ if (enclosing is ClassElement) {
+ if (enclosing.getSetter(element.name) != null) {
+ return;
+ }
+ }
+ }
+ // Try to find the returned expression.
+ Expression expression;
+ {
+ FunctionBody body = getter.body;
+ if (body is ExpressionFunctionBody) {
+ expression = body.expression;
+ } else if (body is BlockFunctionBody) {
+ List<Statement> statements = body.block.statements;
+ if (statements.length == 1) {
+ Statement statement = statements.first;
+ if (statement is ReturnStatement) {
+ expression = statement.expression;
+ }
+ }
+ }
+ }
+ // Use the returned expression as the field initializer.
+ if (expression != null) {
+ AstNode beginNodeToReplace = getter.name;
+ String code = 'final';
+ if (getter.returnType != null) {
+ beginNodeToReplace = getter.returnType;
+ code += ' ' + _getNodeText(getter.returnType);
+ }
+ code += ' ' + _getNodeText(getter.name);
+ if (expression is! NullLiteral) {
+ code += ' = ' + _getNodeText(expression);
+ }
+ code += ';';
+ _addReplaceEdit(rangeStartEnd(beginNodeToReplace, getter), code);
+ _addAssist(DartAssistKind.CONVERT_INTO_FINAL_FIELD, []);
+ }
+ }
+
void _addProposal_convertToBlockFunctionBody() {
FunctionBody body = getEnclosingFunctionBody();
// prepare expression body

Powered by Google App Engine
This is Rietveld 408576698