Refactor content retrieval
parent
10fb3c92e0
commit
2548d7cca8
|
@ -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 LanguageServer\Protocol\{SymbolInformation, TextDocumentIdentifier, ClientCapabilities};
|
||||||
use phpDocumentor\Reflection\DocBlockFactory;
|
use phpDocumentor\Reflection\DocBlockFactory;
|
||||||
|
use LanguageServer\ContentRetriever\{ContentRetriever, ClientContentRetriever, FileSystemContentRetriever};
|
||||||
use Sabre\Event\Promise;
|
use Sabre\Event\Promise;
|
||||||
use function Sabre\Event\coroutine;
|
use function Sabre\Event\coroutine;
|
||||||
|
|
||||||
|
@ -67,6 +68,13 @@ class Project
|
||||||
*/
|
*/
|
||||||
private $clientCapabilities;
|
private $clientCapabilities;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The content retriever
|
||||||
|
*
|
||||||
|
* @var ContentRetriever
|
||||||
|
*/
|
||||||
|
private $contentRetriever;
|
||||||
|
|
||||||
public function __construct(LanguageClient $client, ClientCapabilities $clientCapabilities)
|
public function __construct(LanguageClient $client, ClientCapabilities $clientCapabilities)
|
||||||
{
|
{
|
||||||
$this->client = $client;
|
$this->client = $client;
|
||||||
|
@ -74,6 +82,11 @@ class Project
|
||||||
$this->parser = new Parser;
|
$this->parser = new Parser;
|
||||||
$this->docBlockFactory = DocBlockFactory::createInstance();
|
$this->docBlockFactory = DocBlockFactory::createInstance();
|
||||||
$this->definitionResolver = new DefinitionResolver($this);
|
$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) {
|
return coroutine(function () use ($uri) {
|
||||||
$limit = 150000;
|
$limit = 150000;
|
||||||
if ($this->clientCapabilities->xcontentProvider) {
|
$content = yield $this->contentRetriever->retrieve($uri);
|
||||||
$content = (yield $this->client->textDocument->xcontent(new TextDocumentIdentifier($uri)))->text;
|
|
||||||
$size = strlen($content);
|
$size = strlen($content);
|
||||||
if ($size > $limit) {
|
if ($size > $limit) {
|
||||||
throw new ContentTooLargeException($uri, $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])) {
|
if (isset($this->documents[$uri])) {
|
||||||
$document = $this->documents[$uri];
|
$document = $this->documents[$uri];
|
||||||
|
|
Loading…
Reference in New Issue