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",
"phpdocumentor/reflection-docblock": "^3.0",
"sabre/event": "^4.0",
"felixfbecker/advanced-json-rpc": "^1.2",
"felixfbecker/advanced-json-rpc": "^2.0",
"squizlabs/php_codesniffer" : "^2.7",
"symfony/debug": "^3.1"
},

View File

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

View File

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