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

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

Issue 1009833002: Quick Assist for creating a constructor for selected final fields. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rework into a Quick Fix. Created 5 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 side-by-side diff with in-line comments
Download patch
Index: pkg/analysis_server/lib/src/services/correction/fix_internal.dart
diff --git a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
index 9cf28754a4cddc05e9eb74592d67d0561a07fb89..5ce50a57cdd169fb2e811c37441de9b6b71ea04e 100644
--- a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
+++ b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
@@ -199,6 +199,9 @@ class FixProcessor {
_addFix_createClass();
_addFix_undefinedClass_useSimilar();
}
+ if (errorCode == StaticWarningCode.FINAL_NOT_INITIALIZED) {
+ _addFix_createConstructor_forUninitializedFinalFields();
+ }
if (errorCode == StaticWarningCode.UNDEFINED_IDENTIFIER) {
bool isAsync = _addFix_addAsync();
if (!isAsync) {
@@ -341,6 +344,49 @@ class FixProcessor {
}
}
+ /**
+ * Here we handle cases when there are no constructors in a class, and the
+ * class has uninitialized final fields.
+ */
+ void _addFix_createConstructor_forUninitializedFinalFields() {
+ if (node is! SimpleIdentifier || node.parent is! VariableDeclaration) {
+ return;
+ }
+ ClassDeclaration classDeclaration =
+ node.getAncestor((node) => node is ClassDeclaration);
+ // prepare names of uninitialized final fields
+ List<String> fieldNames = <String>[];
+ for (ClassMember member in classDeclaration.members) {
+ if (member is FieldDeclaration) {
+ VariableDeclarationList variableList = member.fields;
+ if (variableList.isFinal) {
+ fieldNames.addAll(variableList.variables
+ .where((v) => v.initializer == null)
+ .map((v) => v.name.name));
+ }
+ }
+ }
+ // prepare location for a new constructor
+ _ConstructorLocation targetLocation =
+ _prepareNewConstructorLocation(classDeclaration);
+ // build constructor source
+ SourceBuilder sb = new SourceBuilder(file, targetLocation.offset);
+ {
+ String indent = ' ';
+ sb.append(targetLocation.prefix);
+ sb.append(indent);
+ sb.append(classDeclaration.name.name);
+ sb.append('(');
+ sb.append(fieldNames.map((name) => 'this.$name').join(', '));
+ sb.append(');');
+ sb.append(targetLocation.suffix);
+ }
+ // insert source
+ _insertBuilder(sb, unitElement);
+ // add proposal
+ _addFix(FixKind.CREATE_CONSTRUCTOR_FOR_FINAL_FIELDS, []);
+ }
+
void _addFix_createConstructor_insteadOfSyntheticDefault() {
TypeName typeName = null;
ConstructorName constructorName = null;

Powered by Google App Engine
This is Rietveld 408576698