From 6be9b6aa2089c0e3123bbc7b5772c0eebf192c5a Mon Sep 17 00:00:00 2001 From: Camille Dejoye Date: Wed, 22 Sep 2021 10:27:08 +0200 Subject: [PATCH 1/4] replace old notifier service by notifier factory --- lib/LanguageServer/LanguageServerSessionExtension.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/LanguageServer/LanguageServerSessionExtension.php b/lib/LanguageServer/LanguageServerSessionExtension.php index dc7b0c4..eda6a5c 100644 --- a/lib/LanguageServer/LanguageServerSessionExtension.php +++ b/lib/LanguageServer/LanguageServerSessionExtension.php @@ -13,8 +13,7 @@ use Phpactor\LanguageServer\Core\Server\RpcClient; use Phpactor\LanguageServer\Core\Server\RpcClient\JsonRpcClient; use Phpactor\LanguageServer\Core\Server\Transmitter\MessageTransmitter; -use Phpactor\LanguageServer\WorkDoneProgress\ClientCapabilityDependentProgressNotifier; -use Phpactor\LanguageServer\WorkDoneProgress\ProgressNotifier; +use Phpactor\LanguageServer\WorkDoneProgress\ProgressNotifierFactory; use Phpactor\MapResolver\Resolver; class LanguageServerSessionExtension implements Extension @@ -66,8 +65,8 @@ public function load(ContainerBuilder $container): void return new JsonRpcClient($this->transmitter, $container->get(ResponseWatcher::class)); }); - $container->register(ProgressNotifier::class, function (Container $container) { - return new ClientCapabilityDependentProgressNotifier( + $container->register(ProgressNotifierFactory::class, function (Container $container) { + return new ProgressNotifierFactory( $container->get(ClientApi::class), $container->get(ClientCapabilities::class), ); From 3ef5e10340babb91c245d6091ec5c0953f29ce4f Mon Sep 17 00:00:00 2001 From: Camille Dejoye Date: Wed, 22 Sep 2021 10:27:40 +0200 Subject: [PATCH 2/4] add a test for the notifier factory with fake handler --- .../LanguageServer/Example/TestExtension.php | 19 +++++++++++++++++++ .../Unit/LanguageServerExtensionTest.php | 14 ++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/tests/LanguageServer/Example/TestExtension.php b/tests/LanguageServer/Example/TestExtension.php index c4ddc05..37f0c2c 100644 --- a/tests/LanguageServer/Example/TestExtension.php +++ b/tests/LanguageServer/Example/TestExtension.php @@ -15,10 +15,13 @@ use Phpactor\LanguageServerProtocol\TextDocumentItem; use Phpactor\LanguageServer\Core\CodeAction\CodeActionProvider; use Phpactor\LanguageServer\Core\Command\Command as CoreCommand; +use Phpactor\LanguageServer\Core\Handler\ClosureHandler; use Phpactor\LanguageServer\Core\Handler\Handler; use Phpactor\LanguageServer\Core\Rpc\NotificationMessage; use Phpactor\LanguageServer\Core\Server\ClientApi; use Phpactor\LanguageServer\Core\Service\ServiceProvider; +use Phpactor\LanguageServer\WorkDoneProgress\ProgressNotifierFactory; +use Phpactor\LanguageServer\WorkDoneProgress\WorkDoneToken; use Phpactor\MapResolver\Resolver; class TestExtension implements Extension @@ -45,6 +48,22 @@ public function test() }; }, [ LanguageServerExtension::TAG_METHOD_HANDLER => []]); + $container->register('test.progress_notifier_factory', function (Container $container): ClosureHandler { + return new ClosureHandler('test/progress_notifier_factory', function () use ($container): Promise { + $notifierFactory = $container->get(ProgressNotifierFactory::class); + assert($notifierFactory instanceof ProgressNotifierFactory); + $token = WorkDoneToken::generate(); + $notifier = $notifierFactory->create($token); + $notifier->begin('title'); + $notifier->report(); + $notifier->end(); + + return new Success(true); + }); + }, [ + LanguageServerExtension::TAG_METHOD_HANDLER => [], + ]); + $container->register('test.service', function (Container $container) { return new class($container->get(ClientApi::class)) implements ServiceProvider { /** @var ClientApi */ diff --git a/tests/LanguageServer/Unit/LanguageServerExtensionTest.php b/tests/LanguageServer/Unit/LanguageServerExtensionTest.php index b22ff47..93dbb1d 100644 --- a/tests/LanguageServer/Unit/LanguageServerExtensionTest.php +++ b/tests/LanguageServer/Unit/LanguageServerExtensionTest.php @@ -8,6 +8,7 @@ use Phpactor\LanguageServerProtocol\InitializeParams; use Phpactor\LanguageServer\Core\Rpc\NotificationMessage; use Phpactor\LanguageServer\Core\Rpc\RequestMessage; +use Phpactor\LanguageServer\Core\Rpc\ResponseMessage; use Phpactor\LanguageServer\Core\Server\Exception\ExitSession; use Phpactor\LanguageServer\Listener\WorkspaceListener; use Phpactor\LanguageServer\Test\ProtocolFactory; @@ -167,4 +168,17 @@ public function testEnableFileEvents(): void assert($message instanceof RequestMessage); self::assertEquals('client/registerCapability', $message->method); } + + public function testProgressNotifierFactory(): void + { + $serverTester = $this->createTester(null, [ + LanguageServerExtension::PARAM_FILE_EVENTS => true, + ]); + $serverTester->initialize(); + $response = $serverTester->requestAndWait('test/progress_notifier_factory', []); + + self::assertInstanceOf(ResponseMessage::class, $response); + self::assertTrue($response->result); + self::assertNull($response->error); + } } From 39f6ee0680aa9b4163f77fe75f04c52d15f73d07 Mon Sep 17 00:00:00 2001 From: Camille Dejoye Date: Wed, 22 Sep 2021 10:34:20 +0200 Subject: [PATCH 3/4] fix diagnostic issues in TestExtension.php --- .../LanguageServer/Example/TestExtension.php | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/tests/LanguageServer/Example/TestExtension.php b/tests/LanguageServer/Example/TestExtension.php index 37f0c2c..752ab6a 100644 --- a/tests/LanguageServer/Example/TestExtension.php +++ b/tests/LanguageServer/Example/TestExtension.php @@ -31,21 +31,13 @@ class TestExtension implements Extension */ public function load(ContainerBuilder $container): void { - $container->register('test.handler', function (Container $container) { - return new class implements Handler { - public function methods(): array - { - return ['test' => 'test']; - } - - public function test() - { - return new Success(new NotificationMessage('window/showMessage', [ - 'type' => MessageType::INFO, - 'message' => 'Hallo', - ])); - } - }; + $container->register('test.handler', function (Container $container): ClosureHandler { + return new ClosureHandler('test', function (): Promise { + return new Success(new NotificationMessage('window/showMessage', [ + 'type' => MessageType::INFO, + 'message' => 'Hallo', + ])); + }); }, [ LanguageServerExtension::TAG_METHOD_HANDLER => []]); $container->register('test.progress_notifier_factory', function (Container $container): ClosureHandler { @@ -77,7 +69,10 @@ public function services(): array return ['test']; } - public function test() + /** + * @return Promise + */ + public function test(): Promise { $this->api->window()->showmessage()->info('service started'); return new Success(new NotificationMessage('window/showMessage', [ @@ -90,6 +85,9 @@ public function test() $container->register('test.command', function (Container $container) { return new class implements CoreCommand { + /** + * @return Promise + */ public function __invoke(string $text): Promise { return new Success($text); From c1c226a4c2110b405e8dad23b683f07505c06a3d Mon Sep 17 00:00:00 2001 From: Camille Dejoye Date: Wed, 22 Sep 2021 10:39:25 +0200 Subject: [PATCH 4/4] fixup! add a test for the notifier factory with fake handler --- tests/LanguageServer/Example/TestExtension.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/LanguageServer/Example/TestExtension.php b/tests/LanguageServer/Example/TestExtension.php index 752ab6a..4234f77 100644 --- a/tests/LanguageServer/Example/TestExtension.php +++ b/tests/LanguageServer/Example/TestExtension.php @@ -16,7 +16,6 @@ use Phpactor\LanguageServer\Core\CodeAction\CodeActionProvider; use Phpactor\LanguageServer\Core\Command\Command as CoreCommand; use Phpactor\LanguageServer\Core\Handler\ClosureHandler; -use Phpactor\LanguageServer\Core\Handler\Handler; use Phpactor\LanguageServer\Core\Rpc\NotificationMessage; use Phpactor\LanguageServer\Core\Server\ClientApi; use Phpactor\LanguageServer\Core\Service\ServiceProvider;