Skip to content

Refactor some code in PackageGraph #3570

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
Nov 6, 2023
Merged
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
55 changes: 23 additions & 32 deletions lib/src/model/package_graph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,23 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {
void addSpecialLibraryToGraph(DartDocResolvedLibrary resolvedLibrary) {
allLibrariesAdded = true;
assert(!_localDocumentationBuilt);
findOrCreateLibraryFor(resolvedLibrary);
final libraryElement = resolvedLibrary.element.library;
allLibraries.putIfAbsent(
libraryElement.source.fullName,
() => Library.fromLibraryResult(
resolvedLibrary,
this,
Package.fromPackageMeta(
packageMetaProvider.fromElement(libraryElement, config.sdkDir)!,
packageGraph),
),
);
}

/// Call after all libraries are added.
Future<void> initializePackageGraph() async {
assert(!_localDocumentationBuilt);
allLibrariesAdded = true;
// From here on in, we might find special objects. Initialize the
// specialClasses handler so when we find them, they get added.
specialClasses = SpecialClasses();
// Go through docs of every ModelElement in package to pre-build the macros
// index.
await Future.wait(_precacheLocalDocs());
Expand Down Expand Up @@ -137,9 +144,10 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {
}

/// Generate a list of futures for any docs that actually require precaching.
Iterable<Future<void>> _precacheLocalDocs() sync* {
Iterable<Future<void>> _precacheLocalDocs() {
// Prevent reentrancy.
var precachedElements = <ModelElement>{};
var futures = <Future<void>>[];

for (var element in _allModelElements) {
// Only precache elements which are canonical, have a canonical element
Expand All @@ -153,22 +161,23 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {
.where((d) => d.hasDocumentationComment)) {
if (d.needsPrecache && !precachedElements.contains(d)) {
precachedElements.add(d as ModelElement);
yield d.precacheLocalDocs();
futures.add(d.precacheLocalDocs());
logProgress(d.name);
// [TopLevelVariable]s get their documentation from getters and
// setters, so should be precached if either has a template.
if (element is TopLevelVariable &&
!precachedElements.contains(element)) {
precachedElements.add(element);
yield element.precacheLocalDocs();
futures.add(element.precacheLocalDocs());
logProgress(d.name);
}
}
}
}
}
// Now wait for any of the tasks still running to complete.
yield config.tools.runner.wait();
futures.add(config.tools.runner.wait());
return futures;
}

/// Initializes the category mappings in all [packages].
Expand All @@ -178,10 +187,13 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {
}
}

// Many ModelElements have the same ModelNode; don't build/cache this data more
// than once for them.
// Many ModelElements have the same ModelNode; don't build/cache this data
// more than once for them.
final Map<Element, ModelNode> _modelNodes = {};

/// The collection of "special" classes for which we need some special access.
final specialClasses = SpecialClasses();

/// Populate's [_modelNodes] with elements in [resolvedLibrary].
///
/// This is done as [Library] model objects are created, while we are holding
Expand Down Expand Up @@ -253,8 +265,6 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {

ModelNode? getModelNodeFor(Element element) => _modelNodes[element];

late SpecialClasses specialClasses;

/// It is safe to cache values derived from the [_implementors] table if this
/// is true.
bool allImplementorsAdded = false;
Expand Down Expand Up @@ -639,8 +649,7 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {
?.linkedName ??
'Object';

/// Return the set of [Class]es which objects should inherit through if they
/// show up in the inheritance chain.
/// The set of [Class]es which should _not_ be presented as implementors.
///
/// Add classes here if they are similar to Interceptor in that they are to be
/// ignored even when they are the implementors of [Inheritable]s, and the
Expand Down Expand Up @@ -856,24 +865,6 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {
return allLibraries[e.library?.source.fullName];
}

/// This is used when we might need a [Library] that isn't actually a
/// documentation entry point (for elements that have no [Library] within the
/// set of canonical libraries).
Library findOrCreateLibraryFor(DartDocResolvedLibrary resolvedLibrary) {
final libraryElement = resolvedLibrary.element.library;
var foundLibrary = findButDoNotCreateLibraryFor(libraryElement);
if (foundLibrary != null) return foundLibrary;

foundLibrary = Library.fromLibraryResult(
resolvedLibrary,
this,
Package.fromPackageMeta(
packageMetaProvider.fromElement(libraryElement, config.sdkDir)!,
packageGraph));
allLibraries[libraryElement.source.fullName] = foundLibrary;
return foundLibrary;
}

late final Iterable<ModelElement> _allModelElements = () {
assert(allLibrariesAdded);
var allElements = <ModelElement>[];
Expand Down