Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop using deprecated ContextLocator and ContextBuilder. #285

Merged
merged 3 commits into from
Jul 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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