@@ -98,83 +98,112 @@ abstract class BaseProcessor {
98
98
return null ;
99
99
}
100
100
101
- var constructorInvocation ;
101
+ var constructorName ;
102
102
var hasTypeArgs = false ;
103
103
if (type.isDartCoreBool) {
104
- constructorInvocation = 'DiagnosticsProperty<bool>' ;
104
+ constructorName = 'DiagnosticsProperty<bool>' ;
105
105
} else if (type.isDartCoreInt) {
106
- constructorInvocation = 'IntProperty' ;
106
+ constructorName = 'IntProperty' ;
107
107
} else if (type.isDartCoreDouble) {
108
- constructorInvocation = 'DoubleProperty' ;
108
+ constructorName = 'DoubleProperty' ;
109
109
} else if (type.isDartCoreString) {
110
- constructorInvocation = 'StringProperty' ;
110
+ constructorName = 'StringProperty' ;
111
111
} else if (isEnum (type)) {
112
- constructorInvocation = 'EnumProperty' ;
112
+ constructorName = 'EnumProperty' ;
113
113
} else if (isIterable (type)) {
114
- constructorInvocation = 'IterableProperty' ;
114
+ constructorName = 'IterableProperty' ;
115
115
hasTypeArgs = true ;
116
116
} else if (flutter.isColor (type)) {
117
- constructorInvocation = 'ColorProperty' ;
117
+ constructorName = 'ColorProperty' ;
118
118
} else if (flutter.isMatrix4 (type)) {
119
- constructorInvocation = 'TransformProperty' ;
119
+ constructorName = 'TransformProperty' ;
120
120
}
121
121
122
- if (constructorInvocation == null ) {
122
+ if (constructorName == null ) {
123
123
return null ;
124
124
}
125
125
126
- ClassDeclaration classDeclaration =
127
- parent.thisOrAncestorOfType <ClassDeclaration >();
126
+ void writePropertyReference (
127
+ DartEditBuilder builder, {
128
+ @required String prefix,
129
+ @required String builderName,
130
+ }) {
131
+ builder.write ("$prefix $builderName .add($constructorName " );
132
+ if (hasTypeArgs) {
133
+ builder.write ('<' );
134
+ builder.writeTypes ((type as InterfaceType ).typeArguments);
135
+ builder.write ('>' );
136
+ }
137
+ builder.writeln ("('${name .name }', ${name .name }));" );
138
+ }
139
+
140
+ final classDeclaration = parent.thisOrAncestorOfType <ClassDeclaration >();
128
141
final debugFillProperties =
129
142
classDeclaration.getMethod ('debugFillProperties' );
130
- if (debugFillProperties != null ) {
131
- final body = debugFillProperties.body;
132
- if (body is BlockFunctionBody ) {
133
- BlockFunctionBody functionBody = body;
134
-
135
- var offset;
136
- var prefix;
137
- if (functionBody.block.statements.isEmpty) {
138
- offset = functionBody.block.leftBracket.offset;
139
- prefix = utils.getLinePrefix (offset) + utils.getIndent (1 );
140
- } else {
141
- offset = functionBody.block.statements.last.endToken.offset;
142
- prefix = utils.getLinePrefix (offset);
143
- }
143
+ if (debugFillProperties == null ) {
144
+ final insertOffset =
145
+ utils.prepareNewMethodLocation (classDeclaration).offset;
146
+ final changeBuilder = _newDartChangeBuilder ();
147
+ await changeBuilder.addFileEdit (file, (DartFileEditBuilder builder) {
148
+ builder.addInsertion (utils.getLineNext (insertOffset),
149
+ (DartEditBuilder builder) {
150
+ final declPrefix =
151
+ utils.getLinePrefix (classDeclaration.offset) + utils.getIndent (1 );
152
+ final bodyPrefix = declPrefix + utils.getIndent (1 );
153
+
154
+ builder.writeln ('$declPrefix @override' );
155
+ builder.writeln (
156
+ '${declPrefix }void debugFillProperties(DiagnosticPropertiesBuilder properties) {' );
157
+ builder
158
+ .writeln ('${bodyPrefix }super.debugFillProperties(properties);' );
159
+ writePropertyReference (builder,
160
+ prefix: bodyPrefix, builderName: 'properties' );
161
+ builder.writeln ('$declPrefix }' );
162
+ });
163
+ });
164
+ return changeBuilder;
165
+ }
166
+
167
+ final body = debugFillProperties.body;
168
+ if (body is BlockFunctionBody ) {
169
+ BlockFunctionBody functionBody = body;
170
+
171
+ var offset;
172
+ var prefix;
173
+ if (functionBody.block.statements.isEmpty) {
174
+ offset = functionBody.block.leftBracket.offset;
175
+ prefix = utils.getLinePrefix (offset) + utils.getIndent (1 );
176
+ } else {
177
+ offset = functionBody.block.statements.last.endToken.offset;
178
+ prefix = utils.getLinePrefix (offset);
179
+ }
144
180
145
- var parameters = debugFillProperties.parameters.parameters;
146
- var propertiesBuilderName;
147
- for (var parameter in parameters) {
148
- if (parameter is SimpleFormalParameter ) {
149
- final type = parameter.type;
150
- if (type is TypeName ) {
151
- if (type.name.name == 'DiagnosticPropertiesBuilder' ) {
152
- propertiesBuilderName = parameter.identifier.name;
153
- break ;
154
- }
181
+ var parameters = debugFillProperties.parameters.parameters;
182
+ var propertiesBuilderName;
183
+ for (var parameter in parameters) {
184
+ if (parameter is SimpleFormalParameter ) {
185
+ final type = parameter.type;
186
+ if (type is TypeName ) {
187
+ if (type.name.name == 'DiagnosticPropertiesBuilder' ) {
188
+ propertiesBuilderName = parameter.identifier.name;
189
+ break ;
155
190
}
156
191
}
157
192
}
158
- if (propertiesBuilderName == null ) {
159
- return null ;
160
- }
193
+ }
194
+ if (propertiesBuilderName == null ) {
195
+ return null ;
196
+ }
161
197
162
- final changeBuilder = _newDartChangeBuilder ();
163
- await changeBuilder.addFileEdit (file, (DartFileEditBuilder builder) {
164
- builder.addInsertion (utils.getLineNext (offset),
165
- (DartEditBuilder builder) {
166
- builder.write (
167
- "$prefix $propertiesBuilderName .add($constructorInvocation " );
168
- if (hasTypeArgs) {
169
- builder.write ('<' );
170
- builder.writeTypes ((type as InterfaceType ).typeArguments);
171
- builder.write ('>' );
172
- }
173
- builder.write ("('${name .name }', ${name .name }));$eol " );
174
- });
198
+ final changeBuilder = _newDartChangeBuilder ();
199
+ await changeBuilder.addFileEdit (file, (DartFileEditBuilder builder) {
200
+ builder.addInsertion (utils.getLineNext (offset),
201
+ (DartEditBuilder builder) {
202
+ writePropertyReference (builder,
203
+ prefix: prefix, builderName: propertiesBuilderName);
175
204
});
176
- return changeBuilder ;
177
- }
205
+ }) ;
206
+ return changeBuilder;
178
207
}
179
208
180
209
return null ;
0 commit comments