Skip to content

Commit 6311478

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Add documentationComment and metadata to Element and Fragment.
Bug: #61216 Change-Id: I46ee0dfe2a720d61b3b4576634cf623b8817d0ac Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/443149 Reviewed-by: Johnni Winther <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 64875e4 commit 6311478

36 files changed

+199
-288
lines changed

pkg/analysis_server/lib/plugin/protocol/protocol_dart.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ Element convertElement(engine.Element element) {
2424
name,
2525
Element.makeFlags(
2626
isPrivate: element.isPrivate,
27-
isDeprecated:
28-
(element is engine.Annotatable) &&
29-
(element as engine.Annotatable).metadata.hasDeprecated,
27+
isDeprecated: element.metadata.hasDeprecated,
3028
isAbstract: _isAbstract(element),
3129
isConst: _isConst(element),
3230
isFinal: _isFinal(element),

pkg/analysis_server/lib/src/computer/computer_documentation.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analysis_server/src/computer/computer_overrides.dart';
6-
import 'package:analysis_server/src/utilities/extensions/element.dart';
76
import 'package:analyzer/dart/element/element.dart';
87
import 'package:analyzer/src/dartdoc/dartdoc_directive_info.dart';
98

@@ -36,7 +35,7 @@ class DartDocumentationComputer {
3635
if (element case PropertyAccessorElement(:var variable)) variable,
3736
];
3837
for (var candidate in candidates) {
39-
if (candidate.documentationCommentOrNull != null) {
38+
if (candidate.documentationComment != null) {
4039
documentedElement = candidate;
4140
break;
4241
}
@@ -54,7 +53,7 @@ class DartDocumentationComputer {
5453
return null;
5554
}
5655

57-
var rawDoc = documentedElement.documentationCommentOrNull;
56+
var rawDoc = documentedElement.documentationComment;
5857
if (rawDoc == null) {
5958
return null;
6059
}

pkg/analysis_server/lib/src/computer/computer_hover.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ class DartUnitHoverComputer {
6767
// short code that illustrates the element meaning.
6868
hover.elementDescription = _elementDisplayString(node, element);
6969
hover.elementKind = element.kind.displayName;
70-
if (element case Annotatable a) {
71-
hover.isDeprecated = a.metadata.hasDeprecated;
72-
}
70+
hover.isDeprecated = element.metadata.hasDeprecated;
7371
// not local element
7472
if (element.enclosingElement is! ExecutableElement) {
7573
// containing class

pkg/analysis_server/lib/src/lsp/completion_utils.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -689,8 +689,7 @@ CompletionDetail _getCompletionDetail(
689689
// Use the full signature in the details popup.
690690
var detail = fullSignature;
691691
if (element != null &&
692-
(element is Annotatable &&
693-
(element as Annotatable).metadata.hasDeprecated) &&
692+
element.metadata.hasDeprecated &&
694693
!supportsDeprecated) {
695694
// If the item is deprecated and we don't support the native deprecated flag
696695
// then include it in the details.

pkg/analysis_server/lib/src/lsp/handlers/custom/editable_arguments/editable_arguments_mixin.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,8 @@ extension on InvocationExpressionImpl {
221221
_ => null,
222222
};
223223

224-
if (element case Annotatable annotatable) {
225-
return annotatable.metadata.hasWidgetFactory;
224+
if (element != null) {
225+
return element.metadata.hasWidgetFactory;
226226
}
227227
return false;
228228
}

pkg/analysis_server/lib/src/status/utilities/element_writer.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ class ElementWriter with TreeWriter {
3636
Map<String, Object?> _computeProperties(Element element) {
3737
var properties = <String, Object?>{};
3838

39-
if (element case Annotatable element) {
40-
properties['annotations'] = element.metadata.annotations;
41-
}
39+
properties['annotations'] = element.metadata.annotations;
4240
if (element is InterfaceElement) {
4341
properties['interfaces'] = element.interfaces;
4442
properties['isEnum'] = element is EnumElement;

pkg/analysis_server/lib/src/utilities/extensions/element.dart

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,12 @@ extension ClassElementExtensions on ClassElement {
2323
}
2424

2525
extension ElementExtensions on Element {
26-
/// The content of the documentation comment (including delimiters) for this
27-
/// element.
28-
///
29-
/// If the receiver is an element that has fragments, the comment will be a
30-
/// concatenation of the comments from all of the fragments.
31-
///
32-
/// Returns `null` if the receiver does not have or does not support
33-
/// documentation.
34-
String? get documentationCommentOrNull {
35-
return switch (this) {
36-
Annotatable(:var documentationComment) => documentationComment,
37-
_ => null,
38-
};
39-
}
40-
4126
/// Return `true` if this element, the enclosing class (if there is one), or
4227
/// the enclosing library, has been annotated with the `@deprecated`
4328
/// annotation.
4429
bool get hasOrInheritsDeprecated {
45-
if (this case Annotatable annotatable) {
46-
if (annotatable.metadata.hasDeprecated) {
47-
return true;
48-
}
30+
if (metadata.hasDeprecated) {
31+
return true;
4932
}
5033

5134
var ancestor = enclosingElement;

pkg/analyzer/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## 8.1.0-dev
22
* Add `DartObject.constructorInvocation` with the constructor and arguments.
33
* Make `PropertyAccessorElement.variable` non-nullable.
4+
* Add `documentationComment` to `Element` and `Fragment`.
5+
* Add `metadata` to `Element` and `Fragment`.`
46
* Fix draining analysis events when used by `package:build`.
57
* Deprecate `LibraryElementResult.element2`, use `element` instead.
68
* Deprecate `ResolvedLibraryResult.element2`, use `element` instead.
@@ -10,6 +12,7 @@
1012
* Deprecate `resolveFile2`, use `resolveFile` instead.
1113
* Deprecate `DartObject.variable2`, use `variable` instead.
1214
* Deprecate `DartObject.toFunctionValue2`, use `toFunctionValue` instead.
15+
* Deprecate `Annotatable`, use `documentationComment` and `metadata` of `Element` or `Fragment` directly.
1316

1417
## 8.0.0
1518
* Remove deprecated element model V1.

pkg/analyzer/api.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3283,7 +3283,7 @@ package:analyzer/dart/constant/value.dart:
32833283
toSymbolValue (method: String? Function())
32843284
toTypeValue (method: DartType? Function())
32853285
package:analyzer/dart/element/element.dart:
3286-
Annotatable (class extends Object):
3286+
Annotatable (class extends Object, deprecated):
32873287
new (constructor: Annotatable Function())
32883288
documentationComment (getter: String?)
32893289
metadata (getter: Metadata)
@@ -3379,6 +3379,7 @@ package:analyzer/dart/element/element.dart:
33793379
children (getter: List<Element>)
33803380
children2 (getter: List<Element>, deprecated)
33813381
displayName (getter: String)
3382+
documentationComment (getter: String?)
33823383
enclosingElement (getter: Element?)
33833384
enclosingElement2 (getter: Element?, deprecated)
33843385
firstFragment (getter: Fragment)
@@ -3391,6 +3392,7 @@ package:analyzer/dart/element/element.dart:
33913392
library (getter: LibraryElement?)
33923393
library2 (getter: LibraryElement?, deprecated)
33933394
lookupName (getter: String?)
3395+
metadata (getter: Metadata)
33943396
name (getter: String?)
33953397
name3 (getter: String?, deprecated)
33963398
nonSynthetic (getter: Element)
@@ -3648,9 +3650,11 @@ package:analyzer/dart/element/element.dart:
36483650
new (constructor: Fragment Function())
36493651
children (getter: List<Fragment>)
36503652
children3 (getter: List<Fragment>, deprecated)
3653+
documentationComment (getter: String?)
36513654
element (getter: Element)
36523655
enclosingFragment (getter: Fragment?)
36533656
libraryFragment (getter: LibraryFragment?)
3657+
metadata (getter: Metadata)
36543658
name (getter: String?)
36553659
name2 (getter: String?, deprecated)
36563660
nameOffset (getter: int?)

pkg/analyzer/lib/dart/element/element.dart

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ typedef VariableElement2 = VariableElement;
165165

166166
/// An element or fragment that can have either annotations (metadata), a
167167
/// documentation comment, or both associated with it.
168+
@Deprecated('Use Element or Fragment directly instead')
168169
abstract class Annotatable {
169170
/// The content of the documentation comment (including delimiters) for this
170171
/// element or fragment.
@@ -600,6 +601,14 @@ abstract class Element {
600601
/// but the `displayName` is `s`.
601602
String get displayName;
602603

604+
/// The content of the documentation comment (including delimiters) for this
605+
/// element.
606+
///
607+
/// This is a concatenation of the comments from all of the fragments.
608+
///
609+
/// Returns `null` if the element doesn't have documentation.
610+
String? get documentationComment;
611+
603612
/// The element that either physically or logically encloses this element.
604613
///
605614
/// Returns `null` if this element is a library because libraries are the
@@ -676,6 +685,13 @@ abstract class Element {
676685
/// For a binary operator `-` the result is just `-`.
677686
String? get lookupName;
678687

688+
/// The metadata associated with the element.
689+
///
690+
/// It includes all annotations from all of the fragments.
691+
///
692+
/// The list will be empty if the element does not have any metadata.
693+
Metadata get metadata;
694+
679695
/// The name of this element.
680696
///
681697
/// Returns `null` if this element doesn't have a name.
@@ -1012,7 +1028,10 @@ abstract class ElementAnnotation {
10121028
/// A directive within a library fragment.
10131029
///
10141030
/// Clients may not extend, implement or mix-in this class.
1015-
abstract class ElementDirective implements Annotatable {
1031+
abstract class ElementDirective
1032+
implements
1033+
Annotatable // ignore:deprecated_member_use_from_same_package
1034+
{
10161035
/// The library fragment that contains this object.
10171036
LibraryFragment get libraryFragment;
10181037

@@ -1581,7 +1600,10 @@ abstract class FieldFragment implements PropertyInducingFragment {
15811600
///
15821601
/// Clients may not extend, implement or mix-in this class.
15831602
abstract class FormalParameterElement
1584-
implements VariableElement, Annotatable, LocalElement {
1603+
implements
1604+
VariableElement,
1605+
Annotatable, // ignore:deprecated_member_use_from_same_package
1606+
LocalElement {
15851607
@override
15861608
FormalParameterElement get baseElement;
15871609

@@ -1692,7 +1714,10 @@ abstract class FormalParameterElement
16921714
///
16931715
/// Clients may not extend, implement, or mix-in this class.
16941716
abstract class FormalParameterFragment
1695-
implements VariableFragment, Annotatable, LocalFragment {
1717+
implements
1718+
VariableFragment,
1719+
Annotatable, // ignore:deprecated_member_use_from_same_package
1720+
LocalFragment {
16961721
@override
16971722
FormalParameterElement get element;
16981723

@@ -1735,6 +1760,12 @@ abstract class Fragment {
17351760
@Deprecated('Use children instead')
17361761
List<Fragment> get children3;
17371762

1763+
/// The content of the documentation comment (including delimiters) for this
1764+
/// fragment.
1765+
///
1766+
/// Returns `null` if the fragment doesn't have documentation.
1767+
String? get documentationComment;
1768+
17381769
/// The element composed from this fragment and possibly other fragments.
17391770
Element get element;
17401771

@@ -1749,6 +1780,11 @@ abstract class Fragment {
17491780
/// This will be the fragment itself if it is a library fragment.
17501781
LibraryFragment? get libraryFragment;
17511782

1783+
/// The metadata associated with the fragment.
1784+
///
1785+
/// The list will be empty if the fragment does not have any metadata.
1786+
Metadata get metadata;
1787+
17521788
/// The name of the fragment.
17531789
///
17541790
/// Never empty.
@@ -2564,7 +2600,11 @@ abstract class LabelFragment implements Fragment {
25642600
/// A library.
25652601
///
25662602
/// Clients may not extend, implement or mix-in this class.
2567-
abstract class LibraryElement implements Element, Annotatable {
2603+
abstract class LibraryElement
2604+
implements
2605+
Element,
2606+
Annotatable // ignore:deprecated_member_use_from_same_package
2607+
{
25682608
/// The classes defined in this library.
25692609
///
25702610
/// There is no guarantee of the order in which the classes will be returned.
@@ -3045,7 +3085,11 @@ abstract class LocalFunctionFragment
30453085
///
30463086
/// Clients may not extend, implement or mix-in this class.
30473087
abstract class LocalVariableElement
3048-
implements VariableElement, LocalElement, Annotatable {
3088+
implements
3089+
VariableElement,
3090+
LocalElement,
3091+
Annotatable // ignore:deprecated_member_use_from_same_package
3092+
{
30493093
@override
30503094
LocalVariableElement get baseElement;
30513095

@@ -3523,7 +3567,11 @@ abstract class PropertyAccessorFragment implements ExecutableFragment {
35233567
/// [PropertyInducingElement].
35243568
///
35253569
/// Clients may not extend, implement or mix-in this class.
3526-
abstract class PropertyInducingElement implements VariableElement, Annotatable {
3570+
abstract class PropertyInducingElement
3571+
implements
3572+
VariableElement,
3573+
Annotatable // ignore:deprecated_member_use_from_same_package
3574+
{
35273575
@override
35283576
PropertyInducingFragment get firstFragment;
35293577

@@ -3584,7 +3632,10 @@ abstract class PropertyInducingElement implements VariableElement, Annotatable {
35843632
///
35853633
/// Clients may not extend, implement or mix-in this class.
35863634
abstract class PropertyInducingFragment
3587-
implements VariableFragment, Annotatable {
3635+
implements
3636+
VariableFragment,
3637+
Annotatable // ignore:deprecated_member_use_from_same_package
3638+
{
35883639
@override
35893640
PropertyInducingElement get element;
35903641

@@ -3868,7 +3919,11 @@ abstract class TypeAliasFragment
38683919
/// An element that defines a type.
38693920
///
38703921
/// Clients may not extend, implement or mix-in this class.
3871-
abstract class TypeDefiningElement implements Element, Annotatable {
3922+
abstract class TypeDefiningElement
3923+
implements
3924+
Element,
3925+
Annotatable // ignore:deprecated_member_use_from_same_package
3926+
{
38723927
// TODO(brianwilkerson): Evaluate to see whether this type is actually needed
38733928
// after converting clients to the new API.
38743929

@@ -3882,7 +3937,11 @@ abstract class TypeDefiningElement implements Element, Annotatable {
38823937
/// The portion of a [TypeDefiningElement] contributed by a single declaration.
38833938
///
38843939
/// Clients may not extend, implement or mix-in this class.
3885-
abstract class TypeDefiningFragment implements Fragment, Annotatable {
3940+
abstract class TypeDefiningFragment
3941+
implements
3942+
Fragment,
3943+
Annotatable // ignore:deprecated_member_use_from_same_package
3944+
{
38863945
@override
38873946
TypeDefiningElement get element;
38883947

@@ -3943,7 +4002,11 @@ abstract class TypeParameterFragment implements TypeDefiningFragment {
39434002
/// An element that has type parameters, such as a class, typedef, or method.
39444003
///
39454004
/// Clients may not extend, implement or mix-in this class.
3946-
abstract class TypeParameterizedElement implements Element, Annotatable {
4005+
abstract class TypeParameterizedElement
4006+
implements
4007+
Element,
4008+
Annotatable // ignore:deprecated_member_use_from_same_package
4009+
{
39474010
@override
39484011
TypeParameterizedFragment get firstFragment;
39494012

@@ -3982,7 +4045,11 @@ abstract class TypeParameterizedElement implements Element, Annotatable {
39824045
/// declaration.
39834046
///
39844047
/// Clients may not extend, implement or mix-in this class.
3985-
abstract class TypeParameterizedFragment implements Fragment, Annotatable {
4048+
abstract class TypeParameterizedFragment
4049+
implements
4050+
Fragment,
4051+
Annotatable // ignore:deprecated_member_use_from_same_package
4052+
{
39864053
@override
39874054
TypeParameterizedElement get element;
39884055

0 commit comments

Comments
 (0)