2016-08-22 15:32:31 +00:00
|
|
|
<?php
|
2016-09-30 09:54:49 +00:00
|
|
|
declare(strict_types = 1);
|
2016-08-22 15:32:31 +00:00
|
|
|
|
|
|
|
namespace LanguageServer;
|
|
|
|
|
2016-09-30 09:30:08 +00:00
|
|
|
use LanguageServer\Protocol\{
|
|
|
|
ServerCapabilities,
|
|
|
|
ClientCapabilities,
|
|
|
|
TextDocumentSyncKind,
|
|
|
|
Message,
|
|
|
|
MessageType,
|
2016-10-20 01:48:30 +00:00
|
|
|
InitializeResult,
|
2016-11-14 09:25:44 +00:00
|
|
|
SymbolInformation,
|
2016-11-30 21:23:51 +00:00
|
|
|
TextDocumentIdentifier,
|
|
|
|
CompletionOptions
|
2016-09-30 09:30:08 +00:00
|
|
|
};
|
2016-12-08 01:33:48 +00:00
|
|
|
use LanguageServer\FilesFinder\{FilesFinder, ClientFilesFinder, FileSystemFilesFinder};
|
|
|
|
use LanguageServer\ContentRetriever\{ContentRetriever, ClientContentRetriever, FileSystemContentRetriever};
|
2016-12-13 00:51:02 +00:00
|
|
|
use LanguageServer\Index\{DependenciesIndex, GlobalIndex, Index, ProjectIndex, StubsIndex};
|
2017-02-03 23:20:38 +00:00
|
|
|
use LanguageServer\Cache\{FileSystemCache, ClientCache};
|
2016-10-20 01:31:12 +00:00
|
|
|
use AdvancedJsonRpc;
|
2016-11-14 09:25:44 +00:00
|
|
|
use Sabre\Event\{Loop, Promise};
|
|
|
|
use function Sabre\Event\coroutine;
|
2016-10-19 10:41:53 +00:00
|
|
|
use Exception;
|
2016-10-20 01:31:12 +00:00
|
|
|
use Throwable;
|
2016-11-14 09:25:44 +00:00
|
|
|
use Webmozart\PathUtil\Path;
|
|
|
|
use Sabre\Uri;
|
2016-08-22 15:32:31 +00:00
|
|
|
|
2016-10-20 01:31:12 +00:00
|
|
|
class LanguageServer extends AdvancedJsonRpc\Dispatcher
|
2016-08-22 15:32:31 +00:00
|
|
|
{
|
2016-09-02 19:13:30 +00:00
|
|
|
/**
|
|
|
|
* Handles textDocument/* method calls
|
|
|
|
*
|
|
|
|
* @var Server\TextDocument
|
|
|
|
*/
|
2016-08-25 13:27:14 +00:00
|
|
|
public $textDocument;
|
2016-09-02 19:13:30 +00:00
|
|
|
|
2016-09-30 09:30:08 +00:00
|
|
|
/**
|
|
|
|
* Handles workspace/* method calls
|
|
|
|
*
|
|
|
|
* @var Server\Workspace
|
|
|
|
*/
|
|
|
|
public $workspace;
|
|
|
|
|
2016-12-13 00:51:02 +00:00
|
|
|
/**
|
|
|
|
* @var Server\Window
|
|
|
|
*/
|
2016-08-25 13:27:14 +00:00
|
|
|
public $window;
|
2016-12-13 00:51:02 +00:00
|
|
|
|
|
|
|
public $telemetry;
|
2016-08-25 13:27:14 +00:00
|
|
|
public $completionItem;
|
|
|
|
public $codeLens;
|
|
|
|
|
2016-12-13 00:51:02 +00:00
|
|
|
/**
|
|
|
|
* @var ProtocolReader
|
|
|
|
*/
|
2017-01-05 03:18:14 +00:00
|
|
|
protected $protocolReader;
|
2016-12-13 00:51:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ProtocolWriter
|
|
|
|
*/
|
2017-01-05 03:18:14 +00:00
|
|
|
protected $protocolWriter;
|
2016-08-25 13:27:14 +00:00
|
|
|
|
2016-10-20 01:48:30 +00:00
|
|
|
/**
|
2016-12-13 00:51:02 +00:00
|
|
|
* @var LanguageClient
|
2016-10-20 01:48:30 +00:00
|
|
|
*/
|
2017-01-05 03:18:14 +00:00
|
|
|
protected $client;
|
2016-09-30 09:30:08 +00:00
|
|
|
|
2016-12-08 01:33:48 +00:00
|
|
|
/**
|
|
|
|
* @var FilesFinder
|
|
|
|
*/
|
2017-01-05 03:18:14 +00:00
|
|
|
protected $filesFinder;
|
2016-12-08 01:33:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ContentRetriever
|
|
|
|
*/
|
2017-01-05 03:18:14 +00:00
|
|
|
protected $contentRetriever;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var PhpDocumentLoader
|
|
|
|
*/
|
|
|
|
protected $documentLoader;
|
2016-12-08 01:33:48 +00:00
|
|
|
|
2017-01-11 01:08:52 +00:00
|
|
|
/**
|
|
|
|
* The parsed composer.json file in the project, if any
|
|
|
|
*
|
|
|
|
* @var \stdClass
|
|
|
|
*/
|
|
|
|
protected $composerJson;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The parsed composer.lock file in the project, if any
|
|
|
|
*
|
|
|
|
* @var \stdClass
|
|
|
|
*/
|
|
|
|
protected $composerLock;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var GlobalIndex
|
|
|
|
*/
|
|
|
|
protected $globalIndex;
|
|
|
|
|
2017-01-25 00:38:11 +00:00
|
|
|
/**
|
|
|
|
* @var ProjectIndex
|
|
|
|
*/
|
|
|
|
protected $projectIndex;
|
|
|
|
|
2017-01-11 01:08:52 +00:00
|
|
|
/**
|
|
|
|
* @var DefinitionResolver
|
|
|
|
*/
|
|
|
|
protected $definitionResolver;
|
|
|
|
|
2016-12-13 00:51:02 +00:00
|
|
|
/**
|
|
|
|
* @param PotocolReader $reader
|
|
|
|
* @param ProtocolWriter $writer
|
|
|
|
*/
|
2016-08-25 13:27:14 +00:00
|
|
|
public function __construct(ProtocolReader $reader, ProtocolWriter $writer)
|
2016-08-22 15:32:31 +00:00
|
|
|
{
|
2016-08-25 13:27:14 +00:00
|
|
|
parent::__construct($this, '/');
|
|
|
|
$this->protocolReader = $reader;
|
2016-11-30 20:10:05 +00:00
|
|
|
$this->protocolReader->on('close', function () {
|
|
|
|
$this->shutdown();
|
|
|
|
$this->exit();
|
|
|
|
});
|
2016-10-31 10:47:21 +00:00
|
|
|
$this->protocolReader->on('message', function (Message $msg) {
|
2016-11-14 09:25:44 +00:00
|
|
|
coroutine(function () use ($msg) {
|
|
|
|
// Ignore responses, this is the handler for requests and notifications
|
|
|
|
if (AdvancedJsonRpc\Response::isResponse($msg->body)) {
|
|
|
|
return;
|
2016-10-20 01:36:03 +00:00
|
|
|
}
|
2016-11-14 09:25:44 +00:00
|
|
|
$result = null;
|
|
|
|
$error = null;
|
|
|
|
try {
|
|
|
|
// Invoke the method handler to get a result
|
|
|
|
$result = yield $this->dispatch($msg->body);
|
|
|
|
} catch (AdvancedJsonRpc\Error $e) {
|
|
|
|
// If a ResponseError is thrown, send it back in the Response
|
|
|
|
$error = $e;
|
|
|
|
} catch (Throwable $e) {
|
|
|
|
// If an unexpected error occured, send back an INTERNAL_ERROR error response
|
|
|
|
$error = new AdvancedJsonRpc\Error(
|
2016-12-13 00:51:02 +00:00
|
|
|
(string)$e,
|
2016-11-14 09:25:44 +00:00
|
|
|
AdvancedJsonRpc\ErrorCode::INTERNAL_ERROR,
|
|
|
|
null,
|
|
|
|
$e
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// Only send a Response for a Request
|
|
|
|
// Notifications do not send Responses
|
|
|
|
if (AdvancedJsonRpc\Request::isRequest($msg->body)) {
|
|
|
|
if ($error !== null) {
|
|
|
|
$responseBody = new AdvancedJsonRpc\ErrorResponse($msg->body->id, $error);
|
|
|
|
} else {
|
|
|
|
$responseBody = new AdvancedJsonRpc\SuccessResponse($msg->body->id, $result);
|
|
|
|
}
|
|
|
|
$this->protocolWriter->write(new Message($responseBody));
|
|
|
|
}
|
|
|
|
})->otherwise('\\LanguageServer\\crash');
|
2016-08-25 13:27:14 +00:00
|
|
|
});
|
|
|
|
$this->protocolWriter = $writer;
|
2016-10-31 10:47:21 +00:00
|
|
|
$this->client = new LanguageClient($reader, $writer);
|
2016-08-22 15:32:31 +00:00
|
|
|
}
|
2016-08-22 21:48:20 +00:00
|
|
|
|
2016-08-25 13:27:14 +00:00
|
|
|
/**
|
|
|
|
* 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)
|
2016-09-30 09:30:08 +00:00
|
|
|
* @param string|null $rootPath The rootPath of the workspace. Is null if no folder is open.
|
2016-11-23 17:38:57 +00:00
|
|
|
* @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.
|
2016-12-13 00:51:02 +00:00
|
|
|
* @return Promise <InitializeResult>
|
2016-08-25 13:27:14 +00:00
|
|
|
*/
|
2016-12-13 00:51:02 +00:00
|
|
|
public function initialize(ClientCapabilities $capabilities, string $rootPath = null, int $processId = null): Promise
|
2016-08-23 09:21:37 +00:00
|
|
|
{
|
2016-12-13 00:51:02 +00:00
|
|
|
return coroutine(function () use ($capabilities, $rootPath, $processId) {
|
|
|
|
|
|
|
|
if ($capabilities->xfilesProvider) {
|
|
|
|
$this->filesFinder = new ClientFilesFinder($this->client);
|
|
|
|
} else {
|
|
|
|
$this->filesFinder = new FileSystemFilesFinder;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($capabilities->xcontentProvider) {
|
|
|
|
$this->contentRetriever = new ClientContentRetriever($this->client);
|
|
|
|
} else {
|
|
|
|
$this->contentRetriever = new FileSystemContentRetriever;
|
|
|
|
}
|
|
|
|
|
2017-01-11 01:08:52 +00:00
|
|
|
$dependenciesIndex = new DependenciesIndex;
|
|
|
|
$sourceIndex = new Index;
|
2017-02-07 22:20:12 +00:00
|
|
|
$this->projectIndex = new ProjectIndex($sourceIndex, $dependenciesIndex, $this->composerJson);
|
2016-12-13 00:51:02 +00:00
|
|
|
$stubsIndex = StubsIndex::read();
|
2017-01-25 00:38:11 +00:00
|
|
|
$this->globalIndex = new GlobalIndex($stubsIndex, $this->projectIndex);
|
2016-12-13 00:51:02 +00:00
|
|
|
|
|
|
|
// The DefinitionResolver should look in stubs, the project source and dependencies
|
2017-01-11 01:08:52 +00:00
|
|
|
$this->definitionResolver = new DefinitionResolver($this->globalIndex);
|
2016-12-13 00:51:02 +00:00
|
|
|
|
|
|
|
$this->documentLoader = new PhpDocumentLoader(
|
|
|
|
$this->contentRetriever,
|
2017-01-25 00:38:11 +00:00
|
|
|
$this->projectIndex,
|
2017-01-11 01:08:52 +00:00
|
|
|
$this->definitionResolver
|
2016-12-13 00:51:02 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($rootPath !== null) {
|
2017-01-25 00:38:11 +00:00
|
|
|
yield $this->beforeIndex($rootPath);
|
2016-12-13 00:51:02 +00:00
|
|
|
|
2017-02-03 23:20:38 +00:00
|
|
|
// Find composer.json
|
|
|
|
if ($this->composerJson === null) {
|
|
|
|
$composerJsonFiles = yield $this->filesFinder->find(Path::makeAbsolute('**/composer.json', $rootPath));
|
2017-02-07 22:20:12 +00:00
|
|
|
sortUrisLevelOrder($composerJsonFiles);
|
|
|
|
|
2017-02-03 23:20:38 +00:00
|
|
|
if (!empty($composerJsonFiles)) {
|
|
|
|
$this->composerJson = json_decode(yield $this->contentRetriever->retrieve($composerJsonFiles[0]));
|
|
|
|
}
|
2017-01-11 01:08:52 +00:00
|
|
|
}
|
2017-02-03 23:20:38 +00:00
|
|
|
|
|
|
|
// Find composer.lock
|
|
|
|
if ($this->composerLock === null) {
|
|
|
|
$composerLockFiles = yield $this->filesFinder->find(Path::makeAbsolute('**/composer.lock', $rootPath));
|
2017-02-07 22:20:12 +00:00
|
|
|
sortUrisLevelOrder($composerLockFiles);
|
|
|
|
|
2017-02-03 23:20:38 +00:00
|
|
|
if (!empty($composerLockFiles)) {
|
|
|
|
$this->composerLock = json_decode(yield $this->contentRetriever->retrieve($composerLockFiles[0]));
|
|
|
|
}
|
2017-01-11 01:08:52 +00:00
|
|
|
}
|
2017-02-03 23:20:38 +00:00
|
|
|
|
|
|
|
$cache = $capabilities->xcacheProvider ? new ClientCache($this->client) : new FileSystemCache;
|
|
|
|
|
|
|
|
// Index in background
|
|
|
|
$indexer = new Indexer(
|
|
|
|
$this->filesFinder,
|
|
|
|
$rootPath,
|
|
|
|
$this->client,
|
|
|
|
$cache,
|
|
|
|
$dependenciesIndex,
|
|
|
|
$sourceIndex,
|
|
|
|
$this->documentLoader,
|
2017-02-07 22:20:12 +00:00
|
|
|
$this->composerLock,
|
|
|
|
$this->composerJson
|
2017-02-03 23:20:38 +00:00
|
|
|
);
|
|
|
|
$indexer->index()->otherwise('\\LanguageServer\\crash');
|
2017-01-11 01:08:52 +00:00
|
|
|
}
|
|
|
|
|
2017-02-03 23:20:38 +00:00
|
|
|
|
2017-01-11 01:08:52 +00:00
|
|
|
if ($this->textDocument === null) {
|
|
|
|
$this->textDocument = new Server\TextDocument(
|
|
|
|
$this->documentLoader,
|
|
|
|
$this->definitionResolver,
|
|
|
|
$this->client,
|
|
|
|
$this->globalIndex,
|
|
|
|
$this->composerJson,
|
|
|
|
$this->composerLock
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ($this->workspace === null) {
|
2017-01-25 00:38:11 +00:00
|
|
|
$this->workspace = new Server\Workspace(
|
2017-03-01 10:18:37 +00:00
|
|
|
$this->client,
|
2017-01-25 00:38:11 +00:00
|
|
|
$this->projectIndex,
|
|
|
|
$dependenciesIndex,
|
|
|
|
$sourceIndex,
|
|
|
|
$this->composerLock,
|
2017-02-07 22:20:12 +00:00
|
|
|
$this->documentLoader,
|
|
|
|
$this->composerJson
|
2017-01-25 00:38:11 +00:00
|
|
|
);
|
2017-01-11 01:08:52 +00:00
|
|
|
}
|
2016-12-13 00:51:02 +00:00
|
|
|
|
|
|
|
$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 = ['$', '>'];
|
2017-01-11 01:08:52 +00:00
|
|
|
// Support global references
|
|
|
|
$serverCapabilities->xworkspaceReferencesProvider = true;
|
|
|
|
$serverCapabilities->xdefinitionProvider = true;
|
|
|
|
$serverCapabilities->xdependenciesProvider = true;
|
2016-12-13 00:51:02 +00:00
|
|
|
|
|
|
|
return new InitializeResult($serverCapabilities);
|
|
|
|
});
|
2016-08-25 13:27:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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()
|
|
|
|
{
|
2016-11-14 09:25:44 +00:00
|
|
|
unset($this->project);
|
2016-08-25 13:27:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A notification to ask the server to exit its process.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function exit()
|
|
|
|
{
|
|
|
|
exit(0);
|
2016-08-23 09:21:37 +00:00
|
|
|
}
|
2016-09-30 09:30:08 +00:00
|
|
|
|
2017-01-25 00:38:11 +00:00
|
|
|
/**
|
|
|
|
* Called before indexing, can return a Promise
|
|
|
|
*
|
|
|
|
* @param string $rootPath
|
|
|
|
*/
|
|
|
|
protected function beforeIndex(string $rootPath)
|
|
|
|
{
|
|
|
|
}
|
2016-08-22 15:32:31 +00:00
|
|
|
}
|