1
0
Fork 0

Add size limit back (#161)

pull/162/head
Felix Becker 2016-11-18 13:24:26 +01:00 committed by GitHub
parent 32b01afa90
commit 12df6a7dd6
3 changed files with 62 additions and 2 deletions

View File

@ -0,0 +1,45 @@
<?php
declare(strict_types = 1);
namespace LanguageServer;
/**
* Thrown when the document content is not parsed because it exceeds the size limit
*/
class ContentTooLargeException extends \Exception
{
/**
* The URI of the file that exceeded the limit
*
* @var string
*/
public $uri;
/**
* The size of the file in bytes
*
* @var int
*/
public $size;
/**
* The limit that was exceeded in bytes
*
* @var int
*/
public $limit;
/**
* @param string $uri The URI of the file that exceeded the limit
* @param int $size The size of the file in bytes
* @param int $limit The limit that was exceeded in bytes
* @param \Throwable $previous The previous exception used for the exception chaining.
*/
public function __construct(string $uri, int $size, int $limit, \Throwable $previous = null)
{
$this->uri = $uri;
$this->size = $size;
$this->limit = $limit;
parent::__construct("$uri exceeds size limit of $limit bytes ($size)", 0, $previous);
}
}

View File

@ -183,11 +183,16 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher
// Give LS to the chance to handle requests while indexing // Give LS to the chance to handle requests while indexing
yield timeout(); yield timeout();
$this->client->window->logMessage( $this->client->window->logMessage(
MessageType::INFO, MessageType::LOG,
"Parsing file $i/$count: {$textDocument->uri}" "Parsing file $i/$count: {$textDocument->uri}"
); );
try { try {
yield $this->project->loadDocument($textDocument->uri); 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) { } catch (Exception $e) {
$this->client->window->logMessage( $this->client->window->logMessage(
MessageType::ERROR, MessageType::ERROR,

View File

@ -103,10 +103,20 @@ class Project
public function loadDocument(string $uri): Promise public function loadDocument(string $uri): Promise
{ {
return coroutine(function () use ($uri) { return coroutine(function () use ($uri) {
$limit = 150000;
if ($this->clientCapabilities->xcontentProvider) { if ($this->clientCapabilities->xcontentProvider) {
$content = (yield $this->client->textDocument->xcontent(new TextDocumentIdentifier($uri)))->text; $content = (yield $this->client->textDocument->xcontent(new TextDocumentIdentifier($uri)))->text;
$size = strlen($content);
if ($size > $limit) {
throw new ContentTooLargeException($uri, $size, $limit);
}
} else { } else {
$content = file_get_contents(uriToPath($uri)); $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];