Skip to content

Simplify Library.allModelElements #3793

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 1 commit into from
Jun 20, 2024
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
22 changes: 3 additions & 19 deletions lib/src/generator/templates.runtime_renderers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7988,7 +7988,7 @@ class _Renderer_Library extends RendererBase<Library> {
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(
c, remainingNames, 'Iterable<ModelElement>'),
c, remainingNames, 'List<ModelElement>'),
renderIterable: (CT_ c, RendererBase<CT_> r,
List<MustachioNode> ast, StringSink sink) {
return c.allModelElements.map((e) => _render_ModelElement(
Expand Down Expand Up @@ -8380,19 +8380,6 @@ class _Renderer_Library extends RendererBase<Library> {
_render_Mixin(e, ast, r.template, sink, parent: r));
},
),
'modelElementsMap': Property(
getValue: (CT_ c) => c.modelElementsMap,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(c, remainingNames,
'HashMap<Element, Set<ModelElement>>'),
isNullValue: (CT_ c) => false,
renderValue: (CT_ c, RendererBase<CT_> r,
List<MustachioNode> ast, StringSink sink) {
renderSimple(c.modelElementsMap, ast, r.template, sink,
parent: r, getters: _invisibleGetters['HashMap']!);
},
),
'name': Property(
getValue: (CT_ c) => c.name,
renderVariable:
Expand Down Expand Up @@ -12486,13 +12473,13 @@ class _Renderer_PackageTemplateData extends RendererBase<PackageTemplateData> {
}
}

String renderSearchPage(PackageTemplateData context, Template template) {
String renderIndex(PackageTemplateData context, Template template) {
var buffer = StringBuffer();
_render_PackageTemplateData(context, template.ast, template, buffer);
return buffer.toString();
}

String renderIndex(PackageTemplateData context, Template template) {
String renderSearchPage(PackageTemplateData context, Template template) {
var buffer = StringBuffer();
_render_PackageTemplateData(context, template.ast, template, buffer);
return buffer.toString();
Expand Down Expand Up @@ -16162,7 +16149,6 @@ const _invisibleGetters = {
'setter',
'writeOnly'
},
'HashMap': {'hashCode', 'runtimeType'},
'Inheritable': {
'attributes',
'canonicalLibrary',
Expand Down Expand Up @@ -16317,7 +16303,6 @@ const _invisibleGetters = {
'allInheritableElements',
'allLibraries',
'allLibrariesAdded',
'allLocalModelElements',
'analysisContext',
'breadcrumbName',
'config',
Expand Down Expand Up @@ -16498,7 +16483,6 @@ const _invisibleGetters = {
'aliasedType',
'enclosingElement',
'hashCode',
'isAugmentation',
'name',
'runtimeType'
},
Expand Down
62 changes: 26 additions & 36 deletions lib/src/model/library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// 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 'dart:collection';

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/scope.dart';
import 'package:analyzer/source/line_info.dart';
Expand Down Expand Up @@ -361,34 +359,27 @@ class Library extends ModelElement
return variables;
}

/// A mapping of all [Element]s in this library to the [ModelElement]s which
/// represent them in dartdoc.
/// All [ModelElement]s, direct and indirect, which are part of this library's
/// export namespace.
// Note: Keep this a late final field; converting to a getter (without further
// investigation) causes dartdoc to hang.
late final HashMap<Element, Set<ModelElement>> modelElementsMap = () {
var modelElements = HashMap<Element, Set<ModelElement>>();
for (var modelElement in <ModelElement>[
...constants,
...functions,
...properties,
...typedefs,
...extensions.expand((e) => [e, ...e.allModelElements]),
...extensionTypes.expand((e) => [e, ...e.allModelElements]),
...classesAndExceptions.expand((c) => [c, ...c.allModelElements]),
...enums.expand((e) => [e, ...e.allModelElements]),
...mixins.expand((m) => [m, ...m.allModelElements]),
]) {
modelElements
.putIfAbsent(modelElement.element, () => {})
.add(modelElement);
}
modelElements.putIfAbsent(element, () => {}).add(this);
return modelElements;
}();

Iterable<ModelElement> get allModelElements => [
for (var modelElements in modelElementsMap.values) ...modelElements,
];
late final List<ModelElement> allModelElements = [
...constants,
...functions,
...properties,
...typedefs,
...extensions,
for (var e in extensions) ...e.allModelElements,
...extensionTypes,
for (var e in extensionTypes) ...e.allModelElements,
...classesAndExceptions,
for (var c in classesAndExceptions) ...c.allModelElements,
...enums,
for (var e in enums) ...e.allModelElements,
...mixins,
for (var m in mixins) ...m.allModelElements,
this,
];

@override
Map<String, CommentReferable> get referenceChildren {
Expand Down Expand Up @@ -417,13 +408,11 @@ class Library extends ModelElement
@override
String buildDocumentationAddition(String rawDocs) {
rawDocs = super.buildDocumentationAddition(rawDocs);
var notFoundInAllModelElements = <String>{};
var elementNames = _allOriginalModelElementNames;
for (var elementName in canonicalFor) {
if (!elementNames.contains(elementName)) {
notFoundInAllModelElements.add(elementName);
}
}
var notFoundInAllModelElements = {
for (var elementName in canonicalFor)
if (!elementNames.contains(elementName)) elementName,
};
for (var notFound in notFoundInAllModelElements) {
warn(PackageWarning.ignoredCanonicalFor, message: notFound);
}
Expand All @@ -432,12 +421,13 @@ class Library extends ModelElement
return rawDocs;
}

/// [allModelElements] resolved to their original names.
/// The immediate elements of this library, resolved to their original names.
///
/// A collection of [ModelElement.fullyQualifiedName]s for [ModelElement]s
/// documented with this library, but these ModelElements and names correspond
/// to the defining library where each originally came from with respect
/// to inheritance and re-exporting. Used for error reporting.
/// to inheritance and re-exporting. Only used for reporting
/// [PackageWarning.ignoredCanonicalFor].
late final Set<String> _allOriginalModelElementNames = () {
// Instead of using `allModelElements`, which includes deeper elements like
// methods on classes, gather up only the library's immediate members.
Expand Down
12 changes: 3 additions & 9 deletions lib/src/model/package_graph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -544,10 +544,9 @@ class PackageGraph with CommentReferable, Nameable {
}

/// A lookup index for hrefs to allow warnings to indicate where a broken
/// link or orphaned file may have come from. Not cached because
/// [ModelElement]s can be created at any time and we're basing this
/// on more than just [allLocalModelElements] to make the error messages
/// comprehensive.
/// link or orphaned file may have come from.
///
/// This is not cached because [ModelElement]s can be created at any time.
Map<String, Set<ModelElement>> get allHrefs {
final hrefMap = <String, Set<ModelElement>>{};
// TODO(jcollins-g ): handle calculating hrefs causing new elements better
Expand Down Expand Up @@ -895,11 +894,6 @@ class PackageGraph with CommentReferable, Nameable {
return allElements;
}

@visibleForTesting
late final List<ModelElement> allLocalModelElements = [
for (var library in _localLibraries) ...library.allModelElements
];

/// Glob lookups can be expensive. Cache per filename.
final _configSetsNodocFor = HashMap<String, bool>();

Expand Down
8 changes: 6 additions & 2 deletions test/end2end/model_special_cases_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ void main() {
classWithHtml = exLibrary.classes.named('SanitizableHtml');
blockHtml = classWithHtml.instanceMethods.named('blockHtml');
inlineHtml = classWithHtml.instanceMethods.named('inlineHtml');
for (var modelElement in packageGraph.allLocalModelElements) {
for (var modelElement in packageGraph.localPublicLibraries
.expand((l) => l.allModelElements)) {
// Accessing this getter triggers documentation-processing.
modelElement.documentation;
}
});
Expand Down Expand Up @@ -270,7 +272,9 @@ void main() {
htmlInjection.instanceMethods.named('injectSimpleHtml');
injectHtmlFromTool =
htmlInjection.instanceMethods.named('injectHtmlFromTool');
for (var modelElement in injectionPackageGraph.allLocalModelElements) {
for (var modelElement in injectionPackageGraph.localPublicLibraries
.expand((l) => l.allModelElements)) {
// Accessing this getter triggers documentation-processing.
modelElement.documentation;
}
});
Expand Down
4 changes: 3 additions & 1 deletion test/end2end/model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,9 @@ void main() async {
.named('invokeToolParentDoc');
invokeToolParentDocOriginal =
ImplementingClassForTool.instanceMethods.named('invokeToolParentDoc');
for (var modelElement in packageGraph.allLocalModelElements) {
for (var modelElement in packageGraph.localPublicLibraries
.expand((l) => l.allModelElements)) {
// Accessing this getter triggers documentation-processing.
modelElement.documentation;
}
});
Expand Down
7 changes: 2 additions & 5 deletions test/options_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import 'package:args/args.dart';
import 'package:dartdoc/src/dartdoc_options.dart';
import 'package:dartdoc/src/failure.dart';
import 'package:dartdoc/src/model/model.dart';
import 'package:path/path.dart' as path;
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
Expand Down Expand Up @@ -258,10 +257,8 @@ class Foo {}
packageMetaProvider,
packageConfigProvider,
);
final classFoo = packageGraph.allLocalModelElements
.where((e) => e.isCanonical)
.whereType<Class>()
.firstWhere((c) => c.name == 'Foo');
final classFoo =
packageGraph.localPackages.first.libraries.first.classes.named('Foo');
expect(classFoo.displayedCategories, isNotEmpty);
}

Expand Down
11 changes: 1 addition & 10 deletions test/search_index_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,7 @@ class SearchIndexTest extends DartdocTestBase {
.where((l) => l.name.startsWith('dart:')),
];
var text = generateSearchIndexJson(
[
// For each library, gather the elements for the library, it's members,
// and the members of its container members.
for (var library in libraries) ...[
library,
...library.allModelElements,
for (var element in library.allModelElements.whereType<Container>())
...element.allModelElements,
],
],
libraries.expand((library) => library.allModelElements),
packageOrder: actAsFlutter ? const ['flutter', 'Dart'] : const [],
pretty: false,
);
Expand Down