Skip to content

Commit

Permalink
Stop using deprecated ContextLocator and ContextBuilder. (#285)
Browse files Browse the repository at this point in the history
* Stop using deprecated ContextLocator and ContextBuilder.

* Try using Dart SDK 3.5 instead of 3.4

* Revert to analyzer >= 5.2.0, but no dispose().
  • Loading branch information
scheglov authored Jul 18, 2024
1 parent 6a07a16 commit f957dd2
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions pkgs/graphs/example/crawl_async_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import 'dart:async';
import 'dart:isolate';

import 'package:analyzer/dart/analysis/analysis_context.dart';
import 'package:analyzer/dart/analysis/context_builder.dart';
import 'package:analyzer/dart/analysis/context_locator.dart';
import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:graphs/graphs.dart';
Expand All @@ -27,25 +25,21 @@ Future<void> main() async {
print(allImports.map((s) => s.uri).toList());
}

AnalysisContext? _analysisContext;
AnalysisContextCollection? _analysisContextCollection;

Future<AnalysisContext> get analysisContext async {
var context = _analysisContext;
if (context == null) {
Future<AnalysisContextCollection> get analysisContextCollection async {
var collection = _analysisContextCollection;
if (collection == null) {
final libUri = Uri.parse('package:graphs/');
final libPath = await pathForUri(libUri);
final packagePath = p.dirname(libPath);

final roots = ContextLocator().locateRoots(includedPaths: [packagePath]);
if (roots.length != 1) {
throw StateError('Expected to find exactly one context root, got $roots');
}

context = _analysisContext =
ContextBuilder().createContext(contextRoot: roots[0]);
collection = _analysisContextCollection = AnalysisContextCollection(
includedPaths: [packagePath],
);
}

return context;
return collection;
}

Future<Iterable<Uri>> findImports(Uri from, Source source) async =>
Expand All @@ -57,7 +51,8 @@ Future<Iterable<Uri>> findImports(Uri from, Source source) async =>

Future<CompilationUnit> parseUri(Uri uri) async {
final path = await pathForUri(uri);
final analysisSession = (await analysisContext).currentSession;
final analysisContext = (await analysisContextCollection).contexts.single;
final analysisSession = analysisContext.currentSession;
final parseResult = analysisSession.getParsedUnit(path);
return (parseResult as ParsedUnitResult).unit;
}
Expand Down

0 comments on commit f957dd2

Please sign in to comment.