1
0
Fork 0

Store text content in PHPDocument, removed stmts, regenerate on demand

pull/31/head
Stephan Unverwerth 2016-09-22 23:34:14 +02:00
parent 4d6774f09d
commit 28d6cc6e85
3 changed files with 32 additions and 18 deletions

View File

@ -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.");
}
};

View File

@ -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];
}
}

View File

@ -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);
}