Refactor content retrieval (#208)
parent
db6f4f7e5d
commit
ebd1cc6133
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace LanguageServer\ContentRetriever;
|
||||
|
||||
use LanguageServer\LanguageClient;
|
||||
use LanguageServer\Protocol\{TextDocumentIdentifier, TextDocumentItem};
|
||||
use Sabre\Event\Promise;
|
||||
|
||||
/**
|
||||
* Retrieves file content from the client through a textDocument/xcontent request
|
||||
*/
|
||||
class ClientContentRetriever implements ContentRetriever
|
||||
{
|
||||
/**
|
||||
* @param LanguageClient $client
|
||||
*/
|
||||
public function __construct(LanguageClient $client)
|
||||
{
|
||||
$this->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 <string> 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;
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace LanguageServer\ContentRetriever;
|
||||
|
||||
use Sabre\Event\Promise;
|
||||
|
||||
/**
|
||||
* Interface for retrieving the content of a text document
|
||||
*/
|
||||
interface ContentRetriever
|
||||
{
|
||||
/**
|
||||
* Retrieves the content of a text document identified by the URI
|
||||
*
|
||||
* @param string $uri The URI of the document
|
||||
* @return Promise <string> Resolved with the content as a string
|
||||
*/
|
||||
public function retrieve(string $uri): Promise;
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace LanguageServer\ContentRetriever;
|
||||
|
||||
use Sabre\Event\Promise;
|
||||
use function LanguageServer\uriToPath;
|
||||
|
||||
/**
|
||||
* Retrieves document content from the file system
|
||||
*/
|
||||
class FileSystemContentRetriever implements ContentRetriever
|
||||
{
|
||||
/**
|
||||
* Retrieves the content of a text document identified by the URI from the file system
|
||||
*
|
||||
* @param string $uri The URI of the document
|
||||
* @return Promise <string> Resolved with the content as a string
|
||||
*/
|
||||
public function retrieve(string $uri): Promise
|
||||
{
|
||||
return Promise\resolve(file_get_contents(uriToPath($uri)));
|
||||
}
|
||||
}
|
|
@ -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,20 +125,11 @@ 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];
|
||||
$document->updateContent($content);
|
||||
|
|
Loading…
Reference in New Issue