Update
parent
535dc9efda
commit
4d2b223a96
|
@ -2,16 +2,17 @@
|
|||
|
||||
namespace LanguageServer;
|
||||
|
||||
use LanguageServer\Protocol\ProtocolServer;
|
||||
use LanguageServer\Protocol\{ProtocolServer, ServerCapabilities};
|
||||
use LanguageServer\Protocol\Methods\Initialize\{InitializeRequest, InitializeResult, InitializeResponse};
|
||||
|
||||
class LanguageServer extends ProtocolServer
|
||||
{
|
||||
public function listen()
|
||||
public function initialize(InitializeRequest $req): InitializeResponse
|
||||
{
|
||||
$this->on('initialize', function (InitializeRequest $req) {
|
||||
$res = new InitializeResponse();
|
||||
$this->sendResponse($res);
|
||||
});
|
||||
parent::listen();
|
||||
$result = new InitializeResult;
|
||||
$result->capabilites = new ServerCapabilities;
|
||||
return new InitializeResponse($result);
|
||||
}
|
||||
|
||||
public function shutdown
|
||||
}
|
||||
|
|
|
@ -2,14 +2,10 @@
|
|||
|
||||
namespace LanguageServer\Protocol\Methods\Initialize;
|
||||
|
||||
use LanguageServer\Protocol\Response;
|
||||
|
||||
class InitializeResponse extends Response
|
||||
class InitializeResponse
|
||||
{
|
||||
/**
|
||||
* The capabilities the language server provides.
|
||||
*
|
||||
* @var LanguageServer\Protocol\ServerCapabilities
|
||||
* @var InitializeResult
|
||||
*/
|
||||
public $capabilites;
|
||||
public $result;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace LanguageServer\Protocol\Methods\Initialize;
|
||||
|
||||
use LanguageServer\Protocol\Result;
|
||||
|
||||
class InitializeResult extends Result
|
||||
{
|
||||
/**
|
||||
* The capabilities the language server provides.
|
||||
*
|
||||
* @var LanguageServer\Protocol\ServerCapabilities
|
||||
*/
|
||||
public $capabilites;
|
||||
}
|
|
@ -3,14 +3,14 @@
|
|||
namespace LanguageServer\Protocol;
|
||||
|
||||
use Sabre\Event\Loop;
|
||||
use Sabre\Event\EventEmitter;
|
||||
use LanguageServer\Protocol\Methods\Initialize\{InitializeRequest, InitializeResponse};
|
||||
|
||||
abstract class ParsingMode {
|
||||
const HEADERS = 1;
|
||||
const BODY = 2;
|
||||
}
|
||||
|
||||
class ProtocolServer extends EventEmitter
|
||||
abstract class ProtocolServer
|
||||
{
|
||||
private $input;
|
||||
private $output;
|
||||
|
@ -49,7 +49,18 @@ class ProtocolServer extends EventEmitter
|
|||
case ParsingMode::BODY:
|
||||
if (strlen($buffer) === $contentLength) {
|
||||
$req = Message::parse($body, Request::class);
|
||||
$this->emit($body->method, [$req]);
|
||||
if (!method_exists($this, $req->method)) {
|
||||
$this->sendResponse(new Response(null, new ResponseError("Method {$req->method} is not implemented", ErrorCode::METHOD_NOT_FOUND, $e)));
|
||||
} else {
|
||||
try {
|
||||
$result = $this->{$req->method}($req->params);
|
||||
$this->sendResponse(new Response($result));
|
||||
} catch (ResponseError $e) {
|
||||
$this->sendResponse(new Response(null, $e));
|
||||
} catch (Throwable $e) {
|
||||
$this->sendResponse(new Response(null, new ResponseError($e->getMessage(), $e->getCode(), null, $e)));
|
||||
}
|
||||
}
|
||||
$this->parsingMode = ParsingMode::HEADERS;
|
||||
$this->buffer = '';
|
||||
}
|
||||
|
@ -64,4 +75,6 @@ class ProtocolServer extends EventEmitter
|
|||
{
|
||||
fwrite($this->output, json_encode($res));
|
||||
}
|
||||
|
||||
abstract public function initialize(InitializeRequest $req): InitializeResponse;
|
||||
}
|
||||
|
|
|
@ -15,12 +15,18 @@ class Response extends Message
|
|||
public $method;
|
||||
|
||||
/**
|
||||
* @var object|null
|
||||
* @var mixed
|
||||
*/
|
||||
public $params;
|
||||
public $result;
|
||||
|
||||
/**
|
||||
* @var ResponseError|null
|
||||
*/
|
||||
public $error;
|
||||
|
||||
public function __construct($result, ResponseError $error = null)
|
||||
{
|
||||
$this->result = $result;
|
||||
$this->error = $error;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,9 @@
|
|||
|
||||
namespace LanguageServer\Protocol;
|
||||
|
||||
class ResponseError
|
||||
use Exception;
|
||||
|
||||
class ResponseError extends Exception
|
||||
{
|
||||
/**
|
||||
* A number indicating the error type that occurred.
|
||||
|
@ -25,4 +27,10 @@ class ResponseError
|
|||
* @var mixed
|
||||
*/
|
||||
public $data;
|
||||
|
||||
public function __construct(string $message, int $code = ErrorCode::INTERNAL_ERROR, $data = null, Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, $code, $previous);
|
||||
$this->data = $data;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -107,5 +107,5 @@ class ServerCapabilites
|
|||
*
|
||||
* @var bool|null
|
||||
*/
|
||||
public $renameProvidern;
|
||||
public $renameProvider;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue