Fixed formatting
parent
133e2c7913
commit
f1f87d8419
|
@ -4,14 +4,14 @@ declare(strict_types = 1);
|
||||||
namespace LanguageServer;
|
namespace LanguageServer;
|
||||||
|
|
||||||
use LanguageServer\Protocol\{
|
use LanguageServer\Protocol\{
|
||||||
ServerCapabilities,
|
ServerCapabilities,
|
||||||
ClientCapabilities,
|
ClientCapabilities,
|
||||||
TextDocumentSyncKind,
|
TextDocumentSyncKind,
|
||||||
Message,
|
Message,
|
||||||
MessageType,
|
MessageType,
|
||||||
InitializeResult,
|
InitializeResult,
|
||||||
TextDocumentIdentifier,
|
TextDocumentIdentifier,
|
||||||
CompletionOptions
|
CompletionOptions
|
||||||
};
|
};
|
||||||
use AdvancedJsonRpc;
|
use AdvancedJsonRpc;
|
||||||
use Sabre\Event\Promise;
|
use Sabre\Event\Promise;
|
||||||
|
@ -26,232 +26,232 @@ use function Sabre\Event\Loop\setTimeout;
|
||||||
|
|
||||||
class LanguageServer extends AdvancedJsonRpc\Dispatcher
|
class LanguageServer extends AdvancedJsonRpc\Dispatcher
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Handles textDocument/* method calls
|
* Handles textDocument/* method calls
|
||||||
*
|
*
|
||||||
* @var Server\TextDocument
|
* @var Server\TextDocument
|
||||||
*/
|
*/
|
||||||
public $textDocument;
|
public $textDocument;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles workspace/* method calls
|
* Handles workspace/* method calls
|
||||||
*
|
*
|
||||||
* @var Server\Workspace
|
* @var Server\Workspace
|
||||||
*/
|
*/
|
||||||
public $workspace;
|
public $workspace;
|
||||||
|
|
||||||
public $telemetry;
|
public $telemetry;
|
||||||
public $window;
|
public $window;
|
||||||
public $completionItem;
|
public $completionItem;
|
||||||
public $codeLens;
|
public $codeLens;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ClientCapabilities
|
* ClientCapabilities
|
||||||
*/
|
*/
|
||||||
private $clientCapabilities;
|
private $clientCapabilities;
|
||||||
|
|
||||||
private $protocolReader;
|
private $protocolReader;
|
||||||
private $protocolWriter;
|
private $protocolWriter;
|
||||||
private $client;
|
private $client;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The root project path that was passed to initialize()
|
* The root project path that was passed to initialize()
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $rootPath;
|
private $rootPath;
|
||||||
private $project;
|
private $project;
|
||||||
|
|
||||||
public function __construct(ProtocolReader $reader, ProtocolWriter $writer)
|
public function __construct(ProtocolReader $reader, ProtocolWriter $writer)
|
||||||
{
|
{
|
||||||
parent::__construct($this, '/');
|
parent::__construct($this, '/');
|
||||||
$this->protocolReader = $reader;
|
$this->protocolReader = $reader;
|
||||||
$this->protocolReader->on('close', function () {
|
$this->protocolReader->on('close', function () {
|
||||||
$this->shutdown();
|
$this->shutdown();
|
||||||
$this->exit();
|
$this->exit();
|
||||||
});
|
});
|
||||||
$this->protocolReader->on('message', function (Message $msg) {
|
$this->protocolReader->on('message', function (Message $msg) {
|
||||||
coroutine(function () use ($msg) {
|
coroutine(function () use ($msg) {
|
||||||
// Ignore responses, this is the handler for requests and notifications
|
// Ignore responses, this is the handler for requests and notifications
|
||||||
if (AdvancedJsonRpc\Response::isResponse($msg->body)) {
|
if (AdvancedJsonRpc\Response::isResponse($msg->body)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$result = null;
|
$result = null;
|
||||||
$error = null;
|
$error = null;
|
||||||
try {
|
try {
|
||||||
// Invoke the method handler to get a result
|
// Invoke the method handler to get a result
|
||||||
$result = yield $this->dispatch($msg->body);
|
$result = yield $this->dispatch($msg->body);
|
||||||
} catch (AdvancedJsonRpc\Error $e) {
|
} catch (AdvancedJsonRpc\Error $e) {
|
||||||
// If a ResponseError is thrown, send it back in the Response
|
// If a ResponseError is thrown, send it back in the Response
|
||||||
$error = $e;
|
$error = $e;
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
// If an unexpected error occured, send back an INTERNAL_ERROR error response
|
// If an unexpected error occured, send back an INTERNAL_ERROR error response
|
||||||
$error = new AdvancedJsonRpc\Error(
|
$error = new AdvancedJsonRpc\Error(
|
||||||
$e->getMessage(),
|
$e->getMessage(),
|
||||||
AdvancedJsonRpc\ErrorCode::INTERNAL_ERROR,
|
AdvancedJsonRpc\ErrorCode::INTERNAL_ERROR,
|
||||||
null,
|
null,
|
||||||
$e
|
$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 (AdvancedJsonRpc\Request::isRequest($msg->body)) {
|
if (AdvancedJsonRpc\Request::isRequest($msg->body)) {
|
||||||
if ($error !== null) {
|
if ($error !== null) {
|
||||||
$responseBody = new AdvancedJsonRpc\ErrorResponse($msg->body->id, $error);
|
$responseBody = new AdvancedJsonRpc\ErrorResponse($msg->body->id, $error);
|
||||||
} else {
|
} else {
|
||||||
$responseBody = new AdvancedJsonRpc\SuccessResponse($msg->body->id, $result);
|
$responseBody = new AdvancedJsonRpc\SuccessResponse($msg->body->id, $result);
|
||||||
}
|
}
|
||||||
$this->protocolWriter->write(new Message($responseBody));
|
$this->protocolWriter->write(new Message($responseBody));
|
||||||
}
|
}
|
||||||
})->otherwise('\\LanguageServer\\crash');
|
})->otherwise('\\LanguageServer\\crash');
|
||||||
});
|
});
|
||||||
$this->protocolWriter = $writer;
|
$this->protocolWriter = $writer;
|
||||||
$this->client = new LanguageClient($reader, $writer);
|
$this->client = new LanguageClient($reader, $writer);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The initialize request is sent as the first request from the client to the server.
|
* The initialize request is sent as the first request from the client to the server.
|
||||||
*
|
*
|
||||||
* @param ClientCapabilities $capabilities The capabilities provided by the client (editor)
|
* @param ClientCapabilities $capabilities The capabilities provided by the client (editor)
|
||||||
* @param string|null $rootPath The rootPath of the workspace. Is null if no folder is open.
|
* @param string|null $rootPath The rootPath of the workspace. Is null if no folder is open.
|
||||||
* @param int|null $processId The process Id of the parent process that started the server. Is null if the process has not been started by another process. If the parent process is not alive then the server should exit (see exit notification) its process.
|
* @param int|null $processId The process Id of the parent process that started the server. Is null if the process has not been started by another process. If the parent process is not alive then the server should exit (see exit notification) its process.
|
||||||
* @return InitializeResult
|
* @return InitializeResult
|
||||||
*/
|
*/
|
||||||
public function initialize(ClientCapabilities $capabilities, string $rootPath = null, int $processId = null): InitializeResult
|
public function initialize(ClientCapabilities $capabilities, string $rootPath = null, int $processId = null): InitializeResult
|
||||||
{
|
{
|
||||||
$this->rootPath = $rootPath;
|
$this->rootPath = $rootPath;
|
||||||
$this->clientCapabilities = $capabilities;
|
$this->clientCapabilities = $capabilities;
|
||||||
$this->project = new Project($this->client, $capabilities);
|
$this->project = new Project($this->client, $capabilities);
|
||||||
$this->textDocument = new Server\TextDocument($this->project, $this->client);
|
$this->textDocument = new Server\TextDocument($this->project, $this->client);
|
||||||
$this->workspace = new Server\Workspace($this->project, $this->client);
|
$this->workspace = new Server\Workspace($this->project, $this->client);
|
||||||
|
|
||||||
// start building project index
|
// start building project index
|
||||||
if ($rootPath !== null) {
|
if ($rootPath !== null) {
|
||||||
$this->indexProject()->otherwise('\\LanguageServer\\crash');
|
$this->indexProject()->otherwise('\\LanguageServer\\crash');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (extension_loaded('xdebug')) {
|
|
||||||
setTimeout(function () {
|
|
||||||
$this->client->window->showMessage(MessageType::WARNING, 'You are running PHP Language Server with xdebug enabled. This has a major impact on server performance.');
|
|
||||||
}, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
$serverCapabilities = new ServerCapabilities();
|
if (extension_loaded('xdebug')) {
|
||||||
// Ask the client to return always full documents (because we need to rebuild the AST from scratch)
|
setTimeout(function () {
|
||||||
$serverCapabilities->textDocumentSync = TextDocumentSyncKind::FULL;
|
$this->client->window->showMessage(MessageType::WARNING, 'You are running PHP Language Server with xdebug enabled. This has a major impact on server performance.');
|
||||||
// Support "Find all symbols"
|
}, 1);
|
||||||
$serverCapabilities->documentSymbolProvider = true;
|
}
|
||||||
// Support "Find all symbols in workspace"
|
|
||||||
$serverCapabilities->workspaceSymbolProvider = true;
|
|
||||||
// Support "Format Code"
|
|
||||||
$serverCapabilities->documentFormattingProvider = true;
|
|
||||||
// Support "Go to definition"
|
|
||||||
$serverCapabilities->definitionProvider = true;
|
|
||||||
// Support "Find all references"
|
|
||||||
$serverCapabilities->referencesProvider = true;
|
|
||||||
// Support "Hover"
|
|
||||||
$serverCapabilities->hoverProvider = true;
|
|
||||||
// Support "Completion"
|
|
||||||
$serverCapabilities->completionProvider = new CompletionOptions;
|
|
||||||
$serverCapabilities->completionProvider->resolveProvider = false;
|
|
||||||
$serverCapabilities->completionProvider->triggerCharacters = ['$', '>'];
|
|
||||||
|
|
||||||
return new InitializeResult($serverCapabilities);
|
$serverCapabilities = new ServerCapabilities();
|
||||||
}
|
// Ask the client to return always full documents (because we need to rebuild the AST from scratch)
|
||||||
|
$serverCapabilities->textDocumentSync = TextDocumentSyncKind::FULL;
|
||||||
|
// Support "Find all symbols"
|
||||||
|
$serverCapabilities->documentSymbolProvider = true;
|
||||||
|
// Support "Find all symbols in workspace"
|
||||||
|
$serverCapabilities->workspaceSymbolProvider = true;
|
||||||
|
// Support "Format Code"
|
||||||
|
$serverCapabilities->documentFormattingProvider = true;
|
||||||
|
// Support "Go to definition"
|
||||||
|
$serverCapabilities->definitionProvider = true;
|
||||||
|
// Support "Find all references"
|
||||||
|
$serverCapabilities->referencesProvider = true;
|
||||||
|
// Support "Hover"
|
||||||
|
$serverCapabilities->hoverProvider = true;
|
||||||
|
// Support "Completion"
|
||||||
|
$serverCapabilities->completionProvider = new CompletionOptions;
|
||||||
|
$serverCapabilities->completionProvider->resolveProvider = false;
|
||||||
|
$serverCapabilities->completionProvider->triggerCharacters = ['$', '>'];
|
||||||
|
|
||||||
/**
|
return new InitializeResult($serverCapabilities);
|
||||||
* The shutdown request is sent from the client to the server. It asks the server to shut down, but to not exit
|
}
|
||||||
* (otherwise the response might not be delivered correctly to the client). There is a separate exit notification that
|
|
||||||
* asks the server to exit.
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function shutdown()
|
|
||||||
{
|
|
||||||
unset($this->project);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A notification to ask the server to exit its process.
|
* The shutdown request is sent from the client to the server. It asks the server to shut down, but to not exit
|
||||||
*
|
* (otherwise the response might not be delivered correctly to the client). There is a separate exit notification that
|
||||||
* @return void
|
* asks the server to exit.
|
||||||
*/
|
*
|
||||||
public function exit()
|
* @return void
|
||||||
{
|
*/
|
||||||
exit(0);
|
public function shutdown()
|
||||||
}
|
{
|
||||||
|
unset($this->project);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses workspace files, one at a time.
|
* A notification to ask the server to exit its process.
|
||||||
*
|
*
|
||||||
* @return Promise <void>
|
* @return void
|
||||||
*/
|
*/
|
||||||
private function indexProject(): Promise
|
public function exit()
|
||||||
{
|
{
|
||||||
return coroutine(function () {
|
exit(0);
|
||||||
$textDocuments = yield $this->findPhpFiles();
|
}
|
||||||
$count = count($textDocuments);
|
|
||||||
|
|
||||||
$startTime = microtime(true);
|
/**
|
||||||
|
* Parses workspace files, one at a time.
|
||||||
|
*
|
||||||
|
* @return Promise <void>
|
||||||
|
*/
|
||||||
|
private function indexProject(): Promise
|
||||||
|
{
|
||||||
|
return coroutine(function () {
|
||||||
|
$textDocuments = yield $this->findPhpFiles();
|
||||||
|
$count = count($textDocuments);
|
||||||
|
|
||||||
foreach ($textDocuments as $i => $textDocument) {
|
$startTime = microtime(true);
|
||||||
// Give LS to the chance to handle requests while indexing
|
|
||||||
yield timeout();
|
|
||||||
$this->client->window->logMessage(
|
|
||||||
MessageType::LOG,
|
|
||||||
"Parsing file $i/$count: {$textDocument->uri}"
|
|
||||||
);
|
|
||||||
try {
|
|
||||||
yield $this->project->loadDocument($textDocument->uri);
|
|
||||||
} catch (ContentTooLargeException $e) {
|
|
||||||
$this->client->window->logMessage(
|
|
||||||
MessageType::INFO,
|
|
||||||
"Ignoring file {$textDocument->uri} because it exceeds size limit of {$e->limit} bytes ({$e->size})"
|
|
||||||
);
|
|
||||||
} catch (Exception $e) {
|
|
||||||
$this->client->window->logMessage(
|
|
||||||
MessageType::ERROR,
|
|
||||||
"Error parsing file {$textDocument->uri}: " . (string)$e
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$duration = (int)(microtime(true) - $startTime);
|
foreach ($textDocuments as $i => $textDocument) {
|
||||||
$mem = (int)(memory_get_usage(true) / (1024 * 1024));
|
// Give LS to the chance to handle requests while indexing
|
||||||
$this->client->window->logMessage(
|
yield timeout();
|
||||||
MessageType::INFO,
|
$this->client->window->logMessage(
|
||||||
"All $count PHP files parsed in $duration seconds. $mem MiB allocated."
|
MessageType::LOG,
|
||||||
);
|
"Parsing file $i/$count: {$textDocument->uri}"
|
||||||
});
|
);
|
||||||
}
|
try {
|
||||||
|
yield $this->project->loadDocument($textDocument->uri);
|
||||||
|
} catch (ContentTooLargeException $e) {
|
||||||
|
$this->client->window->logMessage(
|
||||||
|
MessageType::INFO,
|
||||||
|
"Ignoring file {$textDocument->uri} because it exceeds size limit of {$e->limit} bytes ({$e->size})"
|
||||||
|
);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$this->client->window->logMessage(
|
||||||
|
MessageType::ERROR,
|
||||||
|
"Error parsing file {$textDocument->uri}: " . (string)$e
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
$duration = (int)(microtime(true) - $startTime);
|
||||||
* Returns all PHP files in the workspace.
|
$mem = (int)(memory_get_usage(true) / (1024 * 1024));
|
||||||
* If the client does not support workspace/files, it falls back to searching the file system directly.
|
$this->client->window->logMessage(
|
||||||
*
|
MessageType::INFO,
|
||||||
* @return Promise <TextDocumentIdentifier[]>
|
"All $count PHP files parsed in $duration seconds. $mem MiB allocated."
|
||||||
*/
|
);
|
||||||
private function findPhpFiles(): Promise
|
});
|
||||||
{
|
}
|
||||||
return coroutine(function () {
|
|
||||||
$textDocuments = [];
|
/**
|
||||||
$pattern = Path::makeAbsolute('**/*.php', $this->rootPath);
|
* Returns all PHP files in the workspace.
|
||||||
if ($this->clientCapabilities->xfilesProvider) {
|
* If the client does not support workspace/files, it falls back to searching the file system directly.
|
||||||
// Use xfiles request
|
*
|
||||||
foreach (yield $this->client->workspace->xfiles() as $textDocument) {
|
* @return Promise <TextDocumentIdentifier[]>
|
||||||
$path = Uri\parse($textDocument->uri)['path'];
|
*/
|
||||||
if (Glob::match($path, $pattern)) {
|
private function findPhpFiles(): Promise
|
||||||
$textDocuments[] = $textDocument;
|
{
|
||||||
}
|
return coroutine(function () {
|
||||||
}
|
$textDocuments = [];
|
||||||
} else {
|
$pattern = Path::makeAbsolute('**/*.php', $this->rootPath);
|
||||||
// Use the file system
|
if ($this->clientCapabilities->xfilesProvider) {
|
||||||
foreach (new GlobIterator($pattern) as $path) {
|
// Use xfiles request
|
||||||
$textDocuments[] = new TextDocumentIdentifier(pathToUri($path));
|
foreach (yield $this->client->workspace->xfiles() as $textDocument) {
|
||||||
yield timeout();
|
$path = Uri\parse($textDocument->uri)['path'];
|
||||||
}
|
if (Glob::match($path, $pattern)) {
|
||||||
}
|
$textDocuments[] = $textDocument;
|
||||||
return $textDocuments;
|
}
|
||||||
});
|
}
|
||||||
}
|
} else {
|
||||||
|
// Use the file system
|
||||||
|
foreach (new GlobIterator($pattern) as $path) {
|
||||||
|
$textDocuments[] = new TextDocumentIdentifier(pathToUri($path));
|
||||||
|
yield timeout();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $textDocuments;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue