Skip to content

Commit

Permalink
Elements. Migrate Change Method Signature refactoring.
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
scheglov authored and Commit Queue committed Dec 19, 2024
1 parent e872b7a commit 9f82b85
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 48 deletions.
2 changes: 0 additions & 2 deletions pkg/analysis_server/analyzer_use_new_elements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<FormalParameterState> formalParameters;
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand All @@ -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
Expand Down Expand Up @@ -479,7 +473,7 @@ final class _AvailableWithDeclaration extends Available {
}

final class _AvailableWithExecutableElement extends Available {
final ExecutableElement element;
final ExecutableElement2 element;

_AvailableWithExecutableElement({
required super.refactoringContext,
Expand All @@ -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<FormalParameter> selected;

Expand Down Expand Up @@ -557,7 +551,7 @@ class _SelectionAnalyzer {
return const UnexpectedSelectionState();
}

var parameterElement = parameterNode.declaredElement;
var parameterElement = parameterNode.declaredFragment?.element;
if (parameterElement == null) {
return const UnexpectedSelectionState();
}
Expand Down Expand Up @@ -599,9 +593,9 @@ class _SelectionAnalyzer {
}
}

Future<AstNode?> _elementDeclaration(ExecutableElement element) async {
Future<AstNode?> _elementDeclaration(ExecutableElement2 element) async {
var helper = refactoringContext.sessionHelper;
var nodeResult = await helper.getElementDeclaration(element);
var nodeResult = await helper.getElementDeclaration2(element.firstFragment);
return nodeResult?.node;
}
}
Expand Down Expand Up @@ -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<List<ExecutableElement>> computeElements() async {
Future<List<ExecutableElement2>> computeElements() async {
var element = selectionState.element;
if (element case ClassMemberElement member) {
var set = await getHierarchyMembers(searchEngine, member.asElement2!);
return set
.whereType<ExecutableElement2>()
.map((e) => e.asElement)
.toList();
switch (element) {
case ConstructorElement2():
case MethodElement2():
var set = await getHierarchyMembers(searchEngine, element);
return set.whereType<ExecutableElement2>().toList();
default:
return [element];
}
return [element];
}

/// Returns the [MethodDeclaration] for a [MethodElement].
Future<AstNode?> elementDeclaration(ExecutableElement element) async {
/// Returns the [MethodDeclaration] for a [MethodElement2].
Future<AstNode?> 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<Selection?> referenceSelection(SearchMatch reference) async {
var unitResult = await referenceUnitResult(reference);
return unitResult?.unit.select(
Expand All @@ -690,8 +684,8 @@ class _SignatureUpdater {

/// Returns the resolved unit with [reference].
Future<ResolvedUnitResult?> 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
Expand Down Expand Up @@ -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<ChangeStatus> 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();
}
Expand Down Expand Up @@ -967,10 +961,10 @@ class _SignatureUpdater {

/// Updates arguments of invocations of [element].
Future<ChangeStatus> 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) {
Expand Down Expand Up @@ -1144,7 +1138,7 @@ class _SignatureUpdater {
return ChangeStatusSuccess();
}

var parameterElementsToSuper = <ParameterElement>{};
var parameterElementsToSuper = <FormalParameterElement>{};
for (var update in signatureUpdate.formalParameters) {
if (update.kind.isNamed) {
var existing = selectionState.formalParameters[update.id];
Expand All @@ -1154,8 +1148,8 @@ class _SignatureUpdater {
continue;
}

var parameterElement = argument.staticElement;
if (parameterElement is! ParameterElement) {
var parameterElement = argument.element;
if (parameterElement is! FormalParameterElement) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}');
Expand Down

0 comments on commit 9f82b85

Please sign in to comment.