From 28d6cc6e8567b081e4a9f3de23b4b7c747bc502f Mon Sep 17 00:00:00 2001 From: Stephan Unverwerth Date: Thu, 22 Sep 2016 23:34:14 +0200 Subject: [PATCH] Store text content in PHPDocument, removed stmts, regenerate on demand --- src/LanguageServer.php | 6 +++--- src/PhpDocument.php | 40 +++++++++++++++++++++++++------------ src/Server/TextDocument.php | 4 ++-- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/LanguageServer.php b/src/LanguageServer.php index ac36313..c9aea3f 100644 --- a/src/LanguageServer.php +++ b/src/LanguageServer.php @@ -142,14 +142,14 @@ class LanguageServer extends \AdvancedJsonRpc\Dispatcher $shortName = substr($file, strlen($rootPath)+1); $this->client->window->logMessage(3, "Parsing file $fileNum/$numTotalFiles: $shortName."); - $this->project->getDocument($uri)->updateAst(file_get_contents($file)); + $this->project->getDocument($uri)->updateContent(file_get_contents($file)); Loop\setTimeout($processFile, 0); } else { $duration = (int)(microtime(true) - $startTime); - $mem = memory_get_usage(true); - $this->client->window->logMessage(3, "All PHP files parsed in $duration seconds. $mem bytes allocated."); + $mem = (int)(memory_get_usage(true)/(1024*1024)); + $this->client->window->logMessage(3, "All PHP files parsed in $duration seconds. $mem MiB allocated."); } }; diff --git a/src/PhpDocument.php b/src/PhpDocument.php index a23b4de..a052a25 100644 --- a/src/PhpDocument.php +++ b/src/PhpDocument.php @@ -10,12 +10,13 @@ use PhpParser\NodeVisitor\NameResolver; class PhpDocument { - private $stmts; private $client; private $project; - private $symbols = []; private $parser; + private $uri; + private $content; + private $symbols = []; public function __construct(string $uri, Project $project, LanguageClient $client, Parser $parser) { @@ -49,16 +50,27 @@ class PhpDocument } /** - * Re-parses a source file, updates the AST and reports parsing errors that may occured as diagnostics + * Updates the content on this document. * - * @param string $content The new content of the source file - * @return void + * @param string $content */ - public function updateAst(string $content) + public function updateContent(string $content) + { + $this->content = $content; + $this->parse(); + } + + /** + * Re-parses a source file, updates symbols, reports parsing errors + * that may have occured as diagnostics and returns parsed nodes. + * + * @return \PhpParser\Node[] + */ + public function parse() { $stmts = null; try { - $stmts = $this->parser->parse($content); + $stmts = $this->parser->parse($this->content); } catch(Error $e) { // Parser still throws errors. e.g for unterminated comments @@ -68,8 +80,8 @@ class PhpDocument foreach ($this->parser->getErrors() as $error) { $diagnostic = new Diagnostic(); $diagnostic->range = new Range( - new Position($error->getStartLine() - 1, $error->hasColumnInfo() ? $error->getStartColumn($content) - 1 : 0), - new Position($error->getEndLine() - 1, $error->hasColumnInfo() ? $error->getEndColumn($content) : 0) + new Position($error->getStartLine() - 1, $error->hasColumnInfo() ? $error->getStartColumn($this->content) - 1 : 0), + new Position($error->getEndLine() - 1, $error->hasColumnInfo() ? $error->getEndColumn($this->content) : 0) ); $diagnostic->severity = DiagnosticSeverity::ERROR; $diagnostic->source = 'php'; @@ -84,13 +96,14 @@ class PhpDocument $traverser = new NodeTraverser; $finder = new SymbolFinder($this->uri); $traverser->addVisitor(new NameResolver); - $traverser->addVisitor(new ColumnCalculator($content)); + $traverser->addVisitor(new ColumnCalculator($this->content)); $traverser->addVisitor($finder); $traverser->traverse($stmts); - $this->stmts = $stmts; $this->symbols = $finder->symbols; } + + return $stmts; } /** @@ -100,13 +113,14 @@ class PhpDocument */ public function getFormattedText() { - if (empty($this->stmts)) { + $stmts = $this->parse(); + if (empty($stmts)) { return []; } $prettyPrinter = new PrettyPrinter(); $edit = new TextEdit(); $edit->range = new Range(new Position(0, 0), new Position(PHP_INT_MAX, PHP_INT_MAX)); - $edit->newText = $prettyPrinter->prettyPrintFile($this->stmts); + $edit->newText = $prettyPrinter->prettyPrintFile($stmts); return [$edit]; } } diff --git a/src/Server/TextDocument.php b/src/Server/TextDocument.php index c340b04..92a9cef 100644 --- a/src/Server/TextDocument.php +++ b/src/Server/TextDocument.php @@ -57,7 +57,7 @@ class TextDocument */ public function didOpen(TextDocumentItem $textDocument) { - $this->project->getDocument($textDocument->uri)->updateAst($textDocument->text); + $this->project->getDocument($textDocument->uri)->updateContent($textDocument->text); } /** @@ -69,7 +69,7 @@ class TextDocument */ public function didChange(VersionedTextDocumentIdentifier $textDocument, array $contentChanges) { - $this->project->getDocument($textDocument->uri)->updateAst($contentChanges[0]->text); + $this->project->getDocument($textDocument->uri)->updateContent($contentChanges[0]->text); }