Index: pkg/analysis_server/lib/src/services/dependencies/reachable_source_collector.dart |
diff --git a/pkg/analysis_server/lib/src/services/dependencies/reachable_source_collector.dart b/pkg/analysis_server/lib/src/services/dependencies/reachable_source_collector.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c2a9c2d12777bfbd09c982636076ac7d93df9878 |
--- /dev/null |
+++ b/pkg/analysis_server/lib/src/services/dependencies/reachable_source_collector.dart |
@@ -0,0 +1,53 @@ |
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
+// 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. |
+ |
+library services.dependencies.reachable_source_collector; |
+ |
+import 'dart:collection'; |
+ |
+import 'package:analyzer/src/generated/engine.dart'; |
+import 'package:analyzer/src/generated/source.dart'; |
+import 'package:analyzer/task/dart.dart'; |
+ |
+/// Collects reachable sources. |
+class ReachableSourceCollector { |
+ final Map<String, List<String>> _sourceMap = |
+ new HashMap<String, List<String>>(); |
+ |
+ final Source source; |
+ final AnalysisContext context; |
+ ReachableSourceCollector(this.source, this.context); |
+ |
+ /// Collect reachable sources. |
+ Map<String, List<String>> collectSources() { |
+ // Play it safe in case we're passed a source with no context. |
+ if (context != null) { |
+ _addDependencies(source); |
+ } |
+ return _sourceMap; |
+ } |
+ |
+ void _addDependencies(Source source) { |
+ String sourceUri = source?.uri?.toString(); |
Brian Wilkerson
2015/12/02 01:56:27
nit: We don't need the null-aware operators if we
pquitslund
2015/12/02 16:40:22
Except that this is called recursively... My thin
Brian Wilkerson
2015/12/02 18:04:53
That shouldn't be the case. IMPORTED_LIBRARIES and
|
+ |
+ // Bail if the source or URI are null. |
+ if (sourceUri == null) { |
Brian Wilkerson
2015/12/02 01:56:27
Similarly, we don't need this test.
pquitslund
2015/12/02 16:40:22
See above.
FWIW: my rationale is based on doing a
|
+ return; |
+ } |
+ |
+ // Careful not to revisit. |
+ if (_sourceMap[sourceUri] != null) { |
+ return; |
+ } |
+ |
+ List<Source> sources = <Source>[]; |
+ sources.addAll(context.computeResult(source, IMPORTED_LIBRARIES)); |
+ sources.addAll(context.computeResult(source, EXPORTED_LIBRARIES)); |
+ |
+ _sourceMap[sourceUri] = |
+ sources.map((source) => source.uri.toString()).toList(); |
+ |
+ sources.forEach((s) => _addDependencies(s)); |
+ } |
+} |