Skip to content

Commit 35b664c

Browse files
authored
Complete extraction of sorted methods used by templates (#2443)
* Complete extraction of sorted methods used by templates * rebuild generated files
1 parent a8d14b0 commit 35b664c

36 files changed

+351
-318
lines changed

lib/src/generator/templates.renderers.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ class _Renderer_Package extends RendererBase<Package> {
130130
'documentedCategories': Property(
131131
getValue: (Object c) => (c as Package).documentedCategories,
132132
),
133+
'documentedCategoriesSorted': Property(
134+
getValue: (Object c) => (c as Package).documentedCategoriesSorted,
135+
),
133136
'documentedWhere': Property(
134137
getValue: (Object c) => (c as Package).documentedWhere,
135138
),
@@ -327,6 +330,9 @@ class _Renderer_LibraryContainer extends RendererBase<LibraryContainer> {
327330
'publicLibraries': Property(
328331
getValue: (Object c) => (c as LibraryContainer).publicLibraries,
329332
),
333+
'publicLibrariesSorted': Property(
334+
getValue: (Object c) => (c as LibraryContainer).publicLibrariesSorted,
335+
),
330336
'sortKey': Property(
331337
getValue: (Object c) => (c as LibraryContainer).sortKey,
332338
getProperties: _Renderer_String.propertyMap,

lib/src/model/class.dart

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ class Class extends Container
2323
implements EnclosedElement {
2424
// TODO(srawlins): To make final, remove public getter, setter, rename to be
2525
// public, and add `final` modifier.
26-
List<DefinedElementType> _mixins;
26+
List<DefinedElementType> _mixedInTypes;
2727

28-
List<DefinedElementType> get mixins => _mixins;
28+
List<DefinedElementType> get mixedInTypes => _mixedInTypes;
2929

3030
@Deprecated('Field intended to be final; setter will be removed as early as '
3131
'Dartdoc 1.0.0')
32-
set mixins(List<DefinedElementType> value) => _mixins = value;
32+
set mixedInTypes(List<DefinedElementType> value) => _mixedInTypes = value;
3333

3434
// TODO(srawlins): To make final, remove public getter, setter, rename to be
3535
// public, and add `final` modifier.
@@ -44,7 +44,7 @@ class Class extends Container
4444
final List<DefinedElementType> _interfaces;
4545

4646
Class(ClassElement element, Library library, PackageGraph packageGraph)
47-
: _mixins = element.mixins
47+
: _mixedInTypes = element.mixins
4848
.map<DefinedElementType>(
4949
(f) => ElementType.from(f, library, packageGraph))
5050
.where((mixin) => mixin != null)
@@ -109,10 +109,16 @@ class Class extends Container
109109
Iterable<Constructor> get constructors => element.constructors
110110
.map((e) => ModelElement.from(e, library, packageGraph) as Constructor);
111111

112+
bool get hasPublicConstructors => publicConstructors.isNotEmpty;
113+
112114
@visibleForTesting
113115
Iterable<Constructor> get publicConstructors =>
114116
model_utils.filterNonPublic(constructors);
115117

118+
List<Constructor> _publicConstructorsSorted;
119+
Iterable<Constructor> get publicConstructorsSorted =>
120+
_publicConstructorsSorted ??= publicConstructors.toList()..sort(byName);
121+
116122
/// Returns the library that encloses this element.
117123
@override
118124
ModelElement get enclosingElement => library;
@@ -131,24 +137,15 @@ class Class extends Container
131137
return kind;
132138
}
133139

134-
@override
135-
bool get hasPublicConstructors => publicConstructorsSorted.isNotEmpty;
136-
137-
List<Constructor> _publicConstructorsSorted;
138-
139-
@override
140-
List<Constructor> get publicConstructorsSorted =>
141-
_publicConstructorsSorted ??= publicConstructors.toList()..sort(byName);
142-
143140
bool get hasPublicImplementors => publicImplementors.isNotEmpty;
144141

145142
bool get hasPublicInterfaces => publicInterfaces.isNotEmpty;
146143

147-
bool get hasPublicMixins => publicMixins.isNotEmpty;
144+
bool get hasPublicMixedInTypes => publicMixedInTypes.isNotEmpty;
148145

149146
@override
150147
bool get hasModifiers =>
151-
hasPublicMixins ||
148+
hasPublicMixedInTypes ||
152149
hasAnnotations ||
153150
hasPublicInterfaces ||
154151
hasPublicSuperChainReversed ||
@@ -199,6 +196,10 @@ class Class extends Container
199196
return result;
200197
}
201198

199+
List<Class> _publicImplementorsSorted;
200+
Iterable<Class> get publicImplementorsSorted =>
201+
_publicImplementorsSorted ??= publicImplementors.toList()..sort(byName);
202+
202203
/*lazy final*/ List<Method> _inheritedMethods;
203204

204205
Iterable<Method> get inheritedMethods {
@@ -291,8 +292,8 @@ class Class extends Container
291292
@override
292293
String get kind => 'class';
293294

294-
Iterable<DefinedElementType> get publicMixins =>
295-
model_utils.filterNonPublic(mixins);
295+
Iterable<DefinedElementType> get publicMixedInTypes =>
296+
model_utils.filterNonPublic(mixedInTypes);
296297

297298
@override
298299
DefinedElementType get modelType => super.modelType;
@@ -310,7 +311,7 @@ class Class extends Container
310311
_inheritanceChain.add(this);
311312

312313
/// Caching should make this recursion a little less painful.
313-
for (var c in mixins.reversed.map((e) => (e.element as Class))) {
314+
for (var c in mixedInTypes.reversed.map((e) => (e.element as Class))) {
314315
_inheritanceChain.addAll(c.inheritanceChain);
315316
}
316317

lib/src/model/container.dart

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,6 @@ abstract class Container extends ModelElement with TypeParameters {
5454
.where((m) => !m.isStatic && !m.isOperator)
5555
.toList(growable: false);
5656

57-
/// Whether any constructors are public.
58-
///
59-
/// This is only used in mustache templates.
60-
bool get hasPublicConstructors => false;
61-
62-
Iterable<Constructor> get publicConstructorsSorted => [];
63-
6457
/// Whether all instance fields are inherited.
6558
///
6659
/// This is only used in mustache templates.

lib/src/model/extension.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ class Extension extends Container
5656
List<Method> get declaredMethods {
5757
_methods ??= _extension.methods.map((e) {
5858
return ModelElement.from(e, library, packageGraph) as Method;
59-
}).toList(growable: false)
60-
..sort(byName);
59+
}).toList(growable: false);
6160
return _methods;
6261
}
6362

@@ -78,8 +77,7 @@ class Extension extends Container
7877
}
7978
return ModelElement.fromPropertyInducingElement(f, library, packageGraph,
8079
getter: getter, setter: setter) as Field;
81-
}).toList(growable: false)
82-
..sort(byName);
80+
}).toList(growable: false);
8381
return _declaredFields;
8482
}
8583

lib/src/model/library.dart

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,7 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
200200
_extensions ??= _exportedAndLocalElements
201201
.whereType<ExtensionElement>()
202202
.map((e) => ModelElement.from(e, this, packageGraph) as Extension)
203-
.toList(growable: false)
204-
..sort(byName);
203+
.toList(growable: false);
205204
return _extensions;
206205
}
207206

@@ -358,8 +357,7 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
358357
.whereType<ClassElement>()
359358
.where((element) => element.isEnum)
360359
.map((e) => ModelElement.from(e, this, packageGraph) as Enum)
361-
.toList(growable: false)
362-
..sort(byName);
360+
.toList(growable: false);
363361
return _enums;
364362
}
365363

@@ -371,8 +369,7 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
371369
.whereType<ClassElement>()
372370
.where((ClassElement c) => c.isMixin)
373371
.map((e) => ModelElement.from(e, this, packageGraph) as Mixin)
374-
.toList(growable: false)
375-
..sort(byName);
372+
.toList(growable: false);
376373
return _mixins;
377374
}
378375

@@ -398,8 +395,7 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
398395
_functions ??=
399396
_exportedAndLocalElements.whereType<FunctionElement>().map((e) {
400397
return ModelElement.from(e, this, packageGraph) as ModelFunction;
401-
}).toList(growable: false)
402-
..sort(byName);
398+
}).toList(growable: false);
403399
return _functions;
404400
}
405401

@@ -480,8 +476,7 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
480476
_typedefs ??= _exportedAndLocalElements
481477
.whereType<FunctionTypeAliasElement>()
482478
.map((e) => ModelElement.from(e, this, packageGraph) as Typedef)
483-
.toList(growable: false)
484-
..sort(byName);
479+
.toList(growable: false);
485480
return _typedefs;
486481
}
487482

@@ -494,8 +489,7 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
494489
.whereType<ClassElement>()
495490
.where((e) => !e.isMixin && !e.isEnum)
496491
.map((e) => ModelElement.from(e, this, packageGraph) as Class)
497-
.toList(growable: false)
498-
..sort(byName);
492+
.toList(growable: false);
499493
return _classes;
500494
}
501495

@@ -528,7 +522,6 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
528522
getter: getter, setter: setter);
529523
_variables.add(me);
530524
}
531-
_variables.sort(byName);
532525
}
533526
return _variables;
534527
}

lib/src/model/library_container.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ abstract class LibraryContainer
1919
Iterable<Library> get publicLibraries =>
2020
model_utils.filterNonPublic(libraries);
2121

22+
List<Library> _publicLibrariesSorted;
23+
Iterable<Library> get publicLibrariesSorted =>
24+
_publicLibrariesSorted ??= publicLibraries.toList()..sort(byName);
25+
2226
bool get hasPublicLibraries => publicLibraries.isNotEmpty;
2327

2428
/// The name of the container or object that this LibraryContainer is a part

lib/src/model/package.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ class Package extends LibraryContainer
8282
/// Holds all libraries added to this package. May include non-documented
8383
/// libraries, but is not guaranteed to include a complete list of
8484
/// non-documented libraries unless they are all referenced by documented ones.
85-
/// Not sorted.
8685
final Set<Library> allLibraries = {};
8786

8887
bool get hasHomepage =>
@@ -338,6 +337,15 @@ class Package extends LibraryContainer
338337
Iterable<Category> get documentedCategories =>
339338
categories.where((c) => c.isDocumented);
340339

340+
Iterable<Category> get documentedCategoriesSorted {
341+
// Category display order is configurable; leave the category order
342+
// as defined if the order is specified.
343+
if (config.categoryOrder.isEmpty) {
344+
return documentedCategories;
345+
}
346+
return documentedCategories.toList()..sort(byName);
347+
}
348+
341349
bool get hasDocumentedCategories => documentedCategories.isNotEmpty;
342350

343351
DartdocOptionContext _config;

lib/src/model/package_graph.dart

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import 'package:analyzer/file_system/file_system.dart';
1010
import 'package:analyzer/src/generated/sdk.dart';
1111
import 'package:analyzer/src/generated/source.dart';
1212
import 'package:analyzer/src/generated/source_io.dart';
13-
import 'package:collection/collection.dart';
1413
import 'package:dartdoc/src/dartdoc_options.dart';
1514
import 'package:dartdoc/src/logging.dart';
1615
import 'package:dartdoc/src/model/model.dart';
@@ -80,7 +79,6 @@ class PackageGraph {
8079
// After the allModelElements traversal to be sure that all packages
8180
// are picked up.
8281
for (var package in documentedPackages) {
83-
package.libraries.sort((a, b) => compareNatural(a.name, b.name));
8482
for (var library in package.libraries) {
8583
_addToImplementors(library.allClasses);
8684
_extensions.addAll(library.extensions);
@@ -89,9 +87,6 @@ class PackageGraph {
8987
package.warn(PackageWarning.noDocumentableLibrariesInPackage);
9088
}
9189
}
92-
for (var l in _implementors.values) {
93-
l.sort();
94-
}
9590
allImplementorsAdded = true;
9691
allExtensionsAdded = true;
9792

@@ -614,7 +609,7 @@ class PackageGraph {
614609
}
615610

616611
void addImplementor(Class class_) {
617-
for (var type in class_.mixins) {
612+
for (var type in class_.mixedInTypes) {
618613
checkAndAddClass(type.element, class_);
619614
}
620615
if (class_.supertype != null) {

lib/src/model/top_level_container.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,59 @@ abstract class TopLevelContainer implements Nameable {
4949

5050
Iterable<Class> get publicClasses => model_utils.filterNonPublic(classes);
5151

52+
List<Class> _publicClassesSorted;
53+
Iterable<Class> get publicClassesSorted =>
54+
_publicClassesSorted ??= publicClasses.toList()..sort(byName);
55+
5256
Iterable<Extension> get publicExtensions =>
5357
model_utils.filterNonPublic(extensions);
5458

59+
List<Extension> _publicExtensionsSorted;
60+
Iterable<Extension> get publicExtensionsSorted =>
61+
_publicExtensionsSorted ??= publicExtensions.toList()..sort(byName);
62+
5563
Iterable<TopLevelVariable> get publicConstants =>
5664
model_utils.filterNonPublic(constants);
5765

66+
Iterable<TopLevelVariable> get publicConstantsSorted =>
67+
publicConstants.toList()..sort(byName);
68+
5869
Iterable<Enum> get publicEnums => model_utils.filterNonPublic(enums);
5970

71+
List<Enum> _publicEnumsSorted;
72+
Iterable<Enum> get publicEnumsSorted =>
73+
_publicEnumsSorted ??= publicEnums.toList()..sort(byName);
74+
6075
Iterable<Class> get publicExceptions =>
6176
model_utils.filterNonPublic(exceptions);
6277

78+
List<Class> _publicExceptionsSorted;
79+
Iterable<Class> get publicExceptionsSorted =>
80+
_publicExceptionsSorted ??= publicExceptions.toList()..sort(byName);
81+
6382
Iterable<ModelFunctionTyped> get publicFunctions =>
6483
model_utils.filterNonPublic(functions);
6584

85+
List<ModelFunctionTyped> _publicFunctionsSorted;
86+
Iterable<ModelFunctionTyped> get publicFunctionsSorted =>
87+
_publicFunctionsSorted ??= publicFunctions.toList()..sort(byName);
88+
6689
Iterable<Mixin> get publicMixins => model_utils.filterNonPublic(mixins);
6790

91+
List<Mixin> _publicMixinsSorted;
92+
Iterable<Mixin> get publicMixinsSorted =>
93+
_publicMixinsSorted ??= publicMixins.toList()..sort(byName);
94+
6895
Iterable<TopLevelVariable> get publicProperties =>
6996
model_utils.filterNonPublic(properties);
7097

98+
List<TopLevelVariable> _publicPropertiesSorted;
99+
Iterable<TopLevelVariable> get publicPropertiesSorted =>
100+
_publicPropertiesSorted ??= publicProperties.toList()..sort(byName);
101+
71102
Iterable<Typedef> get publicTypedefs => model_utils.filterNonPublic(typedefs);
103+
104+
List<Typedef> _publicTypedefsSorted;
105+
Iterable<Typedef> get publicTypedefsSorted =>
106+
_publicTypedefsSorted ??= publicTypedefs.toList()..sort(byName);
72107
}

lib/templates/html/_feature_set.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
{{#displayedLanguageFeatures}}
33
{{{featureLabel}}}
44
{{/displayedLanguageFeatures}}
5-
{{/hasFeatureSet}}
5+
{{/hasFeatureSet}}

lib/templates/html/_packages.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
{{#isFirstPackage}}
44
{{#hasDocumentedCategories}}
55
<li class="section-title">Topics</li>
6-
{{#documentedCategories}}
6+
{{#documentedCategoriesSorted}}
77
<li>{{{linkedName}}}</li>
8-
{{/documentedCategories}}
8+
{{/documentedCategoriesSorted}}
99
{{/hasDocumentedCategories}}
1010
<li class="section-title">Libraries</li>
1111
{{/isFirstPackage}}
1212
{{^isFirstPackage}}
1313
<li class="section-title">{{name}}</li>
1414
{{/isFirstPackage}}
15-
{{#defaultCategory.publicLibraries}}
15+
{{#defaultCategory.publicLibrariesSorted}}
1616
<li>{{{linkedName}}}</li>
17-
{{/defaultCategory.publicLibraries}}
17+
{{/defaultCategory.publicLibrariesSorted}}
1818
{{#categoriesWithPublicLibraries}}
1919
<li class="section-subtitle">{{name}}</li>
20-
{{#publicLibraries}}
20+
{{#publicLibrariesSorted}}
2121
<li class="section-subitem">{{{linkedName}}}</li>
22-
{{/publicLibraries}}
22+
{{/publicLibrariesSorted}}
2323
{{/categoriesWithPublicLibraries}}
2424
{{/localPackages}}
2525
</ol>

0 commit comments

Comments
 (0)