Skip to content

Commit 1196ef0

Browse files
authored
Remove driver and session from PackageGraph (#2204)
1 parent e93f435 commit 1196ef0

File tree

3 files changed

+55
-40
lines changed

3 files changed

+55
-40
lines changed

lib/src/model/library.dart

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import 'package:analyzer/dart/element/element.dart';
88
import 'package:analyzer/dart/element/type_system.dart';
99
import 'package:analyzer/dart/element/visitor.dart';
1010
import 'package:analyzer/source/line_info.dart';
11-
import 'package:analyzer/src/dart/analysis/driver.dart';
1211
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
1312
import 'package:analyzer/src/generated/sdk.dart';
1413
import 'package:dartdoc/src/model/model.dart';
@@ -56,20 +55,22 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
5655
List<TopLevelVariable> _variables;
5756
List<Element> _exportedAndLocalElements;
5857
String _name;
58+
final String _restoredUri;
5959

6060
factory Library(LibraryElement element, PackageGraph packageGraph) {
6161
return packageGraph.findButDoNotCreateLibraryFor(element);
6262
}
6363

64-
Library.fromLibraryResult(ResolvedLibraryResult libraryResult,
64+
Library.fromLibraryResult(DartDocResolvedLibrary resolvedLibrary,
6565
PackageGraph packageGraph, this._package)
66-
: super(libraryResult.element, null, packageGraph, null) {
66+
: _restoredUri = resolvedLibrary.restoredUri,
67+
super(resolvedLibrary.result.element, null, packageGraph, null) {
6768
if (element == null) throw ArgumentError.notNull('element');
6869

6970
// Initialize [packageGraph]'s cache of ModelNodes for relevant
7071
// elements in this library.
7172
var _compilationUnitMap = <String, CompilationUnit>{};
72-
_compilationUnitMap.addEntries(libraryResult.units
73+
_compilationUnitMap.addEntries(resolvedLibrary.result.units
7374
.map((ResolvedUnitResult u) => MapEntry(u.path, u.unit)));
7475
_HashableChildLibraryElementVisitor((Element e) =>
7576
packageGraph.populateModelNodeFor(e, _compilationUnitMap))
@@ -414,7 +415,7 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
414415
/// 'lib') are the same, but this will include slashes and possibly colons
415416
/// for anonymous libraries in subdirectories or other packages.
416417
String get nameFromPath {
417-
_nameFromPath ??= getNameFromPath(element, packageGraph.driver, package);
418+
_nameFromPath ??= _getNameFromPath(element, package, _restoredUri);
418419
return _nameFromPath;
419420
}
420421

@@ -502,14 +503,9 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
502503
/// path components; this function only strips the package prefix if the
503504
/// library is part of the default package or if it is being documented
504505
/// remotely.
505-
static String getNameFromPath(
506-
LibraryElement element, AnalysisDriver driver, Package package) {
507-
String name;
508-
if (element.source.uri.toString().startsWith('dart:')) {
509-
name = element.source.uri.toString();
510-
} else {
511-
name = driver.sourceFactory.restoreUri(element.source).toString();
512-
}
506+
static String _getNameFromPath(
507+
LibraryElement element, Package package, String restoredUri) {
508+
var name = restoredUri;
513509
PackageMeta hidePackage;
514510
if (package.documentedWhere == DocumentLocation.remote) {
515511
hidePackage = package.packageMeta;

lib/src/model/package_builder.dart

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class PackageBuilder {
5555
var rendererFactory = RendererFactory.forFormat(config.format);
5656

5757
var newGraph = PackageGraph.UninitializedPackageGraph(
58-
config, driver, sdk, hasEmbedderSdkFiles, rendererFactory);
58+
config, sdk, hasEmbedderSdkFiles, rendererFactory);
5959
await getLibraries(newGraph);
6060
await newGraph.initializePackageGraph();
6161
return newGraph;
@@ -187,7 +187,7 @@ class PackageBuilder {
187187

188188
/// Parse a single library at [filePath] using the current analysis driver.
189189
/// If [filePath] is not a library, returns null.
190-
Future<ResolvedLibraryResult> processLibrary(String filePath) async {
190+
Future<DartDocResolvedLibrary> processLibrary(String filePath) async {
191191
var name = filePath;
192192

193193
if (name.startsWith(directoryCurrentPath)) {
@@ -215,7 +215,15 @@ class PackageBuilder {
215215
if (sourceKind != SourceKind.PART) {
216216
// Loading libraryElements from part files works, but is painfully slow
217217
// and creates many duplicates.
218-
return await driver.currentSession.getResolvedLibrary(source.fullName);
218+
final library =
219+
await driver.currentSession.getResolvedLibrary(source.fullName);
220+
final libraryElement = library.element;
221+
var restoredUri = libraryElement.source.uri.toString();
222+
if (!restoredUri.startsWith('dart:')) {
223+
restoredUri =
224+
driver.sourceFactory.restoreUri(library.element.source).toString();
225+
}
226+
return DartDocResolvedLibrary(library, restoredUri);
219227
}
220228
return null;
221229
}
@@ -235,7 +243,7 @@ class PackageBuilder {
235243
/// the callback more than once with the same [LibraryElement].
236244
/// Adds [LibraryElement]s found to that parameter.
237245
Future<void> _parseLibraries(
238-
void Function(ResolvedLibraryResult) libraryAdder,
246+
void Function(DartDocResolvedLibrary) libraryAdder,
239247
Set<LibraryElement> libraries,
240248
Set<String> files,
241249
[bool Function(LibraryElement) isLibraryIncluded]) async {
@@ -246,7 +254,7 @@ class PackageBuilder {
246254
lastPass = _packageMetasForFiles(files);
247255

248256
// Be careful here not to accidentally stack up multiple
249-
// ResolvedLibraryResults, as those eat our heap.
257+
// DartDocResolvedLibrarys, as those eat our heap.
250258
for (var f in files) {
251259
logProgress(f);
252260
var r = await processLibrary(f);
@@ -461,3 +469,20 @@ class PackageWithoutSdkResolver extends UriResolver {
461469
return null;
462470
}
463471
}
472+
473+
/// Contains the [ResolvedLibraryResult] and any additional information about
474+
/// the library coming from [AnalysisDriver].
475+
///
476+
/// Prefer to populate this class with more information rather than passing
477+
/// [AnalysisDriver] or [AnalysisSession] down to [PackageGraph]. The graph
478+
/// object is reachable by many DartDoc model objects and there's no guarantee
479+
/// that there's a valid [AnalysisDriver] in every environment dartdoc runs.
480+
class DartDocResolvedLibrary {
481+
final ResolvedLibraryResult result;
482+
final String restoredUri;
483+
484+
DartDocResolvedLibrary(this.result, this.restoredUri);
485+
486+
LibraryElement get element => result.element;
487+
LibraryElement get library => result.element.library;
488+
}

lib/src/model/package_graph.dart

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44

55
import 'dart:async';
66

7-
import 'package:analyzer/dart/analysis/results.dart';
8-
import 'package:analyzer/dart/analysis/session.dart';
97
import 'package:analyzer/dart/ast/ast.dart';
108
import 'package:analyzer/dart/element/element.dart';
11-
import 'package:analyzer/src/dart/analysis/driver.dart';
129
import 'package:analyzer/src/generated/sdk.dart';
1310
import 'package:analyzer/src/generated/source.dart';
1411
import 'package:analyzer/src/generated/source_io.dart';
@@ -24,10 +21,9 @@ import 'package:dartdoc/src/tuple.dart';
2421
import 'package:dartdoc/src/warnings.dart';
2522

2623
class PackageGraph {
27-
PackageGraph.UninitializedPackageGraph(this.config, this.driver, this.sdk,
28-
this.hasEmbedderSdk, this.rendererFactory)
29-
: packageMeta = config.topLevelPackageMeta,
30-
session = driver.currentSession {
24+
PackageGraph.UninitializedPackageGraph(
25+
this.config, this.sdk, this.hasEmbedderSdk, this.rendererFactory)
26+
: packageMeta = config.topLevelPackageMeta {
3127
_packageWarningCounter = PackageWarningCounter(this);
3228
// Make sure the default package exists, even if it has no libraries.
3329
// This can happen for packages that only contain embedder SDKs.
@@ -39,23 +35,23 @@ class PackageGraph {
3935
/// Libraries added in this manner are assumed to be part of documented
4036
/// packages, even if includes or embedder.yaml files cause these to
4137
/// span packages.
42-
void addLibraryToGraph(ResolvedLibraryResult result) {
38+
void addLibraryToGraph(DartDocResolvedLibrary resolvedLibrary) {
4339
assert(!allLibrariesAdded);
44-
var element = result.element;
40+
var element = resolvedLibrary.element;
4541
var packageMeta = PackageMeta.fromElement(element, config.sdkDir);
4642
var lib = Library.fromLibraryResult(
47-
result, this, Package.fromPackageMeta(packageMeta, this));
43+
resolvedLibrary, this, Package.fromPackageMeta(packageMeta, this));
4844
packageMap[packageMeta.name].libraries.add(lib);
4945
allLibraries[element] = lib;
5046
}
5147

5248
/// Call during initialization to add a library possibly containing
5349
/// special/non-documented elements to this [PackageGraph]. Must be called
5450
/// after any normal libraries.
55-
void addSpecialLibraryToGraph(ResolvedLibraryResult result) {
51+
void addSpecialLibraryToGraph(DartDocResolvedLibrary resolvedLibrary) {
5652
allLibrariesAdded = true;
5753
assert(!_localDocumentationBuilt);
58-
findOrCreateLibraryFor(result);
54+
findOrCreateLibraryFor(resolvedLibrary);
5955
}
6056

6157
/// Call after all libraries are added.
@@ -234,9 +230,6 @@ class PackageGraph {
234230
/// Map of package name to Package.
235231
final Map<String, Package> packageMap = {};
236232

237-
/// TODO(brianwilkerson) Replace the driver with the session.
238-
final AnalysisDriver driver;
239-
final AnalysisSession session;
240233
final DartSdk sdk;
241234

242235
Map<Source, SdkLibrary> _sdkLibrarySources;
@@ -842,22 +835,23 @@ class PackageGraph {
842835
/// This is used when we might need a Library object that isn't actually
843836
/// a documentation entry point (for elements that have no Library within the
844837
/// set of canonical Libraries).
845-
Library findOrCreateLibraryFor(ResolvedLibraryResult result) {
838+
Library findOrCreateLibraryFor(DartDocResolvedLibrary resolvedLibrary) {
839+
final elementLibrary = resolvedLibrary.library;
846840
// This is just a cache to avoid creating lots of libraries over and over.
847-
if (allLibraries.containsKey(result.element.library)) {
848-
return allLibraries[result.element.library];
841+
if (allLibraries.containsKey(elementLibrary)) {
842+
return allLibraries[elementLibrary];
849843
}
850844
// can be null if e is for dynamic
851-
if (result.element.library == null) {
845+
if (elementLibrary == null) {
852846
return null;
853847
}
854848
var foundLibrary = Library.fromLibraryResult(
855-
result,
849+
resolvedLibrary,
856850
this,
857851
Package.fromPackageMeta(
858-
PackageMeta.fromElement(result.element.library, config.sdkDir),
852+
PackageMeta.fromElement(elementLibrary, config.sdkDir),
859853
packageGraph));
860-
allLibraries[result.element.library] = foundLibrary;
854+
allLibraries[elementLibrary] = foundLibrary;
861855
return foundLibrary;
862856
}
863857

0 commit comments

Comments
 (0)