-
Notifications
You must be signed in to change notification settings - Fork 118
/
container.dart
268 lines (209 loc) · 9.72 KB
/
container.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/scope.dart';
import 'package:dartdoc/src/model/comment_referable.dart';
import 'package:dartdoc/src/model/model.dart';
import 'package:dartdoc/src/model_utils.dart' as model_utils;
import 'package:meta/meta.dart';
/// A [Container] represents a Dart construct that can contain methods,
/// operators, and fields, such as [Class], [Enum], or [Extension].
///
/// Member naming in [Container] follows these general rules:
///
/// * **instance** : children of this container that can be referenced from
/// within the container without a prefix. These are usually overridden in
/// subclasses with calls to 'super'.
/// * **constant** : children declared constant.
/// * **variable** : The opposite of constant. These are available for the
/// templating system.
/// * **static** : static children of this container.
/// * **public** : Filtered versions of the above members, containing only
/// public items. These are available mostly for the templating system.
/// * **sorted** : Filtered versions of the above members as a list sorted by
/// name. These are available for the templating system.
/// * **has** : boolean getters indicating whether the underlying collections
/// are empty. These are available mostly for the templating system.
abstract class Container extends ModelElement
with Categorization, TypeParameters {
Container(super.library, super.packageGraph);
// TODO(jcollins-g): Implement a ContainerScope that flattens supertypes?
@override
Scope? get scope => null;
@override
bool get hasParameters => false;
bool get isExtension => element is ExtensionElement;
/// Whether this is an interface type (class, enum, mixin, extension type) or
/// an extension.
///
/// For templates, interfaces and extensions have much in common despite
/// differing underlying implementations in the analyzer.
bool get isInterfaceOrExtension => element is InterfaceElement || isExtension;
/// Whether this is an enum.
bool get isEnum => element is EnumElement;
/// Whether this is a class or an enum.
bool get isClassOrEnum => element is InterfaceElement;
/// Whether this is a mixin.
bool get isMixin => element is MixinElement;
Iterable<ModelElement> get allModelElements => [
...instanceMethods,
...instanceFields,
...instanceOperators,
...instanceAccessors,
...staticFields,
...staticAccessors,
...staticMethods,
];
late final List<ModelElement> allCanonicalModelElements =
allModelElements.where((e) => e.isCanonical).toList(growable: false);
/// All methods, including operators and statics, declared as part of this
/// [Container].
///
/// [declaredMethods] must be the union of [instanceMethods],
/// [staticMethods], and [instanceOperators].
Iterable<Method> get declaredMethods;
Iterable<Method> get instanceMethods => declaredMethods
.where((m) => !m.isStatic && !m.isOperator)
.toList(growable: false);
/// Whether all instance fields are inherited.
bool get publicInheritedInstanceFields => false;
/// Whether all instance methods are inherited.
bool get publicInheritedInstanceMethods => false;
/// Whether all instance operators are inherited.
bool get publicInheritedInstanceOperators => false;
/// Override if this is [Constructable].
bool get hasPublicConstructors => false;
List<Constructor> get publicConstructorsSorted => const [];
@nonVirtual
bool get hasPublicInstanceMethods => instanceMethods.any((e) => e.isPublic);
List<Method> get publicInstanceMethodsSorted =>
instanceMethods.wherePublic.toList(growable: false)..sort();
@nonVirtual
late final List<Operator> declaredOperators =
declaredMethods.whereType<Operator>().toList(growable: false);
@override
ModelElement get enclosingElement;
Iterable<Operator> get instanceOperators => declaredOperators;
@nonVirtual
bool get hasPublicInstanceOperators =>
instanceOperators.any((e) => e.isPublic);
List<Operator> get publicInstanceOperatorsSorted =>
instanceOperators.wherePublic.toList(growable: false)..sort();
/// Fields fully declared in this [Container].
Iterable<Field> get declaredFields;
/// All instance fields declared in this [Container].
Iterable<Field> get instanceFields =>
declaredFields.where((f) => !f.isStatic);
bool get hasInstanceFields => instanceFields.isNotEmpty;
@nonVirtual
bool get hasPublicInstanceFields => instanceFields.any((e) => e.isPublic);
List<Field> get publicInstanceFieldsSorted =>
instanceFields.wherePublic.toList(growable: false)..sort(byName);
Iterable<Field> get constantFields => declaredFields.where((f) => f.isConst);
bool get hasPublicConstantFields => constantFields.any((e) => e.isPublic);
List<Field> get publicConstantFieldsSorted =>
constantFields.wherePublic.toList(growable: false)..sort(byName);
/// The total list of public enum values.
///
/// This is defined on [Container] instead of just [Enum], because the
/// `ContainerSidebar` Mustache template needs to refer to this field.
Iterable<Field> get publicEnumValues => const [];
/// Whether this container has any public enum values.
///
/// This is defined on [Container] instead of just [Enum], because the
/// `ContainerSidebar` Mustache template needs to refer to this field.
bool get hasPublicEnumValues => publicEnumValues.isNotEmpty;
Iterable<Accessor> get instanceAccessors =>
instanceFields.expand((f) => f.allAccessors);
Iterable<Accessor> get staticAccessors =>
staticFields.expand((f) => f.allAccessors);
/// This container might be canonical for elements it does not contain.
/// See [Inheritable.canonicalEnclosingContainer].
bool containsElement(Element? element) => allElements.contains(element);
late final Set<Element?> allElements =
allModelElements.map((e) => e.element).toSet();
late final Map<String, List<ModelElement>> _membersByName = () {
var membersByName = <String, List<ModelElement>>{};
for (var element in allModelElements) {
membersByName.putIfAbsent(element.name, () => []).add(element);
}
return membersByName;
}();
/// Given a ModelElement that is a member of some other class, returns
/// the member of this class that has the same name and runtime type.
///
/// This enables object substitution for canonicalization, such as Interceptor
/// for Object.
T memberByExample<T extends ModelElement>(T example) {
ModelElement member;
// [T] is insufficiently specific to disambiguate between different
// subtypes of [Inheritable] or other mixins/implementations of
// [ModelElement] via [Iterable.whereType].
var possibleMembers = _membersByName[example.name]!
.where((e) => e.runtimeType == example.runtimeType);
if (example is Accessor) {
possibleMembers = possibleMembers
.where((e) => example.isGetter == (e as Accessor).isGetter);
}
member = possibleMembers.first;
assert(possibleMembers.length == 1);
return member as T;
}
bool get hasPublicStaticFields => staticFields.any((e) => e.isPublic);
List<Field> get publicStaticFieldsSorted =>
staticFields.wherePublic.toList(growable: false)..sort();
Iterable<Field> get staticFields => declaredFields.where((f) => f.isStatic);
Iterable<Field> get variableStaticFields =>
staticFields.where((f) => !f.isConst);
bool get hasPublicVariableStaticFields =>
variableStaticFields.any((e) => e.isPublic);
List<Field> get publicVariableStaticFieldsSorted =>
variableStaticFields.wherePublic.toList(growable: false)..sort();
Iterable<Method> get staticMethods =>
declaredMethods.where((m) => m.isStatic);
bool get hasPublicStaticMethods => staticMethods.any((e) => e.isPublic);
List<Method> get publicStaticMethodsSorted =>
staticMethods.wherePublic.toList(growable: false)..sort();
/// For subclasses to add items after the main pass but before the
/// parameter-global.
@visibleForOverriding
Iterable<MapEntry<String, CommentReferable>> get extraReferenceChildren;
@override
@mustCallSuper
late final Map<String, CommentReferable> referenceChildren = () {
var referenceChildren = <String, CommentReferable>{
for (var element in allModelElements
.whereNotType<Accessor>()
.whereNotType<Constructor>())
element.referenceName: element,
};
referenceChildren.addEntriesIfAbsent(extraReferenceChildren);
// Process unscoped parameters last to make sure they don't override
// other options.
for (var modelElement in allModelElements) {
// Don't complain about references to parameter names, but prefer
// referring to anything else.
// TODO(jcollins-g): Figure out something good to do in the ecosystem
// here to wean people off the habit of unscoped parameter references.
if (modelElement.hasParameters) {
referenceChildren
.addEntriesIfAbsent(modelElement.parameters.generateEntries());
}
}
referenceChildren['this'] = this;
return referenceChildren;
}();
@override
Iterable<CommentReferable> get referenceParents => [definingLibrary, library];
@override
String get filePath => '${library.dirName}/${fileStructure.fileName}';
/// The full path of this element's sidebar file.
String get sidebarPath;
@override
String get aboveSidebarPath => library.sidebarPath;
@override
String get belowSidebarPath => sidebarPath;
/// The CSS class to use in an inheritance list.
String get relationshipsClass;
}