Skip to content

Commit 9f82b85

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Migrate Change Method Signature refactoring.
Change-Id: I909adf258183d56be98895e07585ab079feafe6e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/401940 Reviewed-by: Phil Quitslund <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent e872b7a commit 9f82b85

File tree

4 files changed

+40
-48
lines changed

4 files changed

+40
-48
lines changed

pkg/analysis_server/analyzer_use_new_elements.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ lib/src/lsp/handlers/handler_rename.dart
1111
lib/src/protocol_server.dart
1212
lib/src/services/correction/namespace.dart
1313
lib/src/services/kythe/kythe_visitors.dart
14-
lib/src/services/refactoring/agnostic/change_method_signature.dart
15-
lib/src/services/refactoring/framework/formal_parameter.dart
1614
lib/src/services/refactoring/legacy/extract_method.dart
1715
lib/src/services/refactoring/legacy/move_file.dart
1816
lib/src/services/refactoring/legacy/refactoring.dart

pkg/analysis_server/lib/src/services/refactoring/agnostic/change_method_signature.dart

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ import 'package:analysis_server_plugin/src/utilities/selection.dart';
1515
import 'package:analyzer/dart/analysis/results.dart';
1616
import 'package:analyzer/dart/ast/ast.dart';
1717
import 'package:analyzer/dart/ast/token.dart';
18-
import 'package:analyzer/dart/element/element.dart';
1918
import 'package:analyzer/dart/element/element2.dart';
2019
import 'package:analyzer/src/dart/analysis/session_helper.dart';
2120
import 'package:analyzer/src/dart/ast/extensions.dart';
2221
import 'package:analyzer/src/utilities/extensions/collection.dart';
23-
import 'package:analyzer/src/utilities/extensions/element.dart';
2422
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
2523
import 'package:analyzer_plugin/utilities/change_builder/change_builder_dart.dart';
2624
import 'package:analyzer_plugin/utilities/range_factory.dart';
@@ -110,7 +108,7 @@ final class FormalParameterState {
110108
final int id;
111109

112110
/// The element, used internally for `super` conversion.
113-
final ParameterElement element;
111+
final FormalParameterElement element;
114112

115113
/// The current kind of the formal parameter.
116114
final FormalParameterKind kind;
@@ -244,7 +242,7 @@ final class ValidSelectionState extends SelectionState {
244242

245243
/// The element of the target method, used to find corresponding methods
246244
/// in the class hierarchy.
247-
final ExecutableElement element;
245+
final ExecutableElement2 element;
248246

249247
/// The current formal parameters.
250248
final List<FormalParameterState> formalParameters;
@@ -254,10 +252,6 @@ final class ValidSelectionState extends SelectionState {
254252
required this.element,
255253
required this.formalParameters,
256254
});
257-
258-
/// The element of the target method, used to find corresponding methods
259-
/// in the class hierarchy.
260-
ExecutableElement2 get element2 => element.asElement2;
261255
}
262256

263257
class _AvailabilityAnalyzer {
@@ -306,8 +300,8 @@ class _AvailabilityAnalyzer {
306300
}
307301

308302
_Declaration? buildDeclaration(Declaration node) {
309-
var element = node.declaredElement;
310-
if (element is ExecutableElement) {
303+
var element = node.declaredFragment?.element;
304+
if (element is ExecutableElement2) {
311305
return _Declaration(element: element, node: node, selected: selected);
312306
}
313307
return null;
@@ -379,20 +373,20 @@ class _AvailabilityAnalyzer {
379373
Availability _executableElement() {
380374
var coveringNode = refactoringContext.coveringNode;
381375

382-
Element? element;
376+
Element2? element;
383377
if (coveringNode is SimpleIdentifier) {
384378
var invocation = coveringNode.parent;
385379
if (invocation is MethodInvocation &&
386380
invocation.methodName == coveringNode) {
387-
element = invocation.methodName.staticElement;
381+
element = invocation.methodName.element;
388382
}
389383
}
390384

391-
if (element is! ExecutableElement) {
385+
if (element is! ExecutableElement2) {
392386
return NotAvailableNoExecutableElement();
393387
}
394388

395-
var libraryFilePath = element.librarySource.fullName;
389+
var libraryFilePath = element.library2.firstFragment.source.fullName;
396390
if (!refactoringContext.workspace.containsFile(libraryFilePath)) {
397391
return NotAvailableExternalElement();
398392
}
@@ -414,7 +408,7 @@ final class _AvailableWithDeclaration extends Available {
414408

415409
@override
416410
bool get hasPositionalParameters {
417-
return declaration.element.parameters.any((e) => e.isPositional);
411+
return declaration.element.formalParameters.any((e) => e.isPositional);
418412
}
419413

420414
@override
@@ -479,7 +473,7 @@ final class _AvailableWithDeclaration extends Available {
479473
}
480474

481475
final class _AvailableWithExecutableElement extends Available {
482-
final ExecutableElement element;
476+
final ExecutableElement2 element;
483477

484478
_AvailableWithExecutableElement({
485479
required super.refactoringContext,
@@ -488,13 +482,13 @@ final class _AvailableWithExecutableElement extends Available {
488482

489483
@override
490484
bool get hasPositionalParameters {
491-
return element.parameters.any((e) => e.isPositional);
485+
return element.formalParameters.any((e) => e.isPositional);
492486
}
493487
}
494488

495489
/// The target method declaration.
496490
class _Declaration {
497-
final ExecutableElement element;
491+
final ExecutableElement2 element;
498492
final AstNode node;
499493
final List<FormalParameter> selected;
500494

@@ -557,7 +551,7 @@ class _SelectionAnalyzer {
557551
return const UnexpectedSelectionState();
558552
}
559553

560-
var parameterElement = parameterNode.declaredElement;
554+
var parameterElement = parameterNode.declaredFragment?.element;
561555
if (parameterElement == null) {
562556
return const UnexpectedSelectionState();
563557
}
@@ -599,9 +593,9 @@ class _SelectionAnalyzer {
599593
}
600594
}
601595

602-
Future<AstNode?> _elementDeclaration(ExecutableElement element) async {
596+
Future<AstNode?> _elementDeclaration(ExecutableElement2 element) async {
603597
var helper = refactoringContext.sessionHelper;
604-
var nodeResult = await helper.getElementDeclaration(element);
598+
var nodeResult = await helper.getElementDeclaration2(element.firstFragment);
605599
return nodeResult?.node;
606600
}
607601
}
@@ -659,27 +653,27 @@ class _SignatureUpdater {
659653
/// a function, only the elements itself has to be updated. If the target
660654
/// is a class method, then every element in this class hierarchy should
661655
/// be updated.
662-
Future<List<ExecutableElement>> computeElements() async {
656+
Future<List<ExecutableElement2>> computeElements() async {
663657
var element = selectionState.element;
664-
if (element case ClassMemberElement member) {
665-
var set = await getHierarchyMembers(searchEngine, member.asElement2!);
666-
return set
667-
.whereType<ExecutableElement2>()
668-
.map((e) => e.asElement)
669-
.toList();
658+
switch (element) {
659+
case ConstructorElement2():
660+
case MethodElement2():
661+
var set = await getHierarchyMembers(searchEngine, element);
662+
return set.whereType<ExecutableElement2>().toList();
663+
default:
664+
return [element];
670665
}
671-
return [element];
672666
}
673667

674-
/// Returns the [MethodDeclaration] for a [MethodElement].
675-
Future<AstNode?> elementDeclaration(ExecutableElement element) async {
668+
/// Returns the [MethodDeclaration] for a [MethodElement2].
669+
Future<AstNode?> elementDeclaration(ExecutableElement2 element) async {
676670
var helper = sessionHelper;
677-
var result = await helper.getElementDeclaration(element);
671+
var result = await helper.getElementDeclaration2(element.firstFragment);
678672
return result?.node;
679673
}
680674

681675
/// Returns the [Selection] for the [reference], using the resolved unit.
682-
/// Used to find [MethodInvocation]s of a [MethodElement].
676+
/// Used to find [MethodInvocation]s of a [MethodElement2].
683677
Future<Selection?> referenceSelection(SearchMatch reference) async {
684678
var unitResult = await referenceUnitResult(reference);
685679
return unitResult?.unit.select(
@@ -690,8 +684,8 @@ class _SignatureUpdater {
690684

691685
/// Returns the resolved unit with [reference].
692686
Future<ResolvedUnitResult?> referenceUnitResult(SearchMatch reference) async {
693-
var element = reference.element2.asElement!;
694-
return await sessionHelper.getResolvedUnitByElement(element);
687+
var element = reference.element2;
688+
return await sessionHelper.getResolvedUnitByElement2(element);
695689
}
696690

697691
/// Replaces [argumentList] with new code that has arguments as requested
@@ -746,12 +740,12 @@ class _SignatureUpdater {
746740
/// Replaces formal parameters of [element] with new code as requested
747741
/// by the formal parameter updates, reordering, changing kind, etc.
748742
Future<ChangeStatus> updateDeclaration({
749-
required ExecutableElement element,
743+
required ExecutableElement2 element,
750744
required ChangeBuilder builder,
751745
}) async {
752-
var path = element.source.fullName;
746+
var path = element.firstFragment.libraryFragment.source.fullName;
753747

754-
var unitResult = await sessionHelper.getResolvedUnitByElement(element);
748+
var unitResult = await sessionHelper.getResolvedUnitByElement2(element);
755749
if (unitResult == null) {
756750
return ChangeStatusFailure();
757751
}
@@ -967,10 +961,10 @@ class _SignatureUpdater {
967961

968962
/// Updates arguments of invocations of [element].
969963
Future<ChangeStatus> updateReferences({
970-
required ExecutableElement element,
964+
required ExecutableElement2 element,
971965
required ChangeBuilder builder,
972966
}) async {
973-
var references = await searchEngine.searchReferences(element.asElement2);
967+
var references = await searchEngine.searchReferences(element);
974968
for (var reference in references) {
975969
var unitResult = await referenceUnitResult(reference);
976970
if (unitResult == null) {
@@ -1144,7 +1138,7 @@ class _SignatureUpdater {
11441138
return ChangeStatusSuccess();
11451139
}
11461140

1147-
var parameterElementsToSuper = <ParameterElement>{};
1141+
var parameterElementsToSuper = <FormalParameterElement>{};
11481142
for (var update in signatureUpdate.formalParameters) {
11491143
if (update.kind.isNamed) {
11501144
var existing = selectionState.formalParameters[update.id];
@@ -1154,8 +1148,8 @@ class _SignatureUpdater {
11541148
continue;
11551149
}
11561150

1157-
var parameterElement = argument.staticElement;
1158-
if (parameterElement is! ParameterElement) {
1151+
var parameterElement = argument.element;
1152+
if (parameterElement is! FormalParameterElement) {
11591153
continue;
11601154
}
11611155

pkg/analysis_server/lib/src/services/refactoring/framework/formal_parameter.dart

Lines changed: 2 additions & 2 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

88
/// The kind of a formal parameter.
99
enum FormalParameterKind {
@@ -40,7 +40,7 @@ enum FormalParameterKind {
4040
return this == requiredPositional;
4141
}
4242

43-
static FormalParameterKind fromElement(ParameterElement element) {
43+
static FormalParameterKind fromElement(FormalParameterElement element) {
4444
if (element.isRequiredPositional) {
4545
return requiredPositional;
4646
} else if (element.isOptionalPositional) {

pkg/analysis_server/test/services/refactoring/agnostic/change_method_signature_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ NotAvailableNoExecutableElement
725725
buffer.writeln('UnexpectedSelectionState');
726726
case ValidSelectionState():
727727
buffer.write('element: ');
728-
buffer.writeln(_elementToReferenceString(selectionState.element2));
728+
buffer.writeln(_elementToReferenceString(selectionState.element));
729729
buffer.writeln('formalParameters');
730730
for (var formalParameter in selectionState.formalParameters) {
731731
buffer.writeln(' id: ${formalParameter.id}');

0 commit comments

Comments
 (0)