From 5317d0a33b5c66d20e8687743f8b7eb099dbd4c3 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Thu, 23 Jun 2022 22:46:23 +0200 Subject: [PATCH] Upgrade to new analysis plugin API --- drift/tools/analyzer_plugin/bin/plugin.dart | 17 ++--- drift/tools/analyzer_plugin/pubspec.yaml | 8 +-- drift_dev/lib/src/backends/common/driver.dart | 6 ++ drift_dev/lib/src/backends/plugin/plugin.dart | 67 ++++++++++--------- drift_dev/pubspec.yaml | 2 +- 5 files changed, 53 insertions(+), 47 deletions(-) diff --git a/drift/tools/analyzer_plugin/bin/plugin.dart b/drift/tools/analyzer_plugin/bin/plugin.dart index 666a3222..f2182399 100644 --- a/drift/tools/analyzer_plugin/bin/plugin.dart +++ b/drift/tools/analyzer_plugin/bin/plugin.dart @@ -1,5 +1,3 @@ -//@dart=2.6 - import 'dart:convert'; import 'dart:isolate'; @@ -21,22 +19,19 @@ void main(List args, SendPort sendPort) { class _PluginProxy { final SendPort sendToAnalysisServer; - ReceivePort _receive; - IOWebSocketChannel _channel; - _PluginProxy(this.sendToAnalysisServer); Future 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)); }); } diff --git a/drift/tools/analyzer_plugin/pubspec.yaml b/drift/tools/analyzer_plugin/pubspec.yaml index a642efda..f5dd58be 100644 --- a/drift/tools/analyzer_plugin/pubspec.yaml +++ b/drift/tools/analyzer_plugin/pubspec.yaml @@ -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) diff --git a/drift_dev/lib/src/backends/common/driver.dart b/drift_dev/lib/src/backends/common/driver.dart index 2923226f..b0162f01 100644 --- a/drift_dev/lib/src/backends/common/driver.dart +++ b/drift_dev/lib/src/backends/common/driver.dart @@ -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') || diff --git a/drift_dev/lib/src/backends/plugin/plugin.dart b/drift_dev/lib/src/backends/plugin/plugin.dart index 2e78833a..1ea95b8b 100644 --- a/drift_dev/lib/src/backends/plugin/plugin.dart +++ b/drift_dev/lib/src/backends/plugin/plugin.dart @@ -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 drivers = {}; + final Map 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 analyzeFile( + {required AnalysisContext analysisContext, required String path}) async { + // Do nothing here, we react to file changes by sending out notifications. } @override - Future 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 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 beforeContextCollectionDispose({ + required AnalysisContextCollection contextCollection, + }) async { + for (final driver in drivers.values) { + driver.dispose(); + } + drivers.clear(); + } + + @override + Future contentChanged(List 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 _waitParsed(String path) async { diff --git a/drift_dev/pubspec.yaml b/drift_dev/pubspec.yaml index 1443d7d4..5e08f7f2 100644 --- a/drift_dev/pubspec.yaml +++ b/drift_dev/pubspec.yaml @@ -31,7 +31,7 @@ dependencies: # Dart analysis analyzer: "^4.1.0" - analyzer_plugin: '^0.10.0' + analyzer_plugin: ^0.11.0 source_span: ^1.5.5 package_config: ^2.0.0 pub_semver: ^2.0.0