diff --git a/lib/src/model/library.dart b/lib/src/model/library.dart index e3882921b7..02f0e16c09 100644 --- a/lib/src/model/library.dart +++ b/lib/src/model/library.dart @@ -8,7 +8,6 @@ import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/type_system.dart'; import 'package:analyzer/dart/element/visitor.dart'; import 'package:analyzer/source/line_info.dart'; -import 'package:analyzer/src/dart/analysis/driver.dart'; import 'package:analyzer/src/dart/element/inheritance_manager3.dart'; import 'package:analyzer/src/generated/sdk.dart'; import 'package:dartdoc/src/model/model.dart'; @@ -56,20 +55,22 @@ class Library extends ModelElement with Categorization, TopLevelContainer { List _variables; List _exportedAndLocalElements; String _name; + final String _restoredUri; factory Library(LibraryElement element, PackageGraph packageGraph) { return packageGraph.findButDoNotCreateLibraryFor(element); } - Library.fromLibraryResult(ResolvedLibraryResult libraryResult, + Library.fromLibraryResult(DartDocResolvedLibrary resolvedLibrary, PackageGraph packageGraph, this._package) - : super(libraryResult.element, null, packageGraph, null) { + : _restoredUri = resolvedLibrary.restoredUri, + super(resolvedLibrary.result.element, null, packageGraph, null) { if (element == null) throw ArgumentError.notNull('element'); // Initialize [packageGraph]'s cache of ModelNodes for relevant // elements in this library. var _compilationUnitMap = {}; - _compilationUnitMap.addEntries(libraryResult.units + _compilationUnitMap.addEntries(resolvedLibrary.result.units .map((ResolvedUnitResult u) => MapEntry(u.path, u.unit))); _HashableChildLibraryElementVisitor((Element e) => packageGraph.populateModelNodeFor(e, _compilationUnitMap)) @@ -414,7 +415,7 @@ class Library extends ModelElement with Categorization, TopLevelContainer { /// 'lib') are the same, but this will include slashes and possibly colons /// for anonymous libraries in subdirectories or other packages. String get nameFromPath { - _nameFromPath ??= getNameFromPath(element, packageGraph.driver, package); + _nameFromPath ??= _getNameFromPath(element, package, _restoredUri); return _nameFromPath; } @@ -502,14 +503,9 @@ class Library extends ModelElement with Categorization, TopLevelContainer { /// path components; this function only strips the package prefix if the /// library is part of the default package or if it is being documented /// remotely. - static String getNameFromPath( - LibraryElement element, AnalysisDriver driver, Package package) { - String name; - if (element.source.uri.toString().startsWith('dart:')) { - name = element.source.uri.toString(); - } else { - name = driver.sourceFactory.restoreUri(element.source).toString(); - } + static String _getNameFromPath( + LibraryElement element, Package package, String restoredUri) { + var name = restoredUri; PackageMeta hidePackage; if (package.documentedWhere == DocumentLocation.remote) { hidePackage = package.packageMeta; diff --git a/lib/src/model/package_builder.dart b/lib/src/model/package_builder.dart index baa580bfaf..ee0a67b636 100644 --- a/lib/src/model/package_builder.dart +++ b/lib/src/model/package_builder.dart @@ -55,7 +55,7 @@ class PackageBuilder { var rendererFactory = RendererFactory.forFormat(config.format); var newGraph = PackageGraph.UninitializedPackageGraph( - config, driver, sdk, hasEmbedderSdkFiles, rendererFactory); + config, sdk, hasEmbedderSdkFiles, rendererFactory); await getLibraries(newGraph); await newGraph.initializePackageGraph(); return newGraph; @@ -187,7 +187,7 @@ class PackageBuilder { /// Parse a single library at [filePath] using the current analysis driver. /// If [filePath] is not a library, returns null. - Future processLibrary(String filePath) async { + Future processLibrary(String filePath) async { var name = filePath; if (name.startsWith(directoryCurrentPath)) { @@ -215,7 +215,15 @@ class PackageBuilder { if (sourceKind != SourceKind.PART) { // Loading libraryElements from part files works, but is painfully slow // and creates many duplicates. - return await driver.currentSession.getResolvedLibrary(source.fullName); + final library = + await driver.currentSession.getResolvedLibrary(source.fullName); + final libraryElement = library.element; + var restoredUri = libraryElement.source.uri.toString(); + if (!restoredUri.startsWith('dart:')) { + restoredUri = + driver.sourceFactory.restoreUri(library.element.source).toString(); + } + return DartDocResolvedLibrary(library, restoredUri); } return null; } @@ -235,7 +243,7 @@ class PackageBuilder { /// the callback more than once with the same [LibraryElement]. /// Adds [LibraryElement]s found to that parameter. Future _parseLibraries( - void Function(ResolvedLibraryResult) libraryAdder, + void Function(DartDocResolvedLibrary) libraryAdder, Set libraries, Set files, [bool Function(LibraryElement) isLibraryIncluded]) async { @@ -246,7 +254,7 @@ class PackageBuilder { lastPass = _packageMetasForFiles(files); // Be careful here not to accidentally stack up multiple - // ResolvedLibraryResults, as those eat our heap. + // DartDocResolvedLibrarys, as those eat our heap. for (var f in files) { logProgress(f); var r = await processLibrary(f); @@ -461,3 +469,20 @@ class PackageWithoutSdkResolver extends UriResolver { return null; } } + +/// Contains the [ResolvedLibraryResult] and any additional information about +/// the library coming from [AnalysisDriver]. +/// +/// Prefer to populate this class with more information rather than passing +/// [AnalysisDriver] or [AnalysisSession] down to [PackageGraph]. The graph +/// object is reachable by many DartDoc model objects and there's no guarantee +/// that there's a valid [AnalysisDriver] in every environment dartdoc runs. +class DartDocResolvedLibrary { + final ResolvedLibraryResult result; + final String restoredUri; + + DartDocResolvedLibrary(this.result, this.restoredUri); + + LibraryElement get element => result.element; + LibraryElement get library => result.element.library; +} diff --git a/lib/src/model/package_graph.dart b/lib/src/model/package_graph.dart index 518d254f47..fbb966e8dc 100644 --- a/lib/src/model/package_graph.dart +++ b/lib/src/model/package_graph.dart @@ -4,11 +4,8 @@ import 'dart:async'; -import 'package:analyzer/dart/analysis/results.dart'; -import 'package:analyzer/dart/analysis/session.dart'; import 'package:analyzer/dart/ast/ast.dart'; import 'package:analyzer/dart/element/element.dart'; -import 'package:analyzer/src/dart/analysis/driver.dart'; import 'package:analyzer/src/generated/sdk.dart'; import 'package:analyzer/src/generated/source.dart'; import 'package:analyzer/src/generated/source_io.dart'; @@ -24,10 +21,9 @@ import 'package:dartdoc/src/tuple.dart'; import 'package:dartdoc/src/warnings.dart'; class PackageGraph { - PackageGraph.UninitializedPackageGraph(this.config, this.driver, this.sdk, - this.hasEmbedderSdk, this.rendererFactory) - : packageMeta = config.topLevelPackageMeta, - session = driver.currentSession { + PackageGraph.UninitializedPackageGraph( + this.config, this.sdk, this.hasEmbedderSdk, this.rendererFactory) + : packageMeta = config.topLevelPackageMeta { _packageWarningCounter = PackageWarningCounter(this); // Make sure the default package exists, even if it has no libraries. // This can happen for packages that only contain embedder SDKs. @@ -39,12 +35,12 @@ class PackageGraph { /// Libraries added in this manner are assumed to be part of documented /// packages, even if includes or embedder.yaml files cause these to /// span packages. - void addLibraryToGraph(ResolvedLibraryResult result) { + void addLibraryToGraph(DartDocResolvedLibrary resolvedLibrary) { assert(!allLibrariesAdded); - var element = result.element; + var element = resolvedLibrary.element; var packageMeta = PackageMeta.fromElement(element, config.sdkDir); var lib = Library.fromLibraryResult( - result, this, Package.fromPackageMeta(packageMeta, this)); + resolvedLibrary, this, Package.fromPackageMeta(packageMeta, this)); packageMap[packageMeta.name].libraries.add(lib); allLibraries[element] = lib; } @@ -52,10 +48,10 @@ class PackageGraph { /// Call during initialization to add a library possibly containing /// special/non-documented elements to this [PackageGraph]. Must be called /// after any normal libraries. - void addSpecialLibraryToGraph(ResolvedLibraryResult result) { + void addSpecialLibraryToGraph(DartDocResolvedLibrary resolvedLibrary) { allLibrariesAdded = true; assert(!_localDocumentationBuilt); - findOrCreateLibraryFor(result); + findOrCreateLibraryFor(resolvedLibrary); } /// Call after all libraries are added. @@ -234,9 +230,6 @@ class PackageGraph { /// Map of package name to Package. final Map packageMap = {}; - /// TODO(brianwilkerson) Replace the driver with the session. - final AnalysisDriver driver; - final AnalysisSession session; final DartSdk sdk; Map _sdkLibrarySources; @@ -829,22 +822,23 @@ class PackageGraph { /// This is used when we might need a Library object that isn't actually /// a documentation entry point (for elements that have no Library within the /// set of canonical Libraries). - Library findOrCreateLibraryFor(ResolvedLibraryResult result) { + Library findOrCreateLibraryFor(DartDocResolvedLibrary resolvedLibrary) { + final elementLibrary = resolvedLibrary.library; // This is just a cache to avoid creating lots of libraries over and over. - if (allLibraries.containsKey(result.element.library)) { - return allLibraries[result.element.library]; + if (allLibraries.containsKey(elementLibrary)) { + return allLibraries[elementLibrary]; } // can be null if e is for dynamic - if (result.element.library == null) { + if (elementLibrary == null) { return null; } var foundLibrary = Library.fromLibraryResult( - result, + resolvedLibrary, this, Package.fromPackageMeta( - PackageMeta.fromElement(result.element.library, config.sdkDir), + PackageMeta.fromElement(elementLibrary, config.sdkDir), packageGraph)); - allLibraries[result.element.library] = foundLibrary; + allLibraries[elementLibrary] = foundLibrary; return foundLibrary; }