Skip to content

Complete extraction of sorted methods used by templates #2443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/src/generator/templates.renderers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ class _Renderer_Package extends RendererBase<Package> {
'documentedCategories': Property(
getValue: (Object c) => (c as Package).documentedCategories,
),
'documentedCategoriesSorted': Property(
getValue: (Object c) => (c as Package).documentedCategoriesSorted,
),
'documentedWhere': Property(
getValue: (Object c) => (c as Package).documentedWhere,
),
Expand Down Expand Up @@ -327,6 +330,9 @@ class _Renderer_LibraryContainer extends RendererBase<LibraryContainer> {
'publicLibraries': Property(
getValue: (Object c) => (c as LibraryContainer).publicLibraries,
),
'publicLibrariesSorted': Property(
getValue: (Object c) => (c as LibraryContainer).publicLibrariesSorted,
),
'sortKey': Property(
getValue: (Object c) => (c as LibraryContainer).sortKey,
getProperties: _Renderer_String.propertyMap,
Expand Down
37 changes: 19 additions & 18 deletions lib/src/model/class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ class Class extends Container
implements EnclosedElement {
// TODO(srawlins): To make final, remove public getter, setter, rename to be
// public, and add `final` modifier.
List<DefinedElementType> _mixins;
List<DefinedElementType> _mixedInTypes;

List<DefinedElementType> get mixins => _mixins;
List<DefinedElementType> get mixedInTypes => _mixedInTypes;

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

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

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

bool get hasPublicConstructors => publicConstructors.isNotEmpty;

@visibleForTesting
Iterable<Constructor> get publicConstructors =>
model_utils.filterNonPublic(constructors);

List<Constructor> _publicConstructorsSorted;
Iterable<Constructor> get publicConstructorsSorted =>
_publicConstructorsSorted ??= publicConstructors.toList()..sort(byName);

/// Returns the library that encloses this element.
@override
ModelElement get enclosingElement => library;
Expand All @@ -131,24 +137,15 @@ class Class extends Container
return kind;
}

@override
bool get hasPublicConstructors => publicConstructorsSorted.isNotEmpty;

List<Constructor> _publicConstructorsSorted;

@override
List<Constructor> get publicConstructorsSorted =>
_publicConstructorsSorted ??= publicConstructors.toList()..sort(byName);

bool get hasPublicImplementors => publicImplementors.isNotEmpty;

bool get hasPublicInterfaces => publicInterfaces.isNotEmpty;

bool get hasPublicMixins => publicMixins.isNotEmpty;
bool get hasPublicMixedInTypes => publicMixedInTypes.isNotEmpty;

@override
bool get hasModifiers =>
hasPublicMixins ||
hasPublicMixedInTypes ||
hasAnnotations ||
hasPublicInterfaces ||
hasPublicSuperChainReversed ||
Expand Down Expand Up @@ -199,6 +196,10 @@ class Class extends Container
return result;
}

List<Class> _publicImplementorsSorted;
Iterable<Class> get publicImplementorsSorted =>
_publicImplementorsSorted ??= publicImplementors.toList()..sort(byName);

/*lazy final*/ List<Method> _inheritedMethods;

Iterable<Method> get inheritedMethods {
Expand Down Expand Up @@ -291,8 +292,8 @@ class Class extends Container
@override
String get kind => 'class';

Iterable<DefinedElementType> get publicMixins =>
model_utils.filterNonPublic(mixins);
Iterable<DefinedElementType> get publicMixedInTypes =>
model_utils.filterNonPublic(mixedInTypes);

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

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

Expand Down
7 changes: 0 additions & 7 deletions lib/src/model/container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,6 @@ abstract class Container extends ModelElement with TypeParameters {
.where((m) => !m.isStatic && !m.isOperator)
.toList(growable: false);

/// Whether any constructors are public.
///
/// This is only used in mustache templates.
bool get hasPublicConstructors => false;

Iterable<Constructor> get publicConstructorsSorted => [];

/// Whether all instance fields are inherited.
///
/// This is only used in mustache templates.
Expand Down
6 changes: 2 additions & 4 deletions lib/src/model/extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ class Extension extends Container
List<Method> get declaredMethods {
_methods ??= _extension.methods.map((e) {
return ModelElement.from(e, library, packageGraph) as Method;
}).toList(growable: false)
..sort(byName);
}).toList(growable: false);
return _methods;
}

Expand All @@ -78,8 +77,7 @@ class Extension extends Container
}
return ModelElement.fromPropertyInducingElement(f, library, packageGraph,
getter: getter, setter: setter) as Field;
}).toList(growable: false)
..sort(byName);
}).toList(growable: false);
return _declaredFields;
}

Expand Down
19 changes: 6 additions & 13 deletions lib/src/model/library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,7 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
_extensions ??= _exportedAndLocalElements
.whereType<ExtensionElement>()
.map((e) => ModelElement.from(e, this, packageGraph) as Extension)
.toList(growable: false)
..sort(byName);
.toList(growable: false);
return _extensions;
}

Expand Down Expand Up @@ -358,8 +357,7 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
.whereType<ClassElement>()
.where((element) => element.isEnum)
.map((e) => ModelElement.from(e, this, packageGraph) as Enum)
.toList(growable: false)
..sort(byName);
.toList(growable: false);
return _enums;
}

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

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

Expand Down Expand Up @@ -480,8 +476,7 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
_typedefs ??= _exportedAndLocalElements
.whereType<FunctionTypeAliasElement>()
.map((e) => ModelElement.from(e, this, packageGraph) as Typedef)
.toList(growable: false)
..sort(byName);
.toList(growable: false);
return _typedefs;
}

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

Expand Down Expand Up @@ -528,7 +522,6 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
getter: getter, setter: setter);
_variables.add(me);
}
_variables.sort(byName);
}
return _variables;
}
Expand Down
4 changes: 4 additions & 0 deletions lib/src/model/library_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ abstract class LibraryContainer
Iterable<Library> get publicLibraries =>
model_utils.filterNonPublic(libraries);

List<Library> _publicLibrariesSorted;
Iterable<Library> get publicLibrariesSorted =>
_publicLibrariesSorted ??= publicLibraries.toList()..sort(byName);

bool get hasPublicLibraries => publicLibraries.isNotEmpty;

/// The name of the container or object that this LibraryContainer is a part
Expand Down
10 changes: 9 additions & 1 deletion lib/src/model/package.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ class Package extends LibraryContainer
/// Holds all libraries added to this package. May include non-documented
/// libraries, but is not guaranteed to include a complete list of
/// non-documented libraries unless they are all referenced by documented ones.
/// Not sorted.
final Set<Library> allLibraries = {};

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

Iterable<Category> get documentedCategoriesSorted {
// Category display order is configurable; leave the category order
// as defined if the order is specified.
if (config.categoryOrder.isEmpty) {
return documentedCategories;
}
return documentedCategories.toList()..sort(byName);
}

bool get hasDocumentedCategories => documentedCategories.isNotEmpty;

DartdocOptionContext _config;
Expand Down
7 changes: 1 addition & 6 deletions lib/src/model/package_graph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer/src/generated/sdk.dart';
import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer/src/generated/source_io.dart';
import 'package:collection/collection.dart';
import 'package:dartdoc/src/dartdoc_options.dart';
import 'package:dartdoc/src/logging.dart';
import 'package:dartdoc/src/model/model.dart';
Expand Down Expand Up @@ -80,7 +79,6 @@ class PackageGraph {
// After the allModelElements traversal to be sure that all packages
// are picked up.
for (var package in documentedPackages) {
package.libraries.sort((a, b) => compareNatural(a.name, b.name));
for (var library in package.libraries) {
_addToImplementors(library.allClasses);
_extensions.addAll(library.extensions);
Expand All @@ -89,9 +87,6 @@ class PackageGraph {
package.warn(PackageWarning.noDocumentableLibrariesInPackage);
}
}
for (var l in _implementors.values) {
l.sort();
}
allImplementorsAdded = true;
allExtensionsAdded = true;

Expand Down Expand Up @@ -614,7 +609,7 @@ class PackageGraph {
}

void addImplementor(Class class_) {
for (var type in class_.mixins) {
for (var type in class_.mixedInTypes) {
checkAndAddClass(type.element, class_);
}
if (class_.supertype != null) {
Expand Down
35 changes: 35 additions & 0 deletions lib/src/model/top_level_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,59 @@ abstract class TopLevelContainer implements Nameable {

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

List<Class> _publicClassesSorted;
Iterable<Class> get publicClassesSorted =>
_publicClassesSorted ??= publicClasses.toList()..sort(byName);

Iterable<Extension> get publicExtensions =>
model_utils.filterNonPublic(extensions);

List<Extension> _publicExtensionsSorted;
Iterable<Extension> get publicExtensionsSorted =>
_publicExtensionsSorted ??= publicExtensions.toList()..sort(byName);

Iterable<TopLevelVariable> get publicConstants =>
model_utils.filterNonPublic(constants);

Iterable<TopLevelVariable> get publicConstantsSorted =>
publicConstants.toList()..sort(byName);

Iterable<Enum> get publicEnums => model_utils.filterNonPublic(enums);

List<Enum> _publicEnumsSorted;
Iterable<Enum> get publicEnumsSorted =>
_publicEnumsSorted ??= publicEnums.toList()..sort(byName);

Iterable<Class> get publicExceptions =>
model_utils.filterNonPublic(exceptions);

List<Class> _publicExceptionsSorted;
Iterable<Class> get publicExceptionsSorted =>
_publicExceptionsSorted ??= publicExceptions.toList()..sort(byName);

Iterable<ModelFunctionTyped> get publicFunctions =>
model_utils.filterNonPublic(functions);

List<ModelFunctionTyped> _publicFunctionsSorted;
Iterable<ModelFunctionTyped> get publicFunctionsSorted =>
_publicFunctionsSorted ??= publicFunctions.toList()..sort(byName);

Iterable<Mixin> get publicMixins => model_utils.filterNonPublic(mixins);

List<Mixin> _publicMixinsSorted;
Iterable<Mixin> get publicMixinsSorted =>
_publicMixinsSorted ??= publicMixins.toList()..sort(byName);

Iterable<TopLevelVariable> get publicProperties =>
model_utils.filterNonPublic(properties);

List<TopLevelVariable> _publicPropertiesSorted;
Iterable<TopLevelVariable> get publicPropertiesSorted =>
_publicPropertiesSorted ??= publicProperties.toList()..sort(byName);

Iterable<Typedef> get publicTypedefs => model_utils.filterNonPublic(typedefs);

List<Typedef> _publicTypedefsSorted;
Iterable<Typedef> get publicTypedefsSorted =>
_publicTypedefsSorted ??= publicTypedefs.toList()..sort(byName);
}
2 changes: 1 addition & 1 deletion lib/templates/html/_feature_set.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
{{#displayedLanguageFeatures}}
{{{featureLabel}}}
{{/displayedLanguageFeatures}}
{{/hasFeatureSet}}
{{/hasFeatureSet}}
12 changes: 6 additions & 6 deletions lib/templates/html/_packages.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
{{#isFirstPackage}}
{{#hasDocumentedCategories}}
<li class="section-title">Topics</li>
{{#documentedCategories}}
{{#documentedCategoriesSorted}}
<li>{{{linkedName}}}</li>
{{/documentedCategories}}
{{/documentedCategoriesSorted}}
{{/hasDocumentedCategories}}
<li class="section-title">Libraries</li>
{{/isFirstPackage}}
{{^isFirstPackage}}
<li class="section-title">{{name}}</li>
{{/isFirstPackage}}
{{#defaultCategory.publicLibraries}}
{{#defaultCategory.publicLibrariesSorted}}
<li>{{{linkedName}}}</li>
{{/defaultCategory.publicLibraries}}
{{/defaultCategory.publicLibrariesSorted}}
{{#categoriesWithPublicLibraries}}
<li class="section-subtitle">{{name}}</li>
{{#publicLibraries}}
{{#publicLibrariesSorted}}
<li class="section-subitem">{{{linkedName}}}</li>
{{/publicLibraries}}
{{/publicLibrariesSorted}}
{{/categoriesWithPublicLibraries}}
{{/localPackages}}
</ol>
Loading