@@ -10,6 +10,8 @@ import 'dart:collection' show UnmodifiableListView;
10
10
import 'dart:convert' ;
11
11
import 'dart:io' ;
12
12
13
+ import 'package:analyzer/dart/analysis/results.dart' ;
14
+ import 'package:analyzer/dart/analysis/session.dart' ;
13
15
import 'package:analyzer/dart/ast/ast.dart'
14
16
show
15
17
AnnotatedNode,
@@ -437,7 +439,7 @@ class Accessor extends ModelElement implements EnclosedElement {
437
439
if (_sourceCode == null ) {
438
440
if (isSynthetic) {
439
441
_sourceCode = packageGraph
440
- ._getModelNodeFor ((element as PropertyAccessorElement ).variable)
442
+ ._getModelNodeFor ((element as PropertyAccessorElement ).variable, this .definingLibrary )
441
443
.sourceCode;
442
444
} else {
443
445
_sourceCode = super .sourceCode;
@@ -495,8 +497,8 @@ class Accessor extends ModelElement implements EnclosedElement {
495
497
@override
496
498
ModelElement get enclosingElement {
497
499
if (_accessor.enclosingElement is CompilationUnitElement ) {
498
- return packageGraph
499
- . findOrCreateLibraryFor ( _accessor.enclosingElement.enclosingElement);
500
+ return packageGraph. findButDoNotCreateLibraryFor (
501
+ _accessor.enclosingElement.enclosingElement);
500
502
}
501
503
502
504
return new ModelElement .from (
@@ -2059,16 +2061,17 @@ abstract class GetterSetterCombo implements ModelElement {
2059
2061
}
2060
2062
2061
2063
class Library extends ModelElement with Categorization , TopLevelContainer {
2064
+ final ResolvedLibraryResult libraryResult;
2062
2065
List <TopLevelVariable > _variables;
2063
2066
Namespace _exportedNamespace;
2064
2067
String _name;
2065
2068
2066
2069
factory Library (LibraryElement element, PackageGraph packageGraph) {
2067
- return packageGraph.findOrCreateLibraryFor (element);
2070
+ return packageGraph.findButDoNotCreateLibraryFor (element);
2068
2071
}
2069
2072
2070
- Library ._(LibraryElement element , PackageGraph packageGraph, this ._package)
2071
- : super (element, null , packageGraph, null ) {
2073
+ Library ._(this .libraryResult , PackageGraph packageGraph, this ._package)
2074
+ : super (libraryResult. element, null , packageGraph, null ) {
2072
2075
if (element == null ) throw new ArgumentError .notNull ('element' );
2073
2076
_exportedNamespace =
2074
2077
new NamespaceBuilder ().createExportNamespaceForLibrary (element);
@@ -2107,9 +2110,12 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
2107
2110
new ModelElement .fromElement (e.setter.element, packageGraph);
2108
2111
}
2109
2112
}
2110
- return new ModelElement .from (e.element,
2111
- packageGraph.findOrCreateLibraryFor (e.element), packageGraph,
2112
- getter: getter, setter: setter)
2113
+ return new ModelElement .from (
2114
+ e.element,
2115
+ packageGraph.findButDoNotCreateLibraryFor (e.element),
2116
+ packageGraph,
2117
+ getter: getter,
2118
+ setter: setter)
2113
2119
.fullyQualifiedName;
2114
2120
}).toList ();
2115
2121
}
@@ -2894,7 +2900,7 @@ abstract class ModelElement extends Canonicalization
2894
2900
this ._element, this ._library, this ._packageGraph, this ._originalMember) {}
2895
2901
2896
2902
factory ModelElement .fromElement (Element e, PackageGraph p) {
2897
- Library lib = p.findOrCreateLibraryFor (e);
2903
+ Library lib = p.findButDoNotCreateLibraryFor (e);
2898
2904
Accessor getter;
2899
2905
Accessor setter;
2900
2906
if (e is PropertyInducingElement ) {
@@ -3099,7 +3105,7 @@ abstract class ModelElement extends Canonicalization
3099
3105
ModelNode _modelNode;
3100
3106
@override
3101
3107
ModelNode get modelNode =>
3102
- _modelNode ?? = packageGraph._getModelNodeFor (element);
3108
+ _modelNode ?? = packageGraph._getModelNodeFor (element, definingLibrary );
3103
3109
3104
3110
List <String > get annotations => annotationsFromMetadata (element.metadata);
3105
3111
@@ -3344,7 +3350,8 @@ abstract class ModelElement extends Canonicalization
3344
3350
documentationFrom.map ((e) => e.documentationLocal).join ('<p>' ));
3345
3351
}
3346
3352
3347
- Library get definingLibrary => packageGraph.findOrCreateLibraryFor (element);
3353
+ Library get definingLibrary =>
3354
+ packageGraph.findButDoNotCreateLibraryFor (element);
3348
3355
3349
3356
Library _canonicalLibrary;
3350
3357
// _canonicalLibrary can be null so we can't check against null to see whether
@@ -4651,31 +4658,28 @@ class PackageGraph {
4651
4658
// to this graph.
4652
4659
4653
4660
PackageGraph ._(this .config, this .packageMeta, this ._packageWarningOptions,
4654
- this .driver, this .sdk) {}
4661
+ this .driver, this .sdk) : session = driver.currentSession {}
4655
4662
4656
4663
static Future <PackageGraph > setUpPackageGraph (
4657
- Iterable <LibraryElement > libraryElements,
4658
- Iterable <LibraryElement > specialLibraryElements,
4659
- DartdocOptionContext config,
4660
- PackageMeta packageMeta,
4661
- packageWarningOptions,
4662
- driver,
4663
- sdk,
4664
- ) async {
4664
+ Iterable <ResolvedLibraryResult > libraryResults,
4665
+ Iterable <ResolvedLibraryResult > specialLibraryResults,
4666
+ DartdocOptionContext config,
4667
+ PackageMeta packageMeta,
4668
+ packageWarningOptions,
4669
+ driver,
4670
+ sdk,) async {
4665
4671
PackageGraph newGraph =
4666
4672
PackageGraph ._(config, packageMeta, packageWarningOptions, driver, sdk);
4667
4673
assert (newGraph._allConstructedModelElements.isEmpty);
4668
4674
assert (newGraph.allLibraries.isEmpty);
4669
4675
newGraph._packageWarningCounter =
4670
4676
new PackageWarningCounter (newGraph._packageWarningOptions);
4671
4677
4672
- // Build [Package] objects.
4673
- libraryElements.forEach ((element) {});
4674
-
4675
4678
// Build [Library] objects, and link them to [Package]s.
4676
- libraryElements.forEach ((element) {
4679
+ libraryResults.forEach ((result) {
4680
+ LibraryElement element = result.element;
4677
4681
var packageMeta = new PackageMeta .fromElement (element, config);
4678
- var lib = new Library ._(element , newGraph,
4682
+ var lib = new Library ._(result , newGraph,
4679
4683
new Package .fromPackageMeta (packageMeta, newGraph));
4680
4684
newGraph.packageMap[packageMeta.name]._libraries.add (lib);
4681
4685
newGraph.allLibraries[element] = lib;
@@ -4687,9 +4691,9 @@ class PackageGraph {
4687
4691
newGraph.allLibrariesAdded = true ;
4688
4692
4689
4693
// [findOrCreateLibraryFor] already adds to the proper structures.
4690
- specialLibraryElements. forEach ((element ) {
4691
- newGraph.findOrCreateLibraryFor (element);
4692
- });
4694
+ for ( ResolvedLibraryResult result in specialLibraryResults ) {
4695
+ await newGraph.findOrCreateLibraryFor (result. element);
4696
+ }
4693
4697
4694
4698
// From here on in, we might find special objects. Initialize the
4695
4699
// specialClasses handler so when we find them, they get added.
@@ -4731,10 +4735,9 @@ class PackageGraph {
4731
4735
// Many ModelElements have the same ModelNode; don't build/cache this data more
4732
4736
// than once for them.
4733
4737
final Map <Element , ModelNode > _modelNodes = Map ();
4734
- ModelNode _getModelNodeFor (element) {
4735
- /// TODO(jcollins-g): merge with removal of computeNode.
4738
+ ModelNode _getModelNodeFor (element, Library definingLibrary) {
4736
4739
_modelNodes.putIfAbsent (
4737
- element, () => ModelNode (element ? . computeNode () , element));
4740
+ element, () => ModelNode (definingLibrary.libraryResult. getElementDeclaration (element) ? .node , element));
4738
4741
return _modelNodes[element];
4739
4742
}
4740
4743
@@ -4808,7 +4811,9 @@ class PackageGraph {
4808
4811
/// Map of package name to Package.
4809
4812
final Map <String , Package > packageMap = {};
4810
4813
4814
+ /// TODO(brianwilkerson) Replace the driver with the session.
4811
4815
final AnalysisDriver driver;
4816
+ final AnalysisSession session;
4812
4817
final DartSdk sdk;
4813
4818
4814
4819
Map <Source , SdkLibrary > _sdkLibrarySources;
@@ -5089,7 +5094,7 @@ class PackageGraph {
5089
5094
// The first call to _tagReexportFor should not have a null libraryElement.
5090
5095
assert (lastExportedElement != null );
5091
5096
warnOnElement (
5092
- findOrCreateLibraryFor (lastExportedElement.enclosingElement),
5097
+ findButDoNotCreateLibraryFor (lastExportedElement.enclosingElement),
5093
5098
PackageWarning .unresolvedExport,
5094
5099
message: '"${lastExportedElement .uri }"' ,
5095
5100
referredFrom: < Locatable > [topLevelLibrary]);
@@ -5401,7 +5406,18 @@ class PackageGraph {
5401
5406
/// This is used when we might need a Library object that isn't actually
5402
5407
/// a documentation entry point (for elements that have no Library within the
5403
5408
/// set of canonical Libraries).
5404
- Library findOrCreateLibraryFor (Element e) {
5409
+ Library findButDoNotCreateLibraryFor (Element e) {
5410
+ // This is just a cache to avoid creating lots of libraries over and over.
5411
+ if (allLibraries.containsKey (e.library)) {
5412
+ return allLibraries[e.library];
5413
+ }
5414
+ return null ;
5415
+ }
5416
+
5417
+ /// This is used when we might need a Library object that isn't actually
5418
+ /// a documentation entry point (for elements that have no Library within the
5419
+ /// set of canonical Libraries).
5420
+ Future <Library > findOrCreateLibraryFor (Element e) async {
5405
5421
// This is just a cache to avoid creating lots of libraries over and over.
5406
5422
if (allLibraries.containsKey (e.library)) {
5407
5423
return allLibraries[e.library];
@@ -5411,8 +5427,10 @@ class PackageGraph {
5411
5427
return null ;
5412
5428
}
5413
5429
5430
+ ResolvedLibraryResult result =
5431
+ await session.getResolvedLibraryByElement (e.library);
5414
5432
Library foundLibrary = new Library ._(
5415
- e.library ,
5433
+ result ,
5416
5434
this ,
5417
5435
new Package .fromPackageMeta (
5418
5436
new PackageMeta .fromElement (e.library, config), packageGraph));
@@ -6396,15 +6414,15 @@ class PackageBuilder {
6396
6414
if (packageMeta.needsPubGet) {
6397
6415
packageMeta.runPubGet ();
6398
6416
}
6399
- Set <LibraryElement > libraries = new Set ();
6400
- Set <LibraryElement > specialLibraries = new Set ();
6417
+ Map <LibraryElement , ResolvedLibraryResult > libraryResults = new Map ();
6418
+ Map <LibraryElement , ResolvedLibraryResult > specialLibraryResults = new Map ();
6401
6419
DartSdk findSpecialsSdk = sdk;
6402
6420
if (embedderSdk != null && embedderSdk.urlMappings.isNotEmpty) {
6403
6421
findSpecialsSdk = embedderSdk;
6404
6422
}
6405
- await getLibraries (libraries, specialLibraries , getFiles,
6423
+ await getLibraries (libraryResults, specialLibraryResults , getFiles,
6406
6424
specialLibraryFiles (findSpecialsSdk).toSet ());
6407
- return await PackageGraph .setUpPackageGraph (libraries, specialLibraries ,
6425
+ return await PackageGraph .setUpPackageGraph (libraryResults.values, specialLibraryResults.values ,
6408
6426
config, config.topLevelPackageMeta, getWarningOptions (), driver, sdk);
6409
6427
}
6410
6428
@@ -6516,11 +6534,22 @@ class PackageBuilder {
6516
6534
options);
6517
6535
driver.results.listen ((_) {});
6518
6536
driver.exceptions.listen ((_) {});
6537
+ _session = driver.currentSession;
6519
6538
scheduler.start ();
6520
6539
}
6521
6540
return _driver;
6522
6541
}
6523
6542
6543
+ AnalysisSession _session;
6544
+ AnalysisSession get session {
6545
+ if (_session == null ) {
6546
+ // The current session is initialized as a side-effect of initializing the
6547
+ // driver.
6548
+ driver;
6549
+ }
6550
+ return _session;
6551
+ }
6552
+
6524
6553
PackageWarningOptions getWarningOptions () {
6525
6554
PackageWarningOptions warningOptions =
6526
6555
new PackageWarningOptions (config.verboseWarnings);
@@ -6554,7 +6583,7 @@ class PackageBuilder {
6554
6583
6555
6584
/// Parse a single library at [filePath] using the current analysis driver.
6556
6585
/// If [filePath] is not a library, returns null.
6557
- Future <LibraryElement > processLibrary (String filePath) async {
6586
+ Future <ResolvedLibraryResult > processLibrary (String filePath) async {
6558
6587
String name = filePath;
6559
6588
6560
6589
if (name.startsWith (directoryCurrentPath)) {
@@ -6581,7 +6610,7 @@ class PackageBuilder {
6581
6610
if (sourceKind == SourceKind .LIBRARY ) {
6582
6611
// Loading libraryElements from part files works, but is painfully slow
6583
6612
// and creates many duplicates.
6584
- return ( await driver. getResult (filePath)) ? .libraryElement ;
6613
+ return await session. getResolvedLibrary (source.fullName) ;
6585
6614
}
6586
6615
return null ;
6587
6616
}
@@ -6594,24 +6623,18 @@ class PackageBuilder {
6594
6623
return metas;
6595
6624
}
6596
6625
6597
- Future <List <LibraryElement >> _parseLibraries (Set <String > files) async {
6598
- Iterable <LibraryElement > libraries = new Iterable . empty ();
6626
+ Future <Map <LibraryElement , ResolvedLibraryResult >> _parseLibraries (Set <String > files) async {
6627
+ Map <LibraryElement , ResolvedLibraryResult > libraries = new Map ();
6599
6628
Set <PackageMeta > lastPass = new Set ();
6600
6629
Set <PackageMeta > current;
6601
- Set <String > addedFiles = new Set ();
6602
6630
do {
6603
6631
lastPass = _packageMetasForFiles (files);
6604
- files.difference (addedFiles).forEach ((filename) {
6605
- addedFiles.add (filename);
6606
- });
6607
- libraries = quiverIterables.concat ([
6608
- libraries,
6609
- (await Future .wait (files.map ((f) => processLibrary (f))))
6610
- .where ((LibraryElement l) => l != null )
6611
- ]);
6632
+ for (ResolvedLibraryResult r in (await Future .wait (files.map ((f) => processLibrary (f)))).where ((ResolvedLibraryResult l) => l != null )) {
6633
+ libraries[r.element] = r;
6634
+ }
6612
6635
6613
- /// We don't care about upstream analysis errors, so save the first
6614
- /// source list.
6636
+ // Be sure to give the analyzer enough time to find all the files.
6637
+ await driver. discoverAvailableFiles ();
6615
6638
files.addAll (driver.knownFiles);
6616
6639
files.addAll (_includeExternalsFrom (driver.knownFiles));
6617
6640
current = _packageMetasForFiles (files);
@@ -6629,7 +6652,7 @@ class PackageBuilder {
6629
6652
}
6630
6653
}
6631
6654
} while (! lastPass.containsAll (current));
6632
- return libraries. toList () ;
6655
+ return libraries;
6633
6656
}
6634
6657
6635
6658
/// Given a package name, explore the directory and pull out all top level
@@ -6719,23 +6742,24 @@ class PackageBuilder {
6719
6742
}
6720
6743
6721
6744
Future <void > getLibraries (
6722
- Set <LibraryElement > libraries ,
6723
- Set <LibraryElement > specialLibraries ,
6745
+ Map <LibraryElement , ResolvedLibraryResult > libraryResults ,
6746
+ Map <LibraryElement , ResolvedLibraryResult > specialLibraryResults ,
6724
6747
Set <String > files,
6725
6748
Set <String > specialFiles) async {
6726
- libraries .addAll (await _parseLibraries (files));
6727
- specialLibraries
6749
+ libraryResults .addAll (await _parseLibraries (files));
6750
+ specialLibraryResults
6728
6751
.addAll (await _parseLibraries (specialFiles.difference (files)));
6729
6752
if (config.include.isNotEmpty) {
6730
- Iterable knownLibraryNames = libraries. map ((l) => l.name);
6753
+ Iterable knownLibraryNames = libraryResults.values. map ((l) => l.element .name);
6731
6754
Set notFound = new Set .from (config.include)
6732
6755
.difference (new Set .from (knownLibraryNames))
6733
6756
.difference (new Set .from (config.exclude));
6734
6757
if (notFound.isNotEmpty) {
6735
6758
throw 'Did not find: [${notFound .join (', ' )}] in '
6736
6759
'known libraries: [${knownLibraryNames .join (', ' )}]' ;
6737
6760
}
6738
- libraries.removeWhere ((lib) => ! config.include.contains (lib.name));
6761
+ libraryResults.removeWhere (
6762
+ (element, result) => ! config.include.contains (result.element.name));
6739
6763
}
6740
6764
}
6741
6765
0 commit comments