Upgrade to new analysis plugin API

This commit is contained in:
Simon Binder 2022-06-23 22:46:23 +02:00
parent 3e82761caa
commit 68df9c3197
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
5 changed files with 54 additions and 48 deletions

View File

@ -1,5 +1,3 @@
//@dart=2.6
import 'dart:convert';
import 'dart:isolate';
@ -21,22 +19,19 @@ void main(List<String> args, SendPort sendPort) {
class _PluginProxy {
final SendPort sendToAnalysisServer;
ReceivePort _receive;
IOWebSocketChannel _channel;
_PluginProxy(this.sendToAnalysisServer);
Future<void> start() async {
_channel = IOWebSocketChannel.connect('ws://localhost:9999');
_receive = ReceivePort();
sendToAnalysisServer.send(_receive.sendPort);
final channel = IOWebSocketChannel.connect('ws://localhost:9999');
final receive = ReceivePort();
sendToAnalysisServer.send(receive.sendPort);
_receive.listen((data) {
receive.listen((data) {
// the server will send messages as maps, convert to json
_channel.sink.add(json.encode(data));
channel.sink.add(json.encode(data));
});
_channel.stream.listen((data) {
channel.stream.listen((data) {
sendToAnalysisServer.send(json.decode(data as String));
});
}

View File

@ -2,12 +2,12 @@ name: analyzer_load_drift_plugin
version: 1.0.0
description: This pubspec is a part of Drift and determines the version of the analyzer plugin to load
environment:
sdk: '>=2.9.0 <3.0.0'
environment:
sdk: '>=2.17.0 <3.0.0'
dependencies:
drift_dev: ^1.0.0
web_socket_channel: ^1.0.15
drift_dev: ^1.7.0
web_socket_channel: ^2.2.0
# These overrides are only needed when working on the plugin with useDebuggingVariant = false (not recommended)

View File

@ -41,6 +41,12 @@ class MoorDriver {
session = MoorSession(backend, options: options);
}
MoorDriver.forContext(this._resourceProvider, this.context) {
final overlayed = OverlayResourceProvider(_resourceProvider);
backend = StandaloneBackend(context, overlayed);
session = MoorSession(backend, options: const MoorOptions.defaults());
}
bool _ownsFile(String path) =>
path.endsWith('.moor') ||
path.endsWith('.drift') ||

View File

@ -1,3 +1,5 @@
import 'package:analyzer/dart/analysis/analysis_context.dart';
import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer/file_system/physical_file_system.dart';
import 'package:analyzer_plugin/plugin/completion_mixin.dart';
@ -7,7 +9,6 @@ import 'package:analyzer_plugin/plugin/outline_mixin.dart';
import 'package:analyzer_plugin/plugin/plugin.dart';
import 'package:analyzer_plugin/protocol/protocol.dart';
import 'package:analyzer_plugin/protocol/protocol_generated.dart' as plugin;
import 'package:analyzer_plugin/protocol/protocol_generated.dart';
import 'package:analyzer_plugin/utilities/completion/completion_core.dart';
import 'package:analyzer_plugin/utilities/folding/folding.dart';
import 'package:analyzer_plugin/utilities/navigation/navigation.dart';
@ -26,11 +27,11 @@ import 'logger.dart';
class MoorPlugin extends ServerPlugin
with OutlineMixin, FoldingMixin, CompletionMixin, NavigationMixin {
final Map<ContextRoot, MoorDriver> drivers = {};
final Map<AnalysisContext, MoorDriver> drivers = {};
late final ErrorService errorService = ErrorService(this);
MoorPlugin(ResourceProvider provider) : super(provider) {
MoorPlugin(ResourceProvider provider) : super(resourceProvider: provider) {
setupLogger(this);
}
@ -61,11 +62,6 @@ class MoorPlugin extends ServerPlugin
});
}
@override
Never createAnalysisDriver(plugin.ContextRoot contextRoot) {
throw UnsupportedError('Using custom driver management');
}
MoorDriver? _moorDriverForPath(String path) {
for (final driver in drivers.values) {
if (driver.context.contextRoot.isAnalyzed(path)) return driver;
@ -86,35 +82,44 @@ class MoorPlugin extends ServerPlugin
}
@override
void contentChanged(String path) {
_moorDriverForPath(path)?.handleFileChanged(path);
Future<void> analyzeFile(
{required AnalysisContext analysisContext, required String path}) async {
// Do nothing here, we react to file changes by sending out notifications.
}
@override
Future<AnalysisSetContextRootsResult> handleAnalysisSetContextRoots(
AnalysisSetContextRootsParams parameters) {
final roots = parameters.roots;
final oldRoots = drivers.keys.toList();
for (final contextRoot in roots) {
if (!oldRoots.remove(contextRoot)) {
// The context is new! Create a driver for it
final driver = MoorDriver(
resourceProvider,
contextRoot: contextRoot.root,
sdkPath: sdkManager.defaultSdkDirectory,
);
_didCreateDriver(driver);
drivers[contextRoot] = driver;
}
Future<void> afterNewContextCollection({
required AnalysisContextCollection contextCollection,
}) async {
for (final context in contextCollection.contexts) {
final createdDriver =
drivers[context] = MoorDriver.forContext(resourceProvider, context);
_didCreateDriver(createdDriver);
}
// All remaining contexts have been removed
for (final removed in oldRoots) {
drivers.remove(removed)?.dispose();
}
return super
.afterNewContextCollection(contextCollection: contextCollection);
}
return Future.value(plugin.AnalysisSetContextRootsResult());
@override
Future<void> beforeContextCollectionDispose({
required AnalysisContextCollection contextCollection,
}) async {
for (final driver in drivers.values) {
driver.dispose();
}
drivers.clear();
}
@override
Future<void> contentChanged(List<String> paths) async {
// Let the plugin class take care of updating Dart analysis
await super.contentChanged(paths);
// And then do drift analysis
for (final path in paths) {
_moorDriverForPath(path)?.handleFileChanged(path);
}
}
Future<FoundFile> _waitParsed(String path) async {

View File

@ -30,8 +30,8 @@ dependencies:
sqlparser: ^0.22.0
# Dart analysis
analyzer: "^4.0.0"
analyzer_plugin: '^0.10.0'
analyzer: "^4.1.0"
analyzer_plugin: ^0.11.0
source_span: ^1.5.5
package_config: ^2.0.0
pub_semver: ^2.0.0