1
0
Fork 0

Update AdvancedJsonRpc

pull/103/head
Felix Becker 2016-10-20 03:28:36 +02:00
parent f8733c741c
commit cdb7876fe3
3 changed files with 15 additions and 20 deletions

View File

@ -27,7 +27,7 @@
"nikic/php-parser": "dev-master#90834bff8eaf7b7f893253f312e73d8f532341ca", "nikic/php-parser": "dev-master#90834bff8eaf7b7f893253f312e73d8f532341ca",
"phpdocumentor/reflection-docblock": "^3.0", "phpdocumentor/reflection-docblock": "^3.0",
"sabre/event": "^4.0", "sabre/event": "^4.0",
"felixfbecker/advanced-json-rpc": "^1.2", "felixfbecker/advanced-json-rpc": "^2.0",
"squizlabs/php_codesniffer" : "^2.7", "squizlabs/php_codesniffer" : "^2.7",
"symfony/debug": "^3.1" "symfony/debug": "^3.1"
}, },

View File

@ -12,11 +12,12 @@ use LanguageServer\Protocol\{
MessageType, MessageType,
InitializeResult InitializeResult
}; };
use AdvancedJsonRpc\{Dispatcher, ResponseError, Response as ResponseBody, Request as RequestBody}; use AdvancedJsonRpc;
use Sabre\Event\Loop; use Sabre\Event\Loop;
use Exception; use Exception;
use Throwable;
class LanguageServer extends \AdvancedJsonRpc\Dispatcher class LanguageServer extends AdvancedJsonRpc\Dispatcher
{ {
/** /**
* Handles textDocument/* method calls * Handles textDocument/* method calls
@ -48,27 +49,22 @@ class LanguageServer extends \AdvancedJsonRpc\Dispatcher
parent::__construct($this, '/'); parent::__construct($this, '/');
$this->protocolReader = $reader; $this->protocolReader = $reader;
$this->protocolReader->onMessage(function (Message $msg) { $this->protocolReader->onMessage(function (Message $msg) {
$err = null;
$result = null;
try { try {
// Invoke the method handler to get a result // Invoke the method handler to get a result
$result = $this->dispatch($msg->body); $result = $this->dispatch($msg->body);
} catch (ResponseError $e) { $responseBody = new AdvancedJsonRpc\SuccessResponse($msg->body->id, $result);
} catch (AdvancedJsonRpc\Error $error) {
// If a ResponseError is thrown, send it back in the Response (result will be null) // If a ResponseError is thrown, send it back in the Response (result will be null)
$err = $e; $responseBody = new AdvancedJsonRpc\ErrorResponse($msg->body->id, $error);
} catch (Throwable $e) { } catch (Throwable $e) {
// If an unexpected error occured, send back an INTERNAL_ERROR error response (result will be null) // If an unexpected error occured, send back an INTERNAL_ERROR error response
$err = new ResponseError( $error = new AdvancedJsonRpc\Error($e->getMessage(), AdvancedJsonRpc\ErrorCode::INTERNAL_ERROR, null, $e);
$e->getMessage(), $responseBody = new AdvancedJsonRpc\ErrorResponse($msg->body->id, $error);
$e->getCode() === 0 ? ErrorCode::INTERNAL_ERROR : $e->getCode(),
null,
$e
);
} }
// Only send a Response for a Request // Only send a Response for a Request
// Notifications do not send Responses // Notifications do not send Responses
if (RequestBody::isRequest($msg->body)) { if (AdvancedJsonRpc\Request::isRequest($msg->body)) {
$this->protocolWriter->write(new Message(new ResponseBody($msg->body->id, $result, $err))); $this->protocolWriter->write(new Message($responseBody));
} }
}); });
$this->protocolWriter = $writer; $this->protocolWriter = $writer;

View File

@ -6,7 +6,7 @@ namespace LanguageServer\Tests;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use LanguageServer\LanguageServer; use LanguageServer\LanguageServer;
use LanguageServer\Protocol\{Message, ClientCapabilities, TextDocumentSyncKind}; use LanguageServer\Protocol\{Message, ClientCapabilities, TextDocumentSyncKind};
use AdvancedJsonRpc\{Request as RequestBody, Response as ResponseBody}; use AdvancedJsonRpc;
class LanguageServerTest extends TestCase class LanguageServerTest extends TestCase
{ {
@ -19,14 +19,13 @@ class LanguageServerTest extends TestCase
$writer->onMessage(function (Message $message) use (&$msg) { $writer->onMessage(function (Message $message) use (&$msg) {
$msg = $message; $msg = $message;
}); });
$reader->write(new Message(new RequestBody(1, 'initialize', [ $reader->write(new Message(new AdvancedJsonRpc\Request(1, 'initialize', [
'rootPath' => __DIR__, 'rootPath' => __DIR__,
'processId' => getmypid(), 'processId' => getmypid(),
'capabilities' => new ClientCapabilities() 'capabilities' => new ClientCapabilities()
]))); ])));
$this->assertNotNull($msg, 'onMessage callback should be called'); $this->assertNotNull($msg, 'onMessage callback should be called');
$this->assertInstanceOf(ResponseBody::class, $msg->body); $this->assertInstanceOf(AdvancedJsonRpc\SuccessResponse::class, $msg->body);
$this->assertNull($msg->body->error);
$this->assertEquals((object)[ $this->assertEquals((object)[
'capabilities' => (object)[ 'capabilities' => (object)[
'textDocumentSync' => TextDocumentSyncKind::FULL, 'textDocumentSync' => TextDocumentSyncKind::FULL,