Skip to content
This repository has been archived by the owner on Mar 6, 2022. It is now read-only.

Update progress notifier & add example tests #26

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
7 changes: 3 additions & 4 deletions lib/LanguageServer/LanguageServerSessionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
);
Expand Down
50 changes: 33 additions & 17 deletions tests/LanguageServer/Example/TestExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
use Phpactor\LanguageServerProtocol\TextDocumentItem;
use Phpactor\LanguageServer\Core\CodeAction\CodeActionProvider;
use Phpactor\LanguageServer\Core\Command\Command as CoreCommand;
use Phpactor\LanguageServer\Core\Handler\Handler;
use Phpactor\LanguageServer\Core\Handler\ClosureHandler;
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
Expand All @@ -28,23 +30,31 @@ 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 {
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 */
Expand All @@ -58,7 +68,10 @@ public function services(): array
return ['test'];
}

public function test()
/**
* @return Promise<NotificationMessage>
*/
public function test(): Promise
{
$this->api->window()->showmessage()->info('service started');
return new Success(new NotificationMessage('window/showMessage', [
Expand All @@ -71,6 +84,9 @@ public function test()

$container->register('test.command', function (Container $container) {
return new class implements CoreCommand {
/**
* @return Promise<string>
*/
public function __invoke(string $text): Promise
{
return new Success($text);
Expand Down
14 changes: 14 additions & 0 deletions tests/LanguageServer/Unit/LanguageServerExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}