diff --git a/src/ContentRetriever/ClientContentRetriever.php b/src/ContentRetriever/ClientContentRetriever.php new file mode 100644 index 0000000..b88042c --- /dev/null +++ b/src/ContentRetriever/ClientContentRetriever.php @@ -0,0 +1,36 @@ +client = $client; + } + + /** + * Retrieves the content of a text document identified by the URI through a textDocument/xcontent request + * + * @param string $uri The URI of the document + * @return Promise Resolved with the content as a string + */ + public function retrieve(string $uri): Promise + { + return $this->client->textDocument->xcontent(new TextDocumentIdentifier($uri)) + ->then(function (TextDocumentItem $textDocument) { + return $textDocument->text; + }); + } +} diff --git a/src/ContentRetriever/ContentRetriever.php b/src/ContentRetriever/ContentRetriever.php new file mode 100644 index 0000000..4d16b98 --- /dev/null +++ b/src/ContentRetriever/ContentRetriever.php @@ -0,0 +1,20 @@ + Resolved with the content as a string + */ + public function retrieve(string $uri): Promise; +} diff --git a/src/ContentRetriever/FileSystemContentRetriever.php b/src/ContentRetriever/FileSystemContentRetriever.php new file mode 100644 index 0000000..82e7002 --- /dev/null +++ b/src/ContentRetriever/FileSystemContentRetriever.php @@ -0,0 +1,24 @@ + Resolved with the content as a string + */ + public function retrieve(string $uri): Promise + { + return Promise\resolve(file_get_contents(uriToPath($uri))); + } +} diff --git a/src/Project.php b/src/Project.php index 3021a1c..2544fa7 100644 --- a/src/Project.php +++ b/src/Project.php @@ -5,6 +5,7 @@ namespace LanguageServer; use LanguageServer\Protocol\{SymbolInformation, TextDocumentIdentifier, ClientCapabilities}; use phpDocumentor\Reflection\DocBlockFactory; +use LanguageServer\ContentRetriever\{ContentRetriever, ClientContentRetriever, FileSystemContentRetriever}; use Sabre\Event\Promise; use function Sabre\Event\coroutine; @@ -67,6 +68,13 @@ class Project */ private $clientCapabilities; + /** + * The content retriever + * + * @var ContentRetriever + */ + private $contentRetriever; + public function __construct(LanguageClient $client, ClientCapabilities $clientCapabilities) { $this->client = $client; @@ -74,6 +82,11 @@ class Project $this->parser = new Parser; $this->docBlockFactory = DocBlockFactory::createInstance(); $this->definitionResolver = new DefinitionResolver($this); + if ($clientCapabilities->xcontentProvider) { + $this->contentRetriever = new ClientContentRetriever($client); + } else { + $this->contentRetriever = new FileSystemContentRetriever; + } } /** @@ -112,19 +125,10 @@ class Project { return coroutine(function () use ($uri) { $limit = 150000; - if ($this->clientCapabilities->xcontentProvider) { - $content = (yield $this->client->textDocument->xcontent(new TextDocumentIdentifier($uri)))->text; + $content = yield $this->contentRetriever->retrieve($uri); $size = strlen($content); if ($size > $limit) { throw new ContentTooLargeException($uri, $size, $limit); - } - } else { - $path = uriToPath($uri); - $size = filesize($path); - if ($size > $limit) { - throw new ContentTooLargeException($uri, $size, $limit); - } - $content = file_get_contents($path); } if (isset($this->documents[$uri])) { $document = $this->documents[$uri];