From 8a08c1bc56c5a31ffce79855aeac136a31e6b340 Mon Sep 17 00:00:00 2001 From: pq Date: Fri, 20 Dec 2024 13:56:49 -0800 Subject: [PATCH] [element model] migrate `session_helper` API Change-Id: Ia2717bb24c83a409e853fb5271a3d5ed91cd3306 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/402142 Reviewed-by: Brian Wilkerson --- .../handler_document_color_presentation.dart | 2 +- .../correction/dart/add_enum_constant.dart | 2 +- .../dart/add_key_to_constructors.dart | 2 +- .../services/correction/dart/add_late.dart | 2 +- .../correction/dart/create_constructor.dart | 8 +- .../create_constructor_for_final_fields.dart | 2 +- .../correction/dart/create_field.dart | 2 +- .../correction/dart/create_getter.dart | 2 +- .../correction/dart/create_method.dart | 10 +- .../dart/create_method_or_function.dart | 4 +- .../correction/dart/create_setter.dart | 2 +- .../flutter_convert_to_stateful_widget.dart | 4 +- .../flutter_convert_to_stateless_widget.dart | 2 +- .../correction/dart/flutter_wrap.dart | 6 +- .../correction/dart/flutter_wrap_builder.dart | 2 +- .../dart/flutter_wrap_stream_builder.dart | 2 +- .../correction/dart/make_field_not_final.dart | 2 +- .../correction/executable_parameters.dart | 4 +- .../lib/src/services/flutter/property.dart | 4 +- .../services/flutter/widget_descriptions.dart | 8 +- .../agnostic/change_method_signature.dart | 8 +- .../legacy/convert_getter_to_method.dart | 2 +- .../legacy/convert_method_to_getter.dart | 4 +- .../refactoring/legacy/extract_widget.dart | 4 +- .../refactoring/legacy/inline_local.dart | 2 +- .../refactoring/legacy/inline_method.dart | 4 +- .../legacy/rename_class_member.dart | 4 +- .../legacy/rename_constructor.dart | 4 +- .../legacy/rename_extension_member.dart | 2 +- .../refactoring/legacy/rename_local.dart | 2 +- .../refactoring/legacy/rename_parameter.dart | 2 +- .../services/snippets/snippet_producer.dart | 4 +- .../lib/edit/dart/correction_producer.dart | 62 ++------- .../lib/src/dart/analysis/session_helper.dart | 127 +++++------------- .../dart/analysis/session_helper_test.dart | 26 ++-- 35 files changed, 107 insertions(+), 222 deletions(-) diff --git a/pkg/analysis_server/lib/src/lsp/handlers/handler_document_color_presentation.dart b/pkg/analysis_server/lib/src/lsp/handlers/handler_document_color_presentation.dart index 5297baa463e3..28b449254480 100644 --- a/pkg/analysis_server/lib/src/lsp/handlers/handler_document_color_presentation.dart +++ b/pkg/analysis_server/lib/src/lsp/handlers/handler_document_color_presentation.dart @@ -158,7 +158,7 @@ class DocumentColorPresentationHandler var editRange = SourceRange(editStart, editEnd - editStart); var sessionHelper = AnalysisSessionHelper(unit.session); - var colorType = await sessionHelper.getFlutterClass2('Color'); + var colorType = await sessionHelper.getFlutterClass('Color'); if (colorType == null) { // If we can't find the class (perhaps because this isn't a Flutter // project) we will not include any results. In theory the client should diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_enum_constant.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_enum_constant.dart index a31da96c5158..49e2075d9ac5 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/add_enum_constant.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/add_enum_constant.dart @@ -42,7 +42,7 @@ class AddEnumConstant extends ResolvedCorrectionProducer { if (targetElement.library2.isInSdk) return; var targetFragment = targetElement.firstFragment; - var targetDeclarationResult = await sessionHelper.getElementDeclaration2( + var targetDeclarationResult = await sessionHelper.getElementDeclaration( targetFragment, ); if (targetDeclarationResult == null) return; diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_key_to_constructors.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_key_to_constructors.dart index 22de2536b134..89c4f45824ec 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/add_key_to_constructors.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/add_key_to_constructors.dart @@ -167,7 +167,7 @@ class AddKeyToConstructors extends ResolvedCorrectionProducer { /// Return the type for the class `Key`. Future _getKeyType() async { - var keyClass = await sessionHelper.getFlutterClass2('Key'); + var keyClass = await sessionHelper.getFlutterClass('Key'); if (keyClass == null) { return null; } diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_late.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_late.dart index fcb0cedbcf8f..399f8bbbb34f 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/add_late.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/add_late.dart @@ -69,7 +69,7 @@ class AddLate extends ResolvedCorrectionProducer { !variableElement.isLate && variableElement.setter2 == null) { var variableFragment = variableElement.firstFragment; - var declarationResult = await sessionHelper.getElementDeclaration2( + var declarationResult = await sessionHelper.getElementDeclaration( variableFragment, ); if (declarationResult == null) { diff --git a/pkg/analysis_server/lib/src/services/correction/dart/create_constructor.dart b/pkg/analysis_server/lib/src/services/correction/dart/create_constructor.dart index 783717565dff..67e98307cae2 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/create_constructor.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/create_constructor.dart @@ -89,7 +89,7 @@ class CreateConstructor extends ResolvedCorrectionProducer { // prepare target ClassDeclaration var targetElement = targetType.element3; var targetFragment = targetElement.firstFragment; - var targetResult = await sessionHelper.getElementDeclaration2( + var targetResult = await sessionHelper.getElementDeclaration( targetFragment, ); if (targetResult == null) { @@ -130,9 +130,7 @@ class CreateConstructor extends ResolvedCorrectionProducer { } // prepare target interface type - var targetResult = await sessionHelper.getElementDeclaration2( - targetElement, - ); + var targetResult = await sessionHelper.getElementDeclaration(targetElement); if (targetResult == null) { return; } @@ -185,7 +183,7 @@ class CreateConstructor extends ResolvedCorrectionProducer { } // prepare target ClassDeclaration - var targetResult = await sessionHelper.getElementDeclaration2( + var targetResult = await sessionHelper.getElementDeclaration( targetFragment, ); if (targetResult == null) { diff --git a/pkg/analysis_server/lib/src/services/correction/dart/create_constructor_for_final_fields.dart b/pkg/analysis_server/lib/src/services/correction/dart/create_constructor_for_final_fields.dart index 501444d4d862..e2899155af54 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/create_constructor_for_final_fields.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/create_constructor_for_final_fields.dart @@ -155,7 +155,7 @@ class CreateConstructorForFinalFields extends ResolvedCorrectionProducer { required _FixContext fixContext, required NamedCompilationUnitMember classDeclaration, }) async { - var keyClass = await sessionHelper.getFlutterClass2('Key'); + var keyClass = await sessionHelper.getFlutterClass('Key'); if (keyClass == null) { return; } diff --git a/pkg/analysis_server/lib/src/services/correction/dart/create_field.dart b/pkg/analysis_server/lib/src/services/correction/dart/create_field.dart index 9c989c86b8b9..36695e15d44e 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/create_field.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/create_field.dart @@ -76,7 +76,7 @@ class CreateField extends CreateFieldOrGetter { } // Prepare target `ClassDeclaration`. var targetFragment = targetElement.firstFragment; - var targetDeclarationResult = await sessionHelper.getElementDeclaration2( + var targetDeclarationResult = await sessionHelper.getElementDeclaration( targetFragment, ); if (targetDeclarationResult == null) { diff --git a/pkg/analysis_server/lib/src/services/correction/dart/create_getter.dart b/pkg/analysis_server/lib/src/services/correction/dart/create_getter.dart index 7c3416f8d6b8..41744154fba3 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/create_getter.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/create_getter.dart @@ -198,7 +198,7 @@ class CreateGetter extends CreateFieldOrGetter { return; } // prepare target declaration - var targetDeclarationResult = await sessionHelper.getElementDeclaration2( + var targetDeclarationResult = await sessionHelper.getElementDeclaration( targetFragment, ); if (targetDeclarationResult == null) { diff --git a/pkg/analysis_server/lib/src/services/correction/dart/create_method.dart b/pkg/analysis_server/lib/src/services/correction/dart/create_method.dart index f34200bb562d..539e935923f3 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/create_method.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/create_method.dart @@ -105,7 +105,7 @@ class CreateMethod extends ResolvedCorrectionProducer { if (target is ExtensionOverride) { targetFragment = target.element2.firstFragment; if (targetFragment is ExtensionFragment) { - targetNode = await getExtensionDeclaration2(targetFragment); + targetNode = await getExtensionDeclaration(targetFragment); if (targetNode == null) { return; } @@ -113,7 +113,7 @@ class CreateMethod extends ResolvedCorrectionProducer { } else if (target is Identifier && target.element is ExtensionElement2) { targetFragment = (target.element as ExtensionElement2).firstFragment; if (targetFragment is ExtensionFragment) { - targetNode = await getExtensionDeclaration2(targetFragment); + targetNode = await getExtensionDeclaration(targetFragment); if (targetNode == null) { return; } @@ -144,13 +144,13 @@ class CreateMethod extends ResolvedCorrectionProducer { // Prepare target ClassDeclaration. if (targetClassElement is MixinElement2) { var fragment = targetClassElement.firstFragment; - targetNode = await getMixinDeclaration2(fragment); + targetNode = await getMixinDeclaration(fragment); } else if (targetClassElement is ClassElement2) { var fragment = targetClassElement.firstFragment; - targetNode = await getClassDeclaration2(fragment); + targetNode = await getClassDeclaration(fragment); } else if (targetClassElement is ExtensionTypeElement2) { var fragment = targetClassElement.firstFragment; - targetNode = await getExtensionTypeDeclaration2(fragment); + targetNode = await getExtensionTypeDeclaration(fragment); } if (targetNode == null) { return; diff --git a/pkg/analysis_server/lib/src/services/correction/dart/create_method_or_function.dart b/pkg/analysis_server/lib/src/services/correction/dart/create_method_or_function.dart index 4f9c1f52b0d3..17c572257baf 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/create_method_or_function.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/create_method_or_function.dart @@ -203,11 +203,11 @@ class CreateMethodOrFunction extends ResolvedCorrectionProducer { List? classMembers; if (targetClassElement is MixinElement2) { var fragment = targetClassElement.firstFragment; - var node = targetNode = await getMixinDeclaration2(fragment); + var node = targetNode = await getMixinDeclaration(fragment); classMembers = node?.members; } else if (targetClassElement is ClassElement2) { var fragment = targetClassElement.firstFragment; - var node = targetNode = await getClassDeclaration2(fragment); + var node = targetNode = await getClassDeclaration(fragment); classMembers = node?.members; } if (targetNode == null || classMembers == null) { diff --git a/pkg/analysis_server/lib/src/services/correction/dart/create_setter.dart b/pkg/analysis_server/lib/src/services/correction/dart/create_setter.dart index 152df8414933..e1f5f6f02071 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/create_setter.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/create_setter.dart @@ -83,7 +83,7 @@ class CreateSetter extends ResolvedCorrectionProducer { return; } // prepare target declaration - var targetDeclarationResult = await sessionHelper.getElementDeclaration2( + var targetDeclarationResult = await sessionHelper.getElementDeclaration( targetFragment, ); if (targetDeclarationResult == null) { diff --git a/pkg/analysis_server/lib/src/services/correction/dart/flutter_convert_to_stateful_widget.dart b/pkg/analysis_server/lib/src/services/correction/dart/flutter_convert_to_stateful_widget.dart index 701fe94db773..d24722935307 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/flutter_convert_to_stateful_widget.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/flutter_convert_to_stateful_widget.dart @@ -117,10 +117,10 @@ class FlutterConvertToStatefulWidget extends ResolvedCorrectionProducer { return SourceEdit.applySequence(text, visitor.edits.reversed); } - var statefulWidgetClass = await sessionHelper.getFlutterClass2( + var statefulWidgetClass = await sessionHelper.getFlutterClass( 'StatefulWidget', ); - var stateClass = await sessionHelper.getFlutterClass2('State'); + var stateClass = await sessionHelper.getFlutterClass('State'); if (statefulWidgetClass == null || stateClass == null) { return; } diff --git a/pkg/analysis_server/lib/src/services/correction/dart/flutter_convert_to_stateless_widget.dart b/pkg/analysis_server/lib/src/services/correction/dart/flutter_convert_to_stateless_widget.dart index e5a2935271eb..de4c99d46d25 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/flutter_convert_to_stateless_widget.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/flutter_convert_to_stateless_widget.dart @@ -139,7 +139,7 @@ class FlutterConvertToStatelessWidget extends ResolvedCorrectionProducer { return SourceEdit.applySequence(text, visitor.edits.reversed); } - var statelessWidgetClass = await sessionHelper.getFlutterClass2( + var statelessWidgetClass = await sessionHelper.getFlutterClass( 'StatelessWidget', ); if (statelessWidgetClass == null) { diff --git a/pkg/analysis_server/lib/src/services/correction/dart/flutter_wrap.dart b/pkg/analysis_server/lib/src/services/correction/dart/flutter_wrap.dart index a628fa8b6abb..8546ffa2a4f2 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/flutter_wrap.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/flutter_wrap.dart @@ -272,11 +272,11 @@ abstract class _WrapMultipleWidgets extends ResolvedCorrectionProducer { Future compute(ChangeBuilder builder) async { var selectedRange = range.startEnd(firstWidget, lastWidget); var src = utils.getRangeText(selectedRange); - var parentClassElement = await sessionHelper.getClass2( + var parentClassElement = await sessionHelper.getClass( _parentLibraryUri, _parentClassName, ); - var widgetClassElement = await sessionHelper.getFlutterClass2('Widget'); + var widgetClassElement = await sessionHelper.getFlutterClass('Widget'); if (parentClassElement == null || widgetClassElement == null) { return; } @@ -341,7 +341,7 @@ abstract class _WrapSingleWidget extends ResolvedCorrectionProducer { var parentClassName = _parentClassName; ClassElement2? parentClassElement; if (parentLibraryUri != null && parentClassName != null) { - parentClassElement = await sessionHelper.getClass2( + parentClassElement = await sessionHelper.getClass( parentLibraryUri, parentClassName, ); diff --git a/pkg/analysis_server/lib/src/services/correction/dart/flutter_wrap_builder.dart b/pkg/analysis_server/lib/src/services/correction/dart/flutter_wrap_builder.dart index 09685b78d948..376005586664 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/flutter_wrap_builder.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/flutter_wrap_builder.dart @@ -33,7 +33,7 @@ class FlutterWrapBuilder extends ResolvedCorrectionProducer { } var widgetSrc = utils.getNodeText(widgetExpr); - var builderElement = await sessionHelper.getFlutterClass2('Builder'); + var builderElement = await sessionHelper.getFlutterClass('Builder'); if (builderElement == null) { return; } diff --git a/pkg/analysis_server/lib/src/services/correction/dart/flutter_wrap_stream_builder.dart b/pkg/analysis_server/lib/src/services/correction/dart/flutter_wrap_stream_builder.dart index 3928904e0cdb..5896103656ca 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/flutter_wrap_stream_builder.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/flutter_wrap_stream_builder.dart @@ -33,7 +33,7 @@ class FlutterWrapStreamBuilder extends ResolvedCorrectionProducer { } var widgetSrc = utils.getNodeText(widgetExpr); - var streamBuilderElement = await sessionHelper.getFlutterClass2( + var streamBuilderElement = await sessionHelper.getFlutterClass( 'StreamBuilder', ); if (streamBuilderElement == null) { diff --git a/pkg/analysis_server/lib/src/services/correction/dart/make_field_not_final.dart b/pkg/analysis_server/lib/src/services/correction/dart/make_field_not_final.dart index ec05b22acb26..90bb2867632c 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/make_field_not_final.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/make_field_not_final.dart @@ -57,7 +57,7 @@ class MakeFieldNotFinal extends ResolvedCorrectionProducer { return; } - var declaration = await sessionHelper.getElementDeclaration2( + var declaration = await sessionHelper.getElementDeclaration( variable.firstFragment, ); var variableNode = declaration?.node; diff --git a/pkg/analysis_server/lib/src/services/correction/executable_parameters.dart b/pkg/analysis_server/lib/src/services/correction/executable_parameters.dart index 608ddaa4ba3a..0083e9ea1535 100644 --- a/pkg/analysis_server/lib/src/services/correction/executable_parameters.dart +++ b/pkg/analysis_server/lib/src/services/correction/executable_parameters.dart @@ -43,7 +43,7 @@ class ExecutableParameters { /// Return the [FormalParameterList] of the [executable], or `null` if it /// can't be found. Future getParameterList() async { - var result = await sessionHelper.getElementDeclaration2(firstFragment); + var result = await sessionHelper.getElementDeclaration(firstFragment); var targetDeclaration = result?.node; if (targetDeclaration is ConstructorDeclaration) { return targetDeclaration.parameters; @@ -61,7 +61,7 @@ class ExecutableParameters { Future getParameterNode2( FormalParameterFragment fragment, ) async { - var result = await sessionHelper.getElementDeclaration2(fragment); + var result = await sessionHelper.getElementDeclaration(fragment); var declaration = result?.node; for (var node = declaration; node != null; node = node.parent) { if (node is FormalParameter && node.parent is FormalParameterList) { diff --git a/pkg/analysis_server/lib/src/services/flutter/property.dart b/pkg/analysis_server/lib/src/services/flutter/property.dart index d6b695d3e8fa..ac3c85c9ebdc 100644 --- a/pkg/analysis_server/lib/src/services/flutter/property.dart +++ b/pkg/analysis_server/lib/src/services/flutter/property.dart @@ -110,11 +110,11 @@ class PropertyDescription { var enumValue = value.enumValue; if (enumValue != null) { var helper = AnalysisSessionHelper(resolvedUnit.session); - enumElement = await helper.getClass2( + enumElement = await helper.getClass( enumValue.libraryUri, enumValue.className, ); - enumElement ??= await helper.getEnum2( + enumElement ??= await helper.getEnum( enumValue.libraryUri, enumValue.className, ); diff --git a/pkg/analysis_server/lib/src/services/flutter/widget_descriptions.dart b/pkg/analysis_server/lib/src/services/flutter/widget_descriptions.dart index 3a763cf2f60e..4ebf0d3cb10a 100644 --- a/pkg/analysis_server/lib/src/services/flutter/widget_descriptions.dart +++ b/pkg/analysis_server/lib/src/services/flutter/widget_descriptions.dart @@ -421,12 +421,12 @@ class _WidgetDescriptionComputer { Future _fetchClassElements() async { var sessionHelper = AnalysisSessionHelper(resolvedUnit.session); - _classAlignment = await sessionHelper.getFlutterClass2('Alignment'); - _classAlignmentDirectional = await sessionHelper.getFlutterClass2( + _classAlignment = await sessionHelper.getFlutterClass('Alignment'); + _classAlignmentDirectional = await sessionHelper.getFlutterClass( 'AlignmentDirectional', ); - _classContainer = await sessionHelper.getFlutterClass2('Container'); - _classEdgeInsets = await sessionHelper.getFlutterClass2('EdgeInsets'); + _classContainer = await sessionHelper.getFlutterClass('Container'); + _classEdgeInsets = await sessionHelper.getFlutterClass('EdgeInsets'); } protocol.FlutterWidgetPropertyEditor? _getEditor(DartType type) { 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 6440ebe100e6..0d430ebb300b 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 @@ -595,7 +595,7 @@ class _SelectionAnalyzer { Future _elementDeclaration(ExecutableElement2 element) async { var helper = refactoringContext.sessionHelper; - var nodeResult = await helper.getElementDeclaration2(element.firstFragment); + var nodeResult = await helper.getElementDeclaration(element.firstFragment); return nodeResult?.node; } } @@ -668,7 +668,7 @@ class _SignatureUpdater { /// Returns the [MethodDeclaration] for a [MethodElement2]. Future elementDeclaration(ExecutableElement2 element) async { var helper = sessionHelper; - var result = await helper.getElementDeclaration2(element.firstFragment); + var result = await helper.getElementDeclaration(element.firstFragment); return result?.node; } @@ -685,7 +685,7 @@ class _SignatureUpdater { /// Returns the resolved unit with [reference]. Future referenceUnitResult(SearchMatch reference) async { var element = reference.element2; - return await sessionHelper.getResolvedUnitByElement2(element); + return await sessionHelper.getResolvedUnitByElement(element); } /// Replaces [argumentList] with new code that has arguments as requested @@ -745,7 +745,7 @@ class _SignatureUpdater { }) async { var path = element.firstFragment.libraryFragment.source.fullName; - var unitResult = await sessionHelper.getResolvedUnitByElement2(element); + var unitResult = await sessionHelper.getResolvedUnitByElement(element); if (unitResult == null) { return ChangeStatusFailure(); } diff --git a/pkg/analysis_server/lib/src/services/refactoring/legacy/convert_getter_to_method.dart b/pkg/analysis_server/lib/src/services/refactoring/legacy/convert_getter_to_method.dart index 5eacfb7a1190..6bd63aae9e3a 100644 --- a/pkg/analysis_server/lib/src/services/refactoring/legacy/convert_getter_to_method.dart +++ b/pkg/analysis_server/lib/src/services/refactoring/legacy/convert_getter_to_method.dart @@ -113,7 +113,7 @@ class ConvertGetterToMethodRefactoringImpl extends RefactoringImpl return; } var sessionHelper = AnalysisSessionHelper(session); - var result = await sessionHelper.getElementDeclaration2(fragment); + var result = await sessionHelper.getElementDeclaration(fragment); var declaration = result?.node; if (declaration is MethodDeclaration) { getKeyword = declaration.propertyKeyword; diff --git a/pkg/analysis_server/lib/src/services/refactoring/legacy/convert_method_to_getter.dart b/pkg/analysis_server/lib/src/services/refactoring/legacy/convert_method_to_getter.dart index 5f834404ff97..d53678ed12ac 100644 --- a/pkg/analysis_server/lib/src/services/refactoring/legacy/convert_method_to_getter.dart +++ b/pkg/analysis_server/lib/src/services/refactoring/legacy/convert_method_to_getter.dart @@ -112,7 +112,7 @@ class ConvertMethodToGetterRefactoringImpl extends RefactoringImpl fragment != null; fragment = fragment.nextFragment as GetterFragment? ) { - var result = await sessionHelper.getElementDeclaration2(fragment); + var result = await sessionHelper.getElementDeclaration(fragment); var declaration = result?.node; if (declaration is MethodDeclaration) { parameters = declaration.parameters; @@ -148,7 +148,7 @@ class ConvertMethodToGetterRefactoringImpl extends RefactoringImpl var refRange = reference.range; // prepare invocation - var resolvedUnit = await sessionHelper.getResolvedUnitByElement2( + var resolvedUnit = await sessionHelper.getResolvedUnitByElement( refElement, ); var refUnit = resolvedUnit?.unit; diff --git a/pkg/analysis_server/lib/src/services/refactoring/legacy/extract_widget.dart b/pkg/analysis_server/lib/src/services/refactoring/legacy/extract_widget.dart index 98abbc20d1c7..cecc08a911cc 100644 --- a/pkg/analysis_server/lib/src/services/refactoring/legacy/extract_widget.dart +++ b/pkg/analysis_server/lib/src/services/refactoring/legacy/extract_widget.dart @@ -248,7 +248,7 @@ class ExtractWidgetRefactoringImpl extends RefactoringImpl var result = RefactoringStatus(); Future getClass(String name) async { - var element = await sessionHelper.getFlutterClass2(name); + var element = await sessionHelper.getFlutterClass(name); if (element == null) { result.addFatalError("Unable to find '$name' in $widgetsUri"); } @@ -259,7 +259,7 @@ class ExtractWidgetRefactoringImpl extends RefactoringImpl String uri, String name, ) async { - var element = await sessionHelper.getTopLevelPropertyAccessor2(uri, name); + var element = await sessionHelper.getTopLevelPropertyAccessor(uri, name); if (element == null) { result.addFatalError("Unable to find 'required' in $uri"); } diff --git a/pkg/analysis_server/lib/src/services/refactoring/legacy/inline_local.dart b/pkg/analysis_server/lib/src/services/refactoring/legacy/inline_local.dart index f3ae0a0ae79d..c93dea95677b 100644 --- a/pkg/analysis_server/lib/src/services/refactoring/legacy/inline_local.dart +++ b/pkg/analysis_server/lib/src/services/refactoring/legacy/inline_local.dart @@ -66,7 +66,7 @@ class InlineLocalRefactoringImpl extends RefactoringImpl } var helper = AnalysisSessionHelper(resolveResult.session); - var declarationResult = await helper.getElementDeclaration2( + var declarationResult = await helper.getElementDeclaration( element.firstFragment, ); var node = declarationResult?.node; diff --git a/pkg/analysis_server/lib/src/services/refactoring/legacy/inline_method.dart b/pkg/analysis_server/lib/src/services/refactoring/legacy/inline_method.dart index d5adb1a5dc08..466b4fc4ac2c 100644 --- a/pkg/analysis_server/lib/src/services/refactoring/legacy/inline_method.dart +++ b/pkg/analysis_server/lib/src/services/refactoring/legacy/inline_method.dart @@ -417,7 +417,7 @@ class InlineMethodRefactoringImpl extends RefactoringImpl } _methodElement = element; - var declaration = await sessionHelper.getElementDeclaration2( + var declaration = await sessionHelper.getElementDeclaration( element.firstFragment, ); var methodNode = declaration!.node; @@ -512,7 +512,7 @@ class _ReferenceProcessor { refElement = reference.element2; // prepare CorrectionUtils - var result = await ref.sessionHelper.getResolvedUnitByElement2(refElement); + var result = await ref.sessionHelper.getResolvedUnitByElement(refElement); _refUtils = CorrectionUtils(result!); // prepare node and environment diff --git a/pkg/analysis_server/lib/src/services/refactoring/legacy/rename_class_member.dart b/pkg/analysis_server/lib/src/services/refactoring/legacy/rename_class_member.dart index 6ee1617252d8..54cb8dcc42de 100644 --- a/pkg/analysis_server/lib/src/services/refactoring/legacy/rename_class_member.dart +++ b/pkg/analysis_server/lib/src/services/refactoring/legacy/rename_class_member.dart @@ -146,7 +146,7 @@ class RenameClassMemberRefactoringImpl extends RenameRefactoringImpl { ) async { FieldFormalParameterFragment? fragment = element.firstFragment; while (fragment != null) { - var result = await sessionHelper.getElementDeclaration2(fragment); + var result = await sessionHelper.getElementDeclaration(fragment); var node = result?.node; if (node is! DefaultFormalParameter) return; var parameter = node.parameter as FieldFormalParameter; @@ -439,7 +439,7 @@ class _RenameClassMemberValidator extends _BaseClassMemberValidator { var localElements = localElementMap[unitFragment]; if (localElements == null) { - var result = await sessionHelper.getResolvedUnitByElement2(element); + var result = await sessionHelper.getResolvedUnitByElement(element); if (result == null) { return const []; } diff --git a/pkg/analysis_server/lib/src/services/refactoring/legacy/rename_constructor.dart b/pkg/analysis_server/lib/src/services/refactoring/legacy/rename_constructor.dart index 509218c0140e..184f4f414f64 100644 --- a/pkg/analysis_server/lib/src/services/refactoring/legacy/rename_constructor.dart +++ b/pkg/analysis_server/lib/src/services/refactoring/legacy/rename_constructor.dart @@ -171,7 +171,7 @@ class RenameConstructorRefactoringImpl extends RenameRefactoringImpl { Future _nodeCoveringReference(SourceReference reference) async { var element = reference.element2; - var unitResult = await sessionHelper.getResolvedUnitByElement2(element); + var unitResult = await sessionHelper.getResolvedUnitByElement(element); return unitResult?.unit .select(offset: reference.range.offset, length: 0) ?.coveringNode; @@ -193,7 +193,7 @@ class RenameConstructorRefactoringImpl extends RenameRefactoringImpl { var classElement = element2.enclosingElement2; var fragment = classElement.firstFragment; - var result = await sessionHelper.getElementDeclaration2(fragment); + var result = await sessionHelper.getElementDeclaration(fragment); if (result == null) { return; } diff --git a/pkg/analysis_server/lib/src/services/refactoring/legacy/rename_extension_member.dart b/pkg/analysis_server/lib/src/services/refactoring/legacy/rename_extension_member.dart index 5ffbbf50da07..ec00b2b75017 100644 --- a/pkg/analysis_server/lib/src/services/refactoring/legacy/rename_extension_member.dart +++ b/pkg/analysis_server/lib/src/services/refactoring/legacy/rename_extension_member.dart @@ -166,7 +166,7 @@ class _ExtensionMemberValidator { var localElements = localElementMap[unitFragment]; if (localElements == null) { - var result = await sessionHelper.getResolvedUnitByElement2(element); + var result = await sessionHelper.getResolvedUnitByElement(element); if (result == null) { return const []; } diff --git a/pkg/analysis_server/lib/src/services/refactoring/legacy/rename_local.dart b/pkg/analysis_server/lib/src/services/refactoring/legacy/rename_local.dart index a949ce3e4b01..a4fd78557fdf 100644 --- a/pkg/analysis_server/lib/src/services/refactoring/legacy/rename_local.dart +++ b/pkg/analysis_server/lib/src/services/refactoring/legacy/rename_local.dart @@ -137,7 +137,7 @@ class RenameLocalRefactoringImpl extends RenameRefactoringImpl { @override Future checkFinalConditions() async { var result = RefactoringStatus(); - var resolvedUnit = await sessionHelper.getResolvedUnitByElement2(element2); + var resolvedUnit = await sessionHelper.getResolvedUnitByElement(element2); var unit = resolvedUnit?.unit; unit?.accept( ConflictValidatorVisitor( diff --git a/pkg/analysis_server/lib/src/services/refactoring/legacy/rename_parameter.dart b/pkg/analysis_server/lib/src/services/refactoring/legacy/rename_parameter.dart index 079e1ef247d3..45b89c2e6218 100644 --- a/pkg/analysis_server/lib/src/services/refactoring/legacy/rename_parameter.dart +++ b/pkg/analysis_server/lib/src/services/refactoring/legacy/rename_parameter.dart @@ -45,7 +45,7 @@ class RenameParameterRefactoringImpl extends RenameRefactoringImpl { ); break; } - var resolvedUnit = await sessionHelper.getResolvedUnitByElement2(element); + var resolvedUnit = await sessionHelper.getResolvedUnitByElement(element); var unit = resolvedUnit?.unit; unit?.accept( ConflictValidatorVisitor( diff --git a/pkg/analysis_server/lib/src/services/snippets/snippet_producer.dart b/pkg/analysis_server/lib/src/services/snippets/snippet_producer.dart index c179dd36e5a4..f8b8eb7cc261 100644 --- a/pkg/analysis_server/lib/src/services/snippets/snippet_producer.dart +++ b/pkg/analysis_server/lib/src/services/snippets/snippet_producer.dart @@ -76,7 +76,7 @@ abstract class FlutterSnippetProducer extends DartSnippetProducer { FlutterSnippetProducer(super.request, {required super.elementImportCache}); Future getClass(String name) async { - var class_ = await sessionHelper.getFlutterClass2(name); + var class_ = await sessionHelper.getFlutterClass(name); if (class_ != null) { requiredElementImports.add(class_); } @@ -84,7 +84,7 @@ abstract class FlutterSnippetProducer extends DartSnippetProducer { } Future getMixin(String name) async { - var mixin = await sessionHelper.getMixin2(widgetsUri, name); + var mixin = await sessionHelper.getMixin(widgetsUri, name); if (mixin != null) { requiredElementImports.add(mixin); } diff --git a/pkg/analysis_server_plugin/lib/edit/dart/correction_producer.dart b/pkg/analysis_server_plugin/lib/edit/dart/correction_producer.dart index 27e65fe25411..81e8f29b5aac 100644 --- a/pkg/analysis_server_plugin/lib/edit/dart/correction_producer.dart +++ b/pkg/analysis_server_plugin/lib/edit/dart/correction_producer.dart @@ -418,21 +418,10 @@ abstract class ResolvedCorrectionProducer /// The type for the class `bool` from `dart:core`. DartType get _coreTypeBool => typeProvider.boolType; - /// Returns the class declaration for the given [element], or `null` if there - /// is no such class. - Future getClassDeclaration(ClassElement element) async { - var result = await sessionHelper.getElementDeclaration(element); - var node = result?.node; - if (node is ClassDeclaration) { - return node; - } - return null; - } - /// Returns the class declaration for the given [fragment], or `null` if there /// is no such class. - Future getClassDeclaration2(ClassFragment fragment) async { - var result = await sessionHelper.getElementDeclaration2(fragment); + Future getClassDeclaration(ClassFragment fragment) async { + var result = await sessionHelper.getElementDeclaration(fragment); var node = result?.node; if (node is ClassDeclaration) { return node; @@ -440,23 +429,11 @@ abstract class ResolvedCorrectionProducer return null; } - /// Returns the extension declaration for the given [element], or `null` if - /// there is no such extension. - Future getExtensionDeclaration( - ExtensionElement element) async { - var result = await sessionHelper.getElementDeclaration(element); - var node = result?.node; - if (node is ExtensionDeclaration) { - return node; - } - return null; - } - /// Returns the extension declaration for the given [fragment], or `null` if /// there is no such extension. - Future getExtensionDeclaration2( + Future getExtensionDeclaration( ExtensionFragment fragment) async { - var result = await sessionHelper.getElementDeclaration2(fragment); + var result = await sessionHelper.getElementDeclaration(fragment); var node = result?.node; if (node is ExtensionDeclaration) { return node; @@ -464,23 +441,11 @@ abstract class ResolvedCorrectionProducer return null; } - /// Returns the extension type for the given [element], or `null` if there - /// is no such extension type. - Future getExtensionTypeDeclaration( - ExtensionTypeElement element) async { - var result = await sessionHelper.getElementDeclaration(element); - var node = result?.node; - if (node is ExtensionTypeDeclaration) { - return node; - } - return null; - } - /// Returns the extension type for the given [fragment], or `null` if there /// is no such extension type. - Future getExtensionTypeDeclaration2( + Future getExtensionTypeDeclaration( ExtensionTypeFragment fragment) async { - var result = await sessionHelper.getElementDeclaration2(fragment); + var result = await sessionHelper.getElementDeclaration(fragment); var node = result?.node; if (node is ExtensionTypeDeclaration) { return node; @@ -488,21 +453,10 @@ abstract class ResolvedCorrectionProducer return null; } - /// Returns the mixin declaration for the given [element], or `null` if there - /// is no such mixin. - Future getMixinDeclaration(MixinElement element) async { - var result = await sessionHelper.getElementDeclaration(element); - var node = result?.node; - if (node is MixinDeclaration) { - return node; - } - return null; - } - /// Returns the mixin declaration for the given [fragment], or `null` if there /// is no such mixin. - Future getMixinDeclaration2(MixinFragment fragment) async { - var result = await sessionHelper.getElementDeclaration2(fragment); + Future getMixinDeclaration(MixinFragment fragment) async { + var result = await sessionHelper.getElementDeclaration(fragment); var node = result?.node; if (node is MixinDeclaration) { return node; diff --git a/pkg/analyzer/lib/src/dart/analysis/session_helper.dart b/pkg/analyzer/lib/src/dart/analysis/session_helper.dart index 2ed685b8ff0f..ba6dad5e3695 100644 --- a/pkg/analyzer/lib/src/dart/analysis/session_helper.dart +++ b/pkg/analyzer/lib/src/dart/analysis/session_helper.dart @@ -20,24 +20,10 @@ class AnalysisSessionHelper { AnalysisSessionHelper(this.session); - /// Returns the [ClassElement] with the given [className] that is exported - /// from the library with the given [libraryUri], or `null` if the library - /// does not export a class with such name. - Future getClass(String libraryUri, String className) async { - var libraryResult = await session.getLibraryByUri(libraryUri); - if (libraryResult is LibraryElementResult) { - var element = libraryResult.element.exportNamespace.get(className); - if (element is ClassElement) { - return element; - } - } - return null; - } - - /// Returns the [ClassElement] with the given [className] that is exported + /// Returns the [ClassElement2] with the given [className] that is exported /// from the library with the given [libraryUri], or `null` if the library /// does not export a class with such name. - Future getClass2(String libraryUri, String className) async { + Future getClass(String libraryUri, String className) async { var libraryResult = await session.getLibraryByUri(libraryUri); if (libraryResult is LibraryElementResult) { var element = libraryResult.element2.exportNamespace.get2(className); @@ -48,44 +34,26 @@ class AnalysisSessionHelper { return null; } - /// Return the declaration of the [element], or `null` is the [element] - /// is synthetic, or is declared in a file that is not a part of a library. - Future getElementDeclaration( - Element element) async { - var libraryPath = element.library!.source.fullName; - var resolvedLibrary = await _getResolvedLibrary(libraryPath); - return resolvedLibrary?.getElementDeclaration(element); + @Deprecated('Use [getClass] instead.') + Future getClass2(String libraryUri, String className) async { + return await getClass(libraryUri, className); } /// Returns the declaration of the [fragment]. /// /// Returns `null` if the [fragment] is synthetic, or is declared in a file /// that is not a part of a library. - Future getElementDeclaration2( + Future getElementDeclaration( Fragment fragment) async { var libraryPath = fragment.libraryFragment!.source.fullName; var resolvedLibrary = await _getResolvedLibrary(libraryPath); return resolvedLibrary?.getElementDeclaration2(fragment); } - /// Return the [EnumElement] with the given [className] that is exported + /// Return the [EnumElement2] with the given [className] that is exported /// from the library with the given [libraryUri], or `null` if the library /// does not export a class with such name. - Future getEnum(String libraryUri, String className) async { - var libraryResult = await session.getLibraryByUri(libraryUri); - if (libraryResult is LibraryElementResult) { - var element = libraryResult.element.exportNamespace.get(className); - if (element is EnumElement) { - return element; - } - } - return null; - } - - /// Return the [EnumElement] with the given [className] that is exported - /// from the library with the given [libraryUri], or `null` if the library - /// does not export a class with such name. - Future getEnum2(String libraryUri, String className) async { + Future getEnum(String libraryUri, String className) async { var libraryResult = await session.getLibraryByUri(libraryUri); if (libraryResult is LibraryElementResult) { var element = libraryResult.element.exportNamespace.get2(className); @@ -96,36 +64,16 @@ class AnalysisSessionHelper { return null; } - /// Returns the [ClassElement] with the given [className] that is exported - /// from the Flutter widgets library, or `null` if the library does not export - /// a class with such name. - Future getFlutterClass(String className) => - getClass('package:flutter/widgets.dart', className); - /// Returns the [ClassElement2] with the given [className] that is exported /// from the Flutter widgets library, or `null` if the library does not export /// a class with such name. - Future getFlutterClass2(String className) => - getClass2('package:flutter/widgets.dart', className); - - /// Return the [MixinElement] with the given [name] that is exported - /// from the library with the given [libraryUri], or `null` if the library - /// does not export a class with such name. - Future getMixin(String libraryUri, String name) async { - var libraryResult = await session.getLibraryByUri(libraryUri); - if (libraryResult is LibraryElementResult) { - var element = libraryResult.element.exportNamespace.get(name); - if (element is MixinElement) { - return element; - } - } - return null; - } + Future getFlutterClass(String className) => + getClass('package:flutter/widgets.dart', className); - /// Return the [MixinElement] with the given [name] that is exported + /// Return the [MixinElement2] with the given [name] that is exported /// from the library with the given [libraryUri], or `null` if the library /// does not export a class with such name. - Future getMixin2(String libraryUri, String name) async { + Future getMixin(String libraryUri, String name) async { var libraryResult = await session.getLibraryByUri(libraryUri); if (libraryResult is LibraryElementResult) { var element = libraryResult.element2.exportNamespace.get2(name); @@ -137,48 +85,19 @@ class AnalysisSessionHelper { } /// Return the resolved unit that declares the given [element]. - Future getResolvedUnitByElement(Element element) async { - var libraryPath = element.library!.source.fullName; - var resolvedLibrary = await _getResolvedLibrary(libraryPath); - if (resolvedLibrary == null) { - return null; - } - - var unitPath = element.source!.fullName; - return resolvedLibrary.units.singleWhere((resolvedUnit) { - return resolvedUnit.path == unitPath; - }); - } - - /// Return the resolved unit that declares the given [element]. - Future getResolvedUnitByElement2( + Future getResolvedUnitByElement( Element2 element2) async { var element = element2.asElement; if (element == null) return null; - return await getResolvedUnitByElement(element); - } - - /// Return the [PropertyAccessorElement] with the given [name] that is - /// exported from the library with the given [uri], or `null` if the - /// library does not export a top-level accessor with such name. - Future getTopLevelPropertyAccessor( - String uri, String name) async { - var libraryResult = await session.getLibraryByUri(uri); - if (libraryResult is LibraryElementResult) { - var element = libraryResult.element.exportNamespace.get(name); - if (element is PropertyAccessorElement) { - return element; - } - } - return null; + return await _getResolvedUnitByElement(element); } - /// Returns the [PropertyAccessorElement] with the given [name] that is + /// Returns the [PropertyAccessorElement2] with the given [name] that is /// exported from the library with the given [uri]. /// /// Returns `null` if the library does not export a top-level accessor with /// that name. - Future getTopLevelPropertyAccessor2( + Future getTopLevelPropertyAccessor( String uri, String name) async { var libraryResult = await session.getLibraryByUri(uri); if (libraryResult is LibraryElementResult) { @@ -201,4 +120,18 @@ class AnalysisSessionHelper { } return result; } + + /// Return the resolved unit that declares the given [element]. + Future _getResolvedUnitByElement(Element element) async { + var libraryPath = element.library!.source.fullName; + var resolvedLibrary = await _getResolvedLibrary(libraryPath); + if (resolvedLibrary == null) { + return null; + } + + var unitPath = element.source!.fullName; + return resolvedLibrary.units.singleWhere((resolvedUnit) { + return resolvedUnit.path == unitPath; + }); + } } diff --git a/pkg/analyzer/test/src/dart/analysis/session_helper_test.dart b/pkg/analyzer/test/src/dart/analysis/session_helper_test.dart index a3e36cae3815..145b69223e14 100644 --- a/pkg/analyzer/test/src/dart/analysis/session_helper_test.dart +++ b/pkg/analyzer/test/src/dart/analysis/session_helper_test.dart @@ -34,7 +34,7 @@ int v = 0; '''); String uri = file.toUri().toString(); - var element = (await helper.getClass2(uri, 'C'))!; + var element = (await helper.getClass(uri, 'C'))!; expect(element.displayName, 'C'); } @@ -44,7 +44,7 @@ int v = 0; '''); String uri = file.toUri().toString(); - var element = await helper.getClass2(uri, 'v'); + var element = await helper.getClass(uri, 'v'); expect(element, isNull); } @@ -57,7 +57,7 @@ export 'a.dart'; '''); String bUri = bFile.toUri().toString(); - var element = (await helper.getClass2(bUri, 'A'))!; + var element = (await helper.getClass(bUri, 'A'))!; expect(element.displayName, 'A'); } @@ -70,7 +70,7 @@ import 'a.dart'; '''); String bUri = bFile.toUri().toString(); - var element = await helper.getClass2(bUri, 'A'); + var element = await helper.getClass(bUri, 'A'); expect(element, isNull); } @@ -79,7 +79,7 @@ import 'a.dart'; class A {} '''); var element = findElement2.class_('A'); - var result = (await helper.getElementDeclaration2(element.firstFragment))!; + var result = (await helper.getElementDeclaration(element.firstFragment))!; var node = result.node as ClassDeclaration; expect(node.name.lexeme, 'A'); } @@ -90,7 +90,7 @@ class A {} class B {} '''); var element = findNode.classDeclaration('A').declaredFragment!.element; - var resolvedUnit = (await helper.getResolvedUnitByElement2(element))!; + var resolvedUnit = (await helper.getResolvedUnitByElement(element))!; expect(resolvedUnit.unit.declarations, hasLength(2)); } @@ -100,7 +100,7 @@ int get a => 0; '''); String uri = file.toUri().toString(); - var element = (await helper.getTopLevelPropertyAccessor2(uri, 'a'))!; + var element = (await helper.getTopLevelPropertyAccessor(uri, 'a'))!; expect(element.kind, ElementKind.GETTER); expect(element.displayName, 'a'); } @@ -111,7 +111,7 @@ set a(_) {} '''); String uri = file.toUri().toString(); - var element = (await helper.getTopLevelPropertyAccessor2(uri, 'a='))!; + var element = (await helper.getTopLevelPropertyAccessor(uri, 'a='))!; expect(element.kind, ElementKind.SETTER); expect(element.displayName, 'a'); } @@ -122,7 +122,7 @@ int a; '''); String uri = file.toUri().toString(); - var element = (await helper.getTopLevelPropertyAccessor2(uri, 'a'))!; + var element = (await helper.getTopLevelPropertyAccessor(uri, 'a'))!; expect(element.kind, ElementKind.GETTER); expect(element.displayName, 'a'); } @@ -136,7 +136,7 @@ export 'a.dart'; '''); String bUri = bFile.toUri().toString(); - var element = (await helper.getTopLevelPropertyAccessor2(bUri, 'a'))!; + var element = (await helper.getTopLevelPropertyAccessor(bUri, 'a'))!; expect(element.kind, ElementKind.GETTER); expect(element.displayName, 'a'); } @@ -150,7 +150,7 @@ import 'a.dart'; '''); String bUri = bFile.toUri().toString(); - var element = await helper.getTopLevelPropertyAccessor2(bUri, 'a'); + var element = await helper.getTopLevelPropertyAccessor(bUri, 'a'); expect(element, isNull); } @@ -160,7 +160,7 @@ int a; '''); String uri = file.toUri().toString(); - var element = await helper.getTopLevelPropertyAccessor2(uri, 'b'); + var element = await helper.getTopLevelPropertyAccessor(uri, 'b'); expect(element, isNull); } @@ -170,7 +170,7 @@ int a() {} '''); String uri = file.toUri().toString(); - var element = await helper.getTopLevelPropertyAccessor2(uri, 'a'); + var element = await helper.getTopLevelPropertyAccessor(uri, 'a'); expect(element, isNull); } }