Update
parent
535dc9efda
commit
4d2b223a96
|
@ -2,16 +2,17 @@
|
||||||
|
|
||||||
namespace LanguageServer;
|
namespace LanguageServer;
|
||||||
|
|
||||||
use LanguageServer\Protocol\ProtocolServer;
|
use LanguageServer\Protocol\{ProtocolServer, ServerCapabilities};
|
||||||
|
use LanguageServer\Protocol\Methods\Initialize\{InitializeRequest, InitializeResult, InitializeResponse};
|
||||||
|
|
||||||
class LanguageServer extends ProtocolServer
|
class LanguageServer extends ProtocolServer
|
||||||
{
|
{
|
||||||
public function listen()
|
public function initialize(InitializeRequest $req): InitializeResponse
|
||||||
{
|
{
|
||||||
$this->on('initialize', function (InitializeRequest $req) {
|
$result = new InitializeResult;
|
||||||
$res = new InitializeResponse();
|
$result->capabilites = new ServerCapabilities;
|
||||||
$this->sendResponse($res);
|
return new InitializeResponse($result);
|
||||||
});
|
|
||||||
parent::listen();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function shutdown
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,14 +2,10 @@
|
||||||
|
|
||||||
namespace LanguageServer\Protocol\Methods\Initialize;
|
namespace LanguageServer\Protocol\Methods\Initialize;
|
||||||
|
|
||||||
use LanguageServer\Protocol\Response;
|
class InitializeResponse
|
||||||
|
|
||||||
class InitializeResponse extends Response
|
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* The capabilities the language server provides.
|
* @var InitializeResult
|
||||||
*
|
|
||||||
* @var LanguageServer\Protocol\ServerCapabilities
|
|
||||||
*/
|
*/
|
||||||
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;
|
namespace LanguageServer\Protocol;
|
||||||
|
|
||||||
use Sabre\Event\Loop;
|
use Sabre\Event\Loop;
|
||||||
use Sabre\Event\EventEmitter;
|
use LanguageServer\Protocol\Methods\Initialize\{InitializeRequest, InitializeResponse};
|
||||||
|
|
||||||
abstract class ParsingMode {
|
abstract class ParsingMode {
|
||||||
const HEADERS = 1;
|
const HEADERS = 1;
|
||||||
const BODY = 2;
|
const BODY = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
class ProtocolServer extends EventEmitter
|
abstract class ProtocolServer
|
||||||
{
|
{
|
||||||
private $input;
|
private $input;
|
||||||
private $output;
|
private $output;
|
||||||
|
@ -49,7 +49,18 @@ class ProtocolServer extends EventEmitter
|
||||||
case ParsingMode::BODY:
|
case ParsingMode::BODY:
|
||||||
if (strlen($buffer) === $contentLength) {
|
if (strlen($buffer) === $contentLength) {
|
||||||
$req = Message::parse($body, Request::class);
|
$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->parsingMode = ParsingMode::HEADERS;
|
||||||
$this->buffer = '';
|
$this->buffer = '';
|
||||||
}
|
}
|
||||||
|
@ -64,4 +75,6 @@ class ProtocolServer extends EventEmitter
|
||||||
{
|
{
|
||||||
fwrite($this->output, json_encode($res));
|
fwrite($this->output, json_encode($res));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
abstract public function initialize(InitializeRequest $req): InitializeResponse;
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,12 +15,18 @@ class Response extends Message
|
||||||
public $method;
|
public $method;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var object|null
|
* @var mixed
|
||||||
*/
|
*/
|
||||||
public $params;
|
public $result;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var ResponseError|null
|
* @var ResponseError|null
|
||||||
*/
|
*/
|
||||||
public $error;
|
public $error;
|
||||||
|
|
||||||
|
public function __construct($result, ResponseError $error = null)
|
||||||
|
{
|
||||||
|
$this->result = $result;
|
||||||
|
$this->error = $error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,9 @@
|
||||||
|
|
||||||
namespace LanguageServer\Protocol;
|
namespace LanguageServer\Protocol;
|
||||||
|
|
||||||
class ResponseError
|
use Exception;
|
||||||
|
|
||||||
|
class ResponseError extends Exception
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* A number indicating the error type that occurred.
|
* A number indicating the error type that occurred.
|
||||||
|
@ -25,4 +27,10 @@ class ResponseError
|
||||||
* @var mixed
|
* @var mixed
|
||||||
*/
|
*/
|
||||||
public $data;
|
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
|
* @var bool|null
|
||||||
*/
|
*/
|
||||||
public $renameProvidern;
|
public $renameProvider;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue