From 070485c19d26ed342a66a6a6717e2bd31332aa6d Mon Sep 17 00:00:00 2001 From: Brian Wilkerson Date: Thu, 8 Feb 2024 20:47:26 +0000 Subject: [PATCH] Address some feedback on a previous CL Change-Id: I2e0f59fb2d22677efa58720d0bd6e73ac65daa22 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/350980 Commit-Queue: Brian Wilkerson Reviewed-by: Konstantin Shcheglov --- .../completion/dart/declaration_helper.dart | 48 +++++++++++++------ pkg/analysis_server/test/test_macros.dart | 18 ++++++- pkg/analyzer/lib/dart/element/element.dart | 9 +++- 3 files changed, 58 insertions(+), 17 deletions(-) diff --git a/pkg/analysis_server/lib/src/services/completion/dart/declaration_helper.dart b/pkg/analysis_server/lib/src/services/completion/dart/declaration_helper.dart index abc5fdca5df..0444838f268 100644 --- a/pkg/analysis_server/lib/src/services/completion/dart/declaration_helper.dart +++ b/pkg/analysis_server/lib/src/services/completion/dart/declaration_helper.dart @@ -364,30 +364,27 @@ class DeclarationHelper { case EnumElement(): var augmented = element.augmented; _addStaticMembers( - accessors: [...element.accessors, ...?augmented?.accessors], + accessors: augmented?.accessors ?? element.accessors, constructors: const [], containingElement: element, - fields: [...element.fields, ...?augmented?.fields], - methods: [...element.methods, ...?augmented?.methods]); + fields: augmented?.fields ?? element.fields, + methods: augmented?.methods ?? element.methods); case ExtensionElement(): var augmented = element.augmented; _addStaticMembers( - accessors: [...element.accessors, ...?augmented?.accessors], + accessors: augmented?.accessors ?? element.accessors, constructors: const [], containingElement: element, - fields: [...element.fields, ...?augmented?.fields], - methods: [...element.methods, ...?augmented?.methods]); + fields: augmented?.fields ?? element.fields, + methods: augmented?.methods ?? element.methods); case InterfaceElement(): var augmented = element.augmented; _addStaticMembers( - accessors: [...element.accessors, ...?augmented?.accessors], - constructors: [ - ...element.constructors, - ...?augmented?.constructors - ], + accessors: augmented?.accessors ?? element.accessors, + constructors: augmented?.constructors ?? element.constructors, containingElement: element, - fields: [...element.fields, ...?augmented?.fields], - methods: [...element.methods, ...?augmented?.methods]); + fields: augmented?.fields ?? element.fields, + methods: augmented?.methods ?? element.methods); } } @@ -775,6 +772,8 @@ class DeclarationHelper { /// Adds suggestions for the [members] of the [containingElement]. void _addMembers(Element containingElement, NodeList members) { + // TODO(brianwilkerson): Replace this method with methods similar to + // `_addMembersOfClass`. for (var member in members) { switch (member) { case ConstructorDeclaration(): @@ -832,7 +831,7 @@ class DeclarationHelper { var classElement = parent.declaredElement; if (classElement != null) { if (!mustBeType) { - _addMembers(classElement, parent.members); + _addMembersOfClass(classElement); } _suggestTypeParameters(classElement.typeParameters); } @@ -890,6 +889,27 @@ class DeclarationHelper { } } + /// Adds suggestions for the [members] of the [containingElement]. + void _addMembersOfClass(ClassElement classElement) { + // Add any immediate members from augmentations. + var augmented = classElement.augmented; + for (var accessor in augmented?.accessors ?? classElement.accessors) { + if (!accessor.isSynthetic && (!mustBeStatic || accessor.isStatic)) { + _suggestProperty(accessor, classElement); + } + } + for (var field in augmented?.fields ?? classElement.fields) { + if (!field.isSynthetic && (!mustBeStatic || field.isStatic)) { + _suggestField(field, classElement); + } + } + for (var method in augmented?.methods ?? classElement.methods) { + if (!mustBeStatic || method.isStatic) { + _suggestMethod(method, classElement); + } + } + } + /// Adds suggestions for the instance members declared on `Object`. void _addMembersOfDartCoreObject() { _addInstanceMembers( diff --git a/pkg/analysis_server/test/test_macros.dart b/pkg/analysis_server/test/test_macros.dart index 105a8b21946..61e4fcb15c7 100644 --- a/pkg/analysis_server/test/test_macros.dart +++ b/pkg/analysis_server/test/test_macros.dart @@ -11,7 +11,13 @@ mixin TestMacros { /// Return the declaration of a macro that will add a member to a library. /// - /// The text of the member is provided as an argument to the macro. + /// The text of the member to be declared is provided as an argument to the + /// macro that is returned. For example, the following can be used to generate + /// a top level function in the library containing the class `C`: + /// ```dart + /// @DeclareInLibrary('void generatedTopLevelFunction() {}') + /// class C {} + /// ``` String declareInLibraryMacro() { return ''' macro class DeclareInLibrary @@ -41,7 +47,15 @@ macro class DeclareInLibrary /// Return the declaration of a macro that will add a member to a type. /// - /// The text of the member is provided as an argument to the macro. + /// The text of the member to be declared is provided as an argument to the + /// macro that is returned. For example, the following can be used to generate + /// a method in the class `C`: + /// ```dart + /// @DeclareInType(' void generatedMethod() {}') + /// class C {} + /// ``` + /// (Adding the indent makes the content of the generated code look nicer, but + /// isn't required.) String declareInTypeMacro() { return ''' macro class DeclareInType diff --git a/pkg/analyzer/lib/dart/element/element.dart b/pkg/analyzer/lib/dart/element/element.dart index 97625284fba..41b97da8701 100644 --- a/pkg/analyzer/lib/dart/element/element.dart +++ b/pkg/analyzer/lib/dart/element/element.dart @@ -1475,7 +1475,14 @@ abstract class InstanceElement @experimental InstanceElement? get augmentationTarget; - /// The result of applying augmentations. + /// The result of merging augmentations. + /// + /// Returns `null` if this element is an augmentation and there is no base + /// element from which a merge can be performed. + /// + /// If a non-null instance is returned it will include the members of + /// the base element and its augmentations as specified by the merge + /// operations. @experimental AugmentedInstanceElement? get augmented;