Skip to content

Commit f24e6f7

Browse files
bwilkersonCommit Queue
authored andcommitted
Migrate extract_widget
Change-Id: I2773545a72e1937cdafc085f3b1e76fbc049f173 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/397104 Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent a33e3f2 commit f24e6f7

File tree

3 files changed

+55
-39
lines changed

3 files changed

+55
-39
lines changed

pkg/analysis_server/analyzer_use_new_elements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ lib/src/services/kythe/kythe_visitors.dart
5050
lib/src/services/refactoring/agnostic/change_method_signature.dart
5151
lib/src/services/refactoring/framework/formal_parameter.dart
5252
lib/src/services/refactoring/legacy/extract_method.dart
53-
lib/src/services/refactoring/legacy/extract_widget.dart
5453
lib/src/services/refactoring/legacy/inline_local.dart
5554
lib/src/services/refactoring/legacy/move_file.dart
5655
lib/src/services/refactoring/legacy/refactoring.dart

pkg/analysis_server/lib/src/services/refactoring/legacy/extract_widget.dart

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,16 @@
44

55
import 'package:analysis_server/src/protocol_server.dart' hide Element;
66
import 'package:analysis_server/src/services/correction/status.dart';
7-
import 'package:analysis_server/src/services/correction/util.dart';
87
import 'package:analysis_server/src/services/refactoring/legacy/naming_conventions.dart';
98
import 'package:analysis_server/src/services/refactoring/legacy/refactoring.dart';
109
import 'package:analysis_server/src/services/refactoring/legacy/refactoring_internal.dart';
11-
import 'package:analysis_server/src/services/search/element_visitors.dart';
1210
import 'package:analysis_server/src/services/search/search_engine.dart';
1311
import 'package:analysis_server_plugin/edit/correction_utils.dart';
1412
import 'package:analyzer/dart/analysis/features.dart';
1513
import 'package:analyzer/dart/analysis/results.dart';
1614
import 'package:analyzer/dart/ast/ast.dart';
1715
import 'package:analyzer/dart/ast/visitor.dart';
18-
import 'package:analyzer/dart/element/element.dart';
16+
import 'package:analyzer/dart/element/element2.dart';
1917
import 'package:analyzer/dart/element/nullability_suffix.dart';
2018
import 'package:analyzer/dart/element/type.dart';
2119
import 'package:analyzer/source/source_range.dart';
@@ -39,11 +37,11 @@ class ExtractWidgetRefactoringImpl extends RefactoringImpl
3937

4038
late CorrectionUtils utils;
4139

42-
ClassElement? classBuildContext;
43-
ClassElement? classKey;
44-
ClassElement? classStatelessWidget;
45-
ClassElement? classWidget;
46-
PropertyAccessorElement? accessorRequired;
40+
ClassElement2? classBuildContext;
41+
ClassElement2? classKey;
42+
ClassElement2? classStatelessWidget;
43+
ClassElement2? classWidget;
44+
PropertyAccessorElement2? accessorRequired;
4745

4846
@override
4947
late String name;
@@ -52,7 +50,7 @@ class ExtractWidgetRefactoringImpl extends RefactoringImpl
5250
ClassDeclaration? _enclosingClassNode;
5351

5452
/// If [offset] is in a class, the element of this class, `null` otherwise.
55-
ClassElement? _enclosingClassElement;
53+
ClassElement2? _enclosingClassElement;
5654

5755
/// The [CompilationUnitMember] that encloses the [offset].
5856
CompilationUnitMember? _enclosingUnitMember;
@@ -128,16 +126,16 @@ class ExtractWidgetRefactoringImpl extends RefactoringImpl
128126

129127
// Check for duplicate declarations.
130128
if (!result.hasFatalError) {
131-
visitLibraryTopLevelElements(resolveResult.libraryElement, (element) {
132-
if (hasDisplayName(element, name)) {
129+
for (var element in resolveResult.libraryElement2.children2) {
130+
if (element.displayName == name) {
133131
var message = format(
134132
"Library already declares {0} with name '{1}'.",
135-
getElementKindName(element),
133+
element.kind.displayName,
136134
name,
137135
);
138-
result.addError(message, newLocation_fromElement(element));
136+
result.addError(message, newLocation_fromElement2(element));
139137
}
140-
});
138+
}
141139
}
142140

143141
return result;
@@ -191,7 +189,7 @@ class ExtractWidgetRefactoringImpl extends RefactoringImpl
191189

192190
// Find the enclosing class.
193191
_enclosingClassNode = node?.thisOrAncestorOfType<ClassDeclaration>();
194-
_enclosingClassElement = _enclosingClassNode?.declaredElement;
192+
_enclosingClassElement = _enclosingClassNode?.declaredFragment?.element;
195193

196194
// `new MyWidget(...)`
197195
var newExpression = node.findInstanceCreationExpression;
@@ -249,19 +247,19 @@ class ExtractWidgetRefactoringImpl extends RefactoringImpl
249247
Future<RefactoringStatus> _initializeClasses() async {
250248
var result = RefactoringStatus();
251249

252-
Future<ClassElement?> getClass(String name) async {
253-
var element = await sessionHelper.getFlutterClass(name);
250+
Future<ClassElement2?> getClass(String name) async {
251+
var element = await sessionHelper.getFlutterClass2(name);
254252
if (element == null) {
255253
result.addFatalError("Unable to find '$name' in $widgetsUri");
256254
}
257255
return element;
258256
}
259257

260-
Future<PropertyAccessorElement?> getAccessor(
258+
Future<PropertyAccessorElement2?> getAccessor(
261259
String uri,
262260
String name,
263261
) async {
264-
var element = await sessionHelper.getTopLevelPropertyAccessor(uri, name);
262+
var element = await sessionHelper.getTopLevelPropertyAccessor2(uri, name);
265263
if (element == null) {
266264
result.addFatalError("Unable to find 'required' in $uri");
267265
}
@@ -319,9 +317,9 @@ class ExtractWidgetRefactoringImpl extends RefactoringImpl
319317
for (var parameter in parameterList.parameters) {
320318
parameter = parameter.notDefault;
321319
if (parameter is NormalFormalParameter) {
322-
var element = parameter.declaredElement!;
320+
var element = parameter.declaredFragment!.element;
323321
_parameters.add(
324-
_Parameter(element.name, element.type, isMethodParameter: true),
322+
_Parameter(element.name3!, element.type, isMethodParameter: true),
325323
);
326324
}
327325
}
@@ -380,7 +378,9 @@ class ExtractWidgetRefactoringImpl extends RefactoringImpl
380378
/// Replace invocations of the [_method] with instantiations of the new
381379
/// widget class.
382380
void _replaceInvocationsWithInstantiations(DartFileEditBuilder builder) {
383-
var collector = _MethodInvocationsCollector(_method!.declaredElement!);
381+
var collector = _MethodInvocationsCollector(
382+
_method!.declaredFragment!.element,
383+
);
384384
_enclosingClassNode!.accept(collector);
385385
for (var invocation in collector.invocations) {
386386
List<Expression> arguments = invocation.argumentList.arguments;
@@ -580,14 +580,14 @@ class ExtractWidgetRefactoringImpl extends RefactoringImpl
580580
}
581581

582582
class _MethodInvocationsCollector extends RecursiveAstVisitor<void> {
583-
final ExecutableElement methodElement;
583+
final ExecutableElement2 methodElement;
584584
final List<MethodInvocation> invocations = [];
585585

586586
_MethodInvocationsCollector(this.methodElement);
587587

588588
@override
589589
void visitMethodInvocation(MethodInvocation node) {
590-
if (node.methodName.staticElement == methodElement) {
590+
if (node.methodName.element == methodElement) {
591591
invocations.add(node);
592592
} else {
593593
super.visitMethodInvocation(node);
@@ -613,42 +613,42 @@ class _Parameter {
613613
}
614614

615615
class _ParametersCollector extends RecursiveAstVisitor<void> {
616-
final InterfaceElement? enclosingClass;
616+
final InterfaceElement2? enclosingClass;
617617
final SourceRange expressionRange;
618618

619619
final RefactoringStatus status = RefactoringStatus();
620-
final Set<Element> uniqueElements = <Element>{};
620+
final Set<Element2> uniqueElements = <Element2>{};
621621
final List<_Parameter> parameters = [];
622622

623-
List<InterfaceElement>? enclosingClasses;
623+
List<InterfaceElement2>? enclosingClasses;
624624

625625
_ParametersCollector(this.enclosingClass, this.expressionRange);
626626

627627
@override
628628
void visitSimpleIdentifier(SimpleIdentifier node) {
629-
var element = node.writeOrReadElement;
629+
var element = node.writeOrReadElement2;
630630
if (element == null) {
631631
return;
632632
}
633633
var elementName = element.displayName;
634634

635635
DartType? type;
636-
if (element is MethodElement) {
636+
if (element is MethodElement2) {
637637
if (_isMemberOfEnclosingClass(element)) {
638638
status.addError(
639639
'Reference to an enclosing class method cannot be extracted.',
640640
);
641641
}
642-
} else if (element is LocalVariableElement) {
643-
if (!expressionRange.contains(element.nameOffset)) {
642+
} else if (element is LocalVariableElement2) {
643+
if (!expressionRange.contains(element.firstFragment.nameOffset)) {
644644
if (node.inSetterContext()) {
645645
status.addError("Write to '$elementName' cannot be extracted.");
646646
} else {
647647
type = element.type;
648648
}
649649
}
650-
} else if (element is PropertyAccessorElement) {
651-
var field = element.variable2;
650+
} else if (element is PropertyAccessorElement2) {
651+
var field = element.variable3;
652652
if (field == null) {
653653
return;
654654
}
@@ -657,7 +657,7 @@ class _ParametersCollector extends RecursiveAstVisitor<void> {
657657
status.addError("Write to '$elementName' cannot be extracted.");
658658
} else {
659659
type = field.type;
660-
element = element.declaration;
660+
element = element.baseElement;
661661
}
662662
}
663663
}
@@ -670,15 +670,15 @@ class _ParametersCollector extends RecursiveAstVisitor<void> {
670670

671671
/// Return `true` if the given [element] is a member of the [enclosingClass]
672672
/// or one of its supertypes, interfaces, or mixins.
673-
bool _isMemberOfEnclosingClass(Element element) {
673+
bool _isMemberOfEnclosingClass(Element2 element) {
674674
var enclosingClass = this.enclosingClass;
675675
if (enclosingClass != null) {
676676
var enclosingClasses =
677-
this.enclosingClasses ??= <InterfaceElement>[
677+
this.enclosingClasses ??= <InterfaceElement2>[
678678
enclosingClass,
679-
...enclosingClass.allSupertypes.map((t) => t.element),
679+
...enclosingClass.allSupertypes.map((t) => t.element3),
680680
];
681-
return enclosingClasses.contains(element.enclosingElement3);
681+
return enclosingClasses.contains(element.enclosingElement2);
682682
}
683683
return false;
684684
}

pkg/analyzer/lib/src/dart/analysis/session_helper.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,23 @@ class AnalysisSessionHelper {
173173
return null;
174174
}
175175

176+
/// Returns the [PropertyAccessorElement] with the given [name] that is
177+
/// exported from the library with the given [uri].
178+
///
179+
/// Returns `null` if the library does not export a top-level accessor with
180+
/// that name.
181+
Future<PropertyAccessorElement2?> getTopLevelPropertyAccessor2(
182+
String uri, String name) async {
183+
var libraryResult = await session.getLibraryByUri(uri);
184+
if (libraryResult is LibraryElementResult) {
185+
var element = libraryResult.element2.exportNamespace.get2(name);
186+
if (element is PropertyAccessorElement2) {
187+
return element;
188+
}
189+
}
190+
return null;
191+
}
192+
176193
/// Return a newly resolved, or cached library with the given [path].
177194
Future<ResolvedLibraryResult?> _getResolvedLibrary(String path) async {
178195
var result = _resolvedLibraries[path];

0 commit comments

Comments
 (0)