Skip to content

Commit 478aba8

Browse files
bwilkersonCommit Queue
authored andcommitted
Migrate flutter support
Change-Id: I52899ecc30b2db3e4990df523c2b0bbd19d46e95 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/395704 Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent d2c9d93 commit 478aba8

File tree

4 files changed

+96
-79
lines changed

4 files changed

+96
-79
lines changed

pkg/analysis_server/lib/src/services/flutter/class_description.dart

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/dart/ast/ast.dart';
6-
import 'package:analyzer/dart/element/element.dart';
6+
import 'package:analyzer/dart/element/element2.dart';
77
import 'package:analyzer/dart/element/type.dart';
88

99
/// Information about a class with nested properties.
@@ -18,15 +18,15 @@ import 'package:analyzer/dart/element/type.dart';
1818
///
1919
/// This class provides such "how to materialize" information.
2020
class ClassDescription {
21-
final ClassElement element;
22-
final ConstructorElement constructor;
21+
final ClassElement2 element;
22+
final ConstructorElement2 constructor;
2323

2424
ClassDescription(this.element, this.constructor);
2525
}
2626

2727
/// The lazy-fill registry of [ClassDescription].
2828
class ClassDescriptionRegistry {
29-
final Map<ClassElement, ClassDescription> _map = {};
29+
final Map<ClassElement2, ClassDescription> _map = {};
3030

3131
/// Flush all data, because there was a change to a file.
3232
void flush() {
@@ -35,8 +35,8 @@ class ClassDescriptionRegistry {
3535

3636
/// If we know how to materialize the [element], return [ClassDescription].
3737
/// Otherwise return `null`.
38-
ClassDescription? get(InterfaceElement element) {
39-
if (element is! ClassElement) {
38+
ClassDescription? get(InterfaceElement2 element) {
39+
if (element is! ClassElement2) {
4040
return null;
4141
}
4242

@@ -53,28 +53,28 @@ class ClassDescriptionRegistry {
5353
/// Return `true` if properties should be created for instances of [type].
5454
bool hasNestedProperties(DartType type) {
5555
if (type is InterfaceType) {
56-
return _isOptedInClass(type.element);
56+
return _isOptedInClass(type.element3);
5757
}
5858
return false;
5959
}
6060

61-
ClassDescription? _classDescription(ClassElement element) {
61+
ClassDescription? _classDescription(ClassElement2 element) {
6262
if (!_isOptedInClass(element)) return null;
6363

64-
var constructor = element.unnamedConstructor;
64+
var constructor = element.unnamedConstructor2;
6565
if (constructor == null) return null;
6666

67-
for (var parameter in constructor.parameters) {
68-
if (parameter.isRequired || parameter.hasRequired) {
67+
for (var parameter in constructor.formalParameters) {
68+
if (parameter.isRequired || parameter.metadata2.hasRequired) {
6969
return null;
7070
}
7171
}
7272

7373
return ClassDescription(element, constructor);
7474
}
7575

76-
bool _isOptedInClass(InterfaceElement element) {
77-
if (element is! ClassElement) {
76+
bool _isOptedInClass(InterfaceElement2 element) {
77+
if (element is! ClassElement2) {
7878
return false;
7979
}
8080

@@ -90,7 +90,7 @@ class ClassDescriptionRegistry {
9090
);
9191
}
9292

93-
static bool _isClass(ClassElement element, String uri, String name) {
94-
return element.name == name && element.library.source.uri.toString() == uri;
93+
static bool _isClass(ClassElement2 element, String uri, String name) {
94+
return element.name3 == name && element.library2.uri.toString() == uri;
9595
}
9696
}

pkg/analysis_server/lib/src/services/flutter/property.dart

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,24 @@ import 'package:analysis_server/src/services/flutter/class_description.dart';
77
import 'package:analyzer/dart/analysis/results.dart';
88
import 'package:analyzer/dart/ast/ast.dart';
99
import 'package:analyzer/dart/ast/token.dart';
10-
import 'package:analyzer/dart/element/element.dart';
10+
import 'package:analyzer/dart/element/element2.dart';
1111
import 'package:analyzer/source/source_range.dart';
1212
import 'package:analyzer/src/dart/analysis/session_helper.dart';
1313
import 'package:analyzer/src/dart/ast/extensions.dart';
1414
import 'package:analyzer/src/util/comment.dart';
1515
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
1616
import 'package:analyzer_plugin/utilities/change_builder/change_builder_dart.dart';
1717
import 'package:analyzer_plugin/utilities/range_factory.dart';
18+
import 'package:collection/collection.dart';
1819

19-
String? getFieldDocumentation(FieldElement field) {
20+
String? getFieldDocumentation(FieldElement2 field) {
2021
var rawComment = field.documentationComment;
2122
return getDartDocPlainText(rawComment);
2223
}
2324

24-
String? getParameterDocumentation(ParameterElement? parameter) {
25-
if (parameter is FieldFormalParameterElement) {
26-
var rawComment = parameter.field?.documentationComment;
25+
String? getParameterDocumentation(FormalParameterElement? parameter) {
26+
if (parameter is FieldFormalParameterElement2) {
27+
var rawComment = parameter.field2?.documentationComment;
2728
return getDartDocPlainText(rawComment);
2829
}
2930
return null;
@@ -64,7 +65,7 @@ class PropertyDescription {
6465
/// The parameter element in the object constructor that is actually
6566
/// invoked by [instanceCreation], or will be invoked when
6667
/// [classDescription] is materialized.
67-
final ParameterElement? parameterElement;
68+
final FormalParameterElement? parameterElement;
6869

6970
/// Optional nested properties.
7071
final List<PropertyDescription> children = [];
@@ -90,7 +91,7 @@ class PropertyDescription {
9091
String get name => protocolProperty.name;
9192

9293
/// This property has type `EdgeInsets`, add its nested properties.
93-
void addEdgeInsetsNestedProperties(ClassElement classEdgeInsets) {
94+
void addEdgeInsetsNestedProperties(ClassElement2 classEdgeInsets) {
9495
_edgeInsetsProperty = _EdgeInsetsProperty(classEdgeInsets, this)
9596
..addNested();
9697
}
@@ -105,15 +106,15 @@ class PropertyDescription {
105106

106107
var builder = ChangeBuilder(session: resolvedUnit.session);
107108

108-
InterfaceElement? enumElement;
109+
InterfaceElement2? enumElement;
109110
var enumValue = value.enumValue;
110111
if (enumValue != null) {
111112
var helper = AnalysisSessionHelper(resolvedUnit.session);
112-
enumElement = await helper.getClass(
113+
enumElement = await helper.getClass2(
113114
enumValue.libraryUri,
114115
enumValue.className,
115116
);
116-
enumElement ??= await helper.getEnum(
117+
enumElement ??= await helper.getEnum2(
117118
enumValue.libraryUri,
118119
enumValue.className,
119120
);
@@ -125,7 +126,7 @@ class PropertyDescription {
125126
if (expression != null) {
126127
builder.write(expression);
127128
} else if (enumElement != null && enumValue != null) {
128-
builder.writeReference(enumElement);
129+
builder.writeReference2(enumElement);
129130
builder.write('.');
130131
builder.write(enumValue.name);
131132
} else {
@@ -186,7 +187,7 @@ class PropertyDescription {
186187
if (parameterElement == null) {
187188
return;
188189
}
189-
var parameterName = parameterElement.name;
190+
var parameterName = parameterElement.name3!;
190191
var instanceCreation = this.instanceCreation;
191192
if (instanceCreation != null) {
192193
var argumentList = instanceCreation.argumentList;
@@ -240,7 +241,7 @@ class PropertyDescription {
240241
return;
241242
}
242243
parent._changeCode(builder, (builder) {
243-
builder.writeReference(classDescription.element);
244+
builder.writeReference2(classDescription.element);
244245
// TODO(scheglov): constructor name
245246
builder.write('(');
246247
builder.write(parameterName);
@@ -269,7 +270,7 @@ class PropertyDescription {
269270
builder.addReplacement(
270271
range.startEnd(parentCreation, parentCreation.constructorName),
271272
(builder) {
272-
builder.writeReference(virtualContainer.containerElement);
273+
builder.writeReference2(virtualContainer.containerElement);
273274
},
274275
);
275276

@@ -311,7 +312,7 @@ class PropertyDescription {
311312
});
312313
} else {
313314
builder.addInsertion(virtualContainer.widgetCreation.offset, (builder) {
314-
builder.writeReference(virtualContainer.containerElement);
315+
builder.writeReference2(virtualContainer.containerElement);
315316
builder.write('(');
316317

317318
builder.write(parameterName);
@@ -388,7 +389,7 @@ class PropertyDescription {
388389
///
389390
/// This class provides information necessary for such materialization.
390391
class VirtualContainerProperty {
391-
final ClassElement containerElement;
392+
final ClassElement2 containerElement;
392393
final InstanceCreationExpression widgetCreation;
393394

394395
/// The existing wrapper around the widget, with semantic that is a subset
@@ -417,13 +418,13 @@ class VirtualContainerProperty {
417418
///
418419
/// We try to generate nice looking code for `EdgeInsets` instances.
419420
class _EdgeInsetsProperty {
420-
final ClassElement classEdgeInsets;
421+
final ClassElement2 classEdgeInsets;
421422

422423
/// The property that has type `EdgeInsets`.
423424
final PropertyDescription property;
424425

425426
/// The constructor `EdgeInsets.only`.
426-
ConstructorElement? onlyConstructor;
427+
ConstructorElement2? onlyConstructor;
427428

428429
double? leftValue;
429430
double? topValue;
@@ -444,11 +445,11 @@ class _EdgeInsetsProperty {
444445
Expression? bottomExpression;
445446
var propertyExpression = property.valueExpression;
446447
if (propertyExpression is InstanceCreationExpression) {
447-
var constructor = propertyExpression.constructorName.staticElement;
448+
var constructor = propertyExpression.constructorName.element;
448449
if (constructor != null &&
449-
constructor.enclosingElement3 == classEdgeInsets) {
450+
constructor.enclosingElement2 == classEdgeInsets) {
450451
var arguments = propertyExpression.argumentList;
451-
var constructorName = constructor.name;
452+
var constructorName = constructor.name3;
452453
if (constructorName == 'all') {
453454
var expression = arguments.elementAtOrNull(0);
454455
leftExpression = expression;
@@ -485,7 +486,9 @@ class _EdgeInsetsProperty {
485486
}
486487
}
487488

488-
onlyConstructor = classEdgeInsets.getNamedConstructor('only');
489+
onlyConstructor = classEdgeInsets.constructors2.firstWhereOrNull(
490+
(e) => e.name3 == 'only',
491+
);
489492

490493
leftProperty = _addNestedProperty(
491494
name: 'left',
@@ -545,7 +548,7 @@ class _EdgeInsetsProperty {
545548
await builder.addDartFileEdit(property.resolvedUnit.path, (builder) {
546549
property._changeCode(builder, (builder) {
547550
if (leftCode == rightCode && topCode == bottomCode) {
548-
builder.writeReference(classEdgeInsets);
551+
builder.writeReference2(classEdgeInsets);
549552
if (leftCode == topCode) {
550553
builder.write('.all(');
551554
builder.write(leftCode);
@@ -568,7 +571,7 @@ class _EdgeInsetsProperty {
568571
builder.write(')');
569572
}
570573
} else {
571-
builder.writeReference(classEdgeInsets);
574+
builder.writeReference2(classEdgeInsets);
572575
builder.write('.only(');
573576
var needsComma = false;
574577
if (leftCode != '0') {
@@ -614,8 +617,8 @@ class _EdgeInsetsProperty {
614617
required Expression? expression,
615618
required double? value,
616619
}) {
617-
var parameter = onlyConstructor?.parameters.singleWhere(
618-
(p) => p.name == name,
620+
var parameter = onlyConstructor?.formalParameters.singleWhere(
621+
(p) => p.name3 == name,
619622
);
620623
var parameterDocumentation = getParameterDocumentation(parameter);
621624
var nested = PropertyDescription(

0 commit comments

Comments
 (0)