fix no backend error

This commit is contained in:
Nikita Dauhashei 2024-04-21 13:00:57 +02:00
parent 9a1dc2b0e3
commit 7c6e36e4f4
No known key found for this signature in database
GPG Key ID: 3B93F636D935382D
7 changed files with 25 additions and 5 deletions

View File

@ -19,6 +19,8 @@ abstract class DriftBackend {
return element.source!.uri;
}
bool get canReadDart;
/// Resolves a Dart library by its uri.
///
/// This should also be able to resolve SDK libraries.

View File

@ -63,13 +63,15 @@ class DriftAnalysisDriver {
AnalysisResultCacheReader? cacheReader;
final KnownDriftTypes knownTypes;
final KnownDriftTypes? _knownTypes;
KnownDriftTypes get knownTypes => _knownTypes!;
@visibleForTesting
DriftAnalysisDriver(
this.backend,
this.options,
this.knownTypes, {
this._knownTypes, {
bool isTesting = false,
}) : _isTesting = isTesting;

View File

@ -102,10 +102,14 @@ class KnownDriftTypes {
return type?.asInstanceOf(converter);
}
static Future<KnownDriftTypes> resolve(DriftBackend backend) async {
final library = await backend.readDart(uri);
static Future<KnownDriftTypes?> resolve(DriftBackend backend) async {
if (backend.canReadDart) {
final library = await backend.readDart(uri);
return KnownDriftTypes._fromLibrary(library);
return KnownDriftTypes._fromLibrary(library);
}
return null;
}
static final Uri uri = Uri.parse('package:drift/src/drift_dev_helper.dart');

View File

@ -96,6 +96,9 @@ class AnalysisContextBackend extends DriftBackend {
return Future.value(resourceProvider.getFile(path).readAsStringSync());
}
@override
bool get canReadDart => true;
@override
Future<LibraryElement> readDart(Uri uri) async {
final result = await context.currentSession.getLibraryByUri(uri.toString());

View File

@ -38,6 +38,9 @@ class DriftBuildBackend extends DriftBackend {
return id.uri;
}
@override
bool get canReadDart => true;
@override
Future<LibraryElement> readDart(Uri uri) async {
if (uri.scheme == 'dart') {

View File

@ -91,6 +91,9 @@ class _SingleFileNoAnalyzerBackend extends DriftBackend {
return Future.value(contents);
}
@override
bool get canReadDart => false;
@override
Future<LibraryElement> readDart(Uri uri) async {
_noAnalyzer();

View File

@ -237,6 +237,9 @@ class TestBackend extends DriftBackend {
return null;
}
@override
bool get canReadDart => true;
@override
Future<LibraryElement> readDart(Uri uri) async {
await ensureHasDartAnalyzer();