From 9f82b8595dce2d269cf2fe32f313360d53a51192 Mon Sep 17 00:00:00 2001 From: Konstantin Shcheglov Date: Thu, 19 Dec 2024 14:06:50 -0800 Subject: [PATCH] Elements. Migrate Change Method Signature refactoring. Change-Id: I909adf258183d56be98895e07585ab079feafe6e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/401940 Reviewed-by: Phil Quitslund Reviewed-by: Brian Wilkerson Commit-Queue: Konstantin Shcheglov --- .../analyzer_use_new_elements.txt | 2 - .../agnostic/change_method_signature.dart | 80 +++++++++---------- .../framework/formal_parameter.dart | 4 +- .../change_method_signature_test.dart | 2 +- 4 files changed, 40 insertions(+), 48 deletions(-) diff --git a/pkg/analysis_server/analyzer_use_new_elements.txt b/pkg/analysis_server/analyzer_use_new_elements.txt index 40ce9a497d8b..e4b658aa381c 100644 --- a/pkg/analysis_server/analyzer_use_new_elements.txt +++ b/pkg/analysis_server/analyzer_use_new_elements.txt @@ -11,8 +11,6 @@ lib/src/lsp/handlers/handler_rename.dart lib/src/protocol_server.dart lib/src/services/correction/namespace.dart lib/src/services/kythe/kythe_visitors.dart -lib/src/services/refactoring/agnostic/change_method_signature.dart -lib/src/services/refactoring/framework/formal_parameter.dart lib/src/services/refactoring/legacy/extract_method.dart lib/src/services/refactoring/legacy/move_file.dart lib/src/services/refactoring/legacy/refactoring.dart diff --git a/pkg/analysis_server/lib/src/services/refactoring/agnostic/change_method_signature.dart b/pkg/analysis_server/lib/src/services/refactoring/agnostic/change_method_signature.dart index 5642cc6393e9..6440ebe100e6 100644 --- a/pkg/analysis_server/lib/src/services/refactoring/agnostic/change_method_signature.dart +++ b/pkg/analysis_server/lib/src/services/refactoring/agnostic/change_method_signature.dart @@ -15,12 +15,10 @@ import 'package:analysis_server_plugin/src/utilities/selection.dart'; import 'package:analyzer/dart/analysis/results.dart'; import 'package:analyzer/dart/ast/ast.dart'; import 'package:analyzer/dart/ast/token.dart'; -import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/element2.dart'; import 'package:analyzer/src/dart/analysis/session_helper.dart'; import 'package:analyzer/src/dart/ast/extensions.dart'; import 'package:analyzer/src/utilities/extensions/collection.dart'; -import 'package:analyzer/src/utilities/extensions/element.dart'; import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart'; import 'package:analyzer_plugin/utilities/change_builder/change_builder_dart.dart'; import 'package:analyzer_plugin/utilities/range_factory.dart'; @@ -110,7 +108,7 @@ final class FormalParameterState { final int id; /// The element, used internally for `super` conversion. - final ParameterElement element; + final FormalParameterElement element; /// The current kind of the formal parameter. final FormalParameterKind kind; @@ -244,7 +242,7 @@ final class ValidSelectionState extends SelectionState { /// The element of the target method, used to find corresponding methods /// in the class hierarchy. - final ExecutableElement element; + final ExecutableElement2 element; /// The current formal parameters. final List formalParameters; @@ -254,10 +252,6 @@ final class ValidSelectionState extends SelectionState { required this.element, required this.formalParameters, }); - - /// The element of the target method, used to find corresponding methods - /// in the class hierarchy. - ExecutableElement2 get element2 => element.asElement2; } class _AvailabilityAnalyzer { @@ -306,8 +300,8 @@ class _AvailabilityAnalyzer { } _Declaration? buildDeclaration(Declaration node) { - var element = node.declaredElement; - if (element is ExecutableElement) { + var element = node.declaredFragment?.element; + if (element is ExecutableElement2) { return _Declaration(element: element, node: node, selected: selected); } return null; @@ -379,20 +373,20 @@ class _AvailabilityAnalyzer { Availability _executableElement() { var coveringNode = refactoringContext.coveringNode; - Element? element; + Element2? element; if (coveringNode is SimpleIdentifier) { var invocation = coveringNode.parent; if (invocation is MethodInvocation && invocation.methodName == coveringNode) { - element = invocation.methodName.staticElement; + element = invocation.methodName.element; } } - if (element is! ExecutableElement) { + if (element is! ExecutableElement2) { return NotAvailableNoExecutableElement(); } - var libraryFilePath = element.librarySource.fullName; + var libraryFilePath = element.library2.firstFragment.source.fullName; if (!refactoringContext.workspace.containsFile(libraryFilePath)) { return NotAvailableExternalElement(); } @@ -414,7 +408,7 @@ final class _AvailableWithDeclaration extends Available { @override bool get hasPositionalParameters { - return declaration.element.parameters.any((e) => e.isPositional); + return declaration.element.formalParameters.any((e) => e.isPositional); } @override @@ -479,7 +473,7 @@ final class _AvailableWithDeclaration extends Available { } final class _AvailableWithExecutableElement extends Available { - final ExecutableElement element; + final ExecutableElement2 element; _AvailableWithExecutableElement({ required super.refactoringContext, @@ -488,13 +482,13 @@ final class _AvailableWithExecutableElement extends Available { @override bool get hasPositionalParameters { - return element.parameters.any((e) => e.isPositional); + return element.formalParameters.any((e) => e.isPositional); } } /// The target method declaration. class _Declaration { - final ExecutableElement element; + final ExecutableElement2 element; final AstNode node; final List selected; @@ -557,7 +551,7 @@ class _SelectionAnalyzer { return const UnexpectedSelectionState(); } - var parameterElement = parameterNode.declaredElement; + var parameterElement = parameterNode.declaredFragment?.element; if (parameterElement == null) { return const UnexpectedSelectionState(); } @@ -599,9 +593,9 @@ class _SelectionAnalyzer { } } - Future _elementDeclaration(ExecutableElement element) async { + Future _elementDeclaration(ExecutableElement2 element) async { var helper = refactoringContext.sessionHelper; - var nodeResult = await helper.getElementDeclaration(element); + var nodeResult = await helper.getElementDeclaration2(element.firstFragment); return nodeResult?.node; } } @@ -659,27 +653,27 @@ class _SignatureUpdater { /// a function, only the elements itself has to be updated. If the target /// is a class method, then every element in this class hierarchy should /// be updated. - Future> computeElements() async { + Future> computeElements() async { var element = selectionState.element; - if (element case ClassMemberElement member) { - var set = await getHierarchyMembers(searchEngine, member.asElement2!); - return set - .whereType() - .map((e) => e.asElement) - .toList(); + switch (element) { + case ConstructorElement2(): + case MethodElement2(): + var set = await getHierarchyMembers(searchEngine, element); + return set.whereType().toList(); + default: + return [element]; } - return [element]; } - /// Returns the [MethodDeclaration] for a [MethodElement]. - Future elementDeclaration(ExecutableElement element) async { + /// Returns the [MethodDeclaration] for a [MethodElement2]. + Future elementDeclaration(ExecutableElement2 element) async { var helper = sessionHelper; - var result = await helper.getElementDeclaration(element); + var result = await helper.getElementDeclaration2(element.firstFragment); return result?.node; } /// Returns the [Selection] for the [reference], using the resolved unit. - /// Used to find [MethodInvocation]s of a [MethodElement]. + /// Used to find [MethodInvocation]s of a [MethodElement2]. Future referenceSelection(SearchMatch reference) async { var unitResult = await referenceUnitResult(reference); return unitResult?.unit.select( @@ -690,8 +684,8 @@ class _SignatureUpdater { /// Returns the resolved unit with [reference]. Future referenceUnitResult(SearchMatch reference) async { - var element = reference.element2.asElement!; - return await sessionHelper.getResolvedUnitByElement(element); + var element = reference.element2; + return await sessionHelper.getResolvedUnitByElement2(element); } /// Replaces [argumentList] with new code that has arguments as requested @@ -746,12 +740,12 @@ class _SignatureUpdater { /// Replaces formal parameters of [element] with new code as requested /// by the formal parameter updates, reordering, changing kind, etc. Future updateDeclaration({ - required ExecutableElement element, + required ExecutableElement2 element, required ChangeBuilder builder, }) async { - var path = element.source.fullName; + var path = element.firstFragment.libraryFragment.source.fullName; - var unitResult = await sessionHelper.getResolvedUnitByElement(element); + var unitResult = await sessionHelper.getResolvedUnitByElement2(element); if (unitResult == null) { return ChangeStatusFailure(); } @@ -967,10 +961,10 @@ class _SignatureUpdater { /// Updates arguments of invocations of [element]. Future updateReferences({ - required ExecutableElement element, + required ExecutableElement2 element, required ChangeBuilder builder, }) async { - var references = await searchEngine.searchReferences(element.asElement2); + var references = await searchEngine.searchReferences(element); for (var reference in references) { var unitResult = await referenceUnitResult(reference); if (unitResult == null) { @@ -1144,7 +1138,7 @@ class _SignatureUpdater { return ChangeStatusSuccess(); } - var parameterElementsToSuper = {}; + var parameterElementsToSuper = {}; for (var update in signatureUpdate.formalParameters) { if (update.kind.isNamed) { var existing = selectionState.formalParameters[update.id]; @@ -1154,8 +1148,8 @@ class _SignatureUpdater { continue; } - var parameterElement = argument.staticElement; - if (parameterElement is! ParameterElement) { + var parameterElement = argument.element; + if (parameterElement is! FormalParameterElement) { continue; } diff --git a/pkg/analysis_server/lib/src/services/refactoring/framework/formal_parameter.dart b/pkg/analysis_server/lib/src/services/refactoring/framework/formal_parameter.dart index 57a0d463954c..026f41d2853c 100644 --- a/pkg/analysis_server/lib/src/services/refactoring/framework/formal_parameter.dart +++ b/pkg/analysis_server/lib/src/services/refactoring/framework/formal_parameter.dart @@ -3,7 +3,7 @@ // BSD-style license that can be found in the LICENSE file. import 'package:analyzer/dart/ast/ast.dart'; -import 'package:analyzer/dart/element/element.dart'; +import 'package:analyzer/dart/element/element2.dart'; /// The kind of a formal parameter. enum FormalParameterKind { @@ -40,7 +40,7 @@ enum FormalParameterKind { return this == requiredPositional; } - static FormalParameterKind fromElement(ParameterElement element) { + static FormalParameterKind fromElement(FormalParameterElement element) { if (element.isRequiredPositional) { return requiredPositional; } else if (element.isOptionalPositional) { diff --git a/pkg/analysis_server/test/services/refactoring/agnostic/change_method_signature_test.dart b/pkg/analysis_server/test/services/refactoring/agnostic/change_method_signature_test.dart index f659dffa9b8a..7f505309d2ed 100644 --- a/pkg/analysis_server/test/services/refactoring/agnostic/change_method_signature_test.dart +++ b/pkg/analysis_server/test/services/refactoring/agnostic/change_method_signature_test.dart @@ -725,7 +725,7 @@ NotAvailableNoExecutableElement buffer.writeln('UnexpectedSelectionState'); case ValidSelectionState(): buffer.write('element: '); - buffer.writeln(_elementToReferenceString(selectionState.element2)); + buffer.writeln(_elementToReferenceString(selectionState.element)); buffer.writeln('formalParameters'); for (var formalParameter in selectionState.formalParameters) { buffer.writeln(' id: ${formalParameter.id}');