Store text content in PHPDocument, removed stmts, regenerate on demand
parent
4d6774f09d
commit
28d6cc6e85
|
@ -142,14 +142,14 @@ class LanguageServer extends \AdvancedJsonRpc\Dispatcher
|
||||||
$shortName = substr($file, strlen($rootPath)+1);
|
$shortName = substr($file, strlen($rootPath)+1);
|
||||||
$this->client->window->logMessage(3, "Parsing file $fileNum/$numTotalFiles: $shortName.");
|
$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);
|
Loop\setTimeout($processFile, 0);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$duration = (int)(microtime(true) - $startTime);
|
$duration = (int)(microtime(true) - $startTime);
|
||||||
$mem = memory_get_usage(true);
|
$mem = (int)(memory_get_usage(true)/(1024*1024));
|
||||||
$this->client->window->logMessage(3, "All PHP files parsed in $duration seconds. $mem bytes allocated.");
|
$this->client->window->logMessage(3, "All PHP files parsed in $duration seconds. $mem MiB allocated.");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -10,12 +10,13 @@ use PhpParser\NodeVisitor\NameResolver;
|
||||||
|
|
||||||
class PhpDocument
|
class PhpDocument
|
||||||
{
|
{
|
||||||
private $stmts;
|
|
||||||
private $client;
|
private $client;
|
||||||
private $project;
|
private $project;
|
||||||
private $symbols = [];
|
|
||||||
private $parser;
|
private $parser;
|
||||||
|
|
||||||
private $uri;
|
private $uri;
|
||||||
|
private $content;
|
||||||
|
private $symbols = [];
|
||||||
|
|
||||||
public function __construct(string $uri, Project $project, LanguageClient $client, Parser $parser)
|
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
|
* @param string $content
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
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;
|
$stmts = null;
|
||||||
try {
|
try {
|
||||||
$stmts = $this->parser->parse($content);
|
$stmts = $this->parser->parse($this->content);
|
||||||
}
|
}
|
||||||
catch(Error $e) {
|
catch(Error $e) {
|
||||||
// Parser still throws errors. e.g for unterminated comments
|
// Parser still throws errors. e.g for unterminated comments
|
||||||
|
@ -68,8 +80,8 @@ class PhpDocument
|
||||||
foreach ($this->parser->getErrors() as $error) {
|
foreach ($this->parser->getErrors() as $error) {
|
||||||
$diagnostic = new Diagnostic();
|
$diagnostic = new Diagnostic();
|
||||||
$diagnostic->range = new Range(
|
$diagnostic->range = new Range(
|
||||||
new Position($error->getStartLine() - 1, $error->hasColumnInfo() ? $error->getStartColumn($content) - 1 : 0),
|
new Position($error->getStartLine() - 1, $error->hasColumnInfo() ? $error->getStartColumn($this->content) - 1 : 0),
|
||||||
new Position($error->getEndLine() - 1, $error->hasColumnInfo() ? $error->getEndColumn($content) : 0)
|
new Position($error->getEndLine() - 1, $error->hasColumnInfo() ? $error->getEndColumn($this->content) : 0)
|
||||||
);
|
);
|
||||||
$diagnostic->severity = DiagnosticSeverity::ERROR;
|
$diagnostic->severity = DiagnosticSeverity::ERROR;
|
||||||
$diagnostic->source = 'php';
|
$diagnostic->source = 'php';
|
||||||
|
@ -84,13 +96,14 @@ class PhpDocument
|
||||||
$traverser = new NodeTraverser;
|
$traverser = new NodeTraverser;
|
||||||
$finder = new SymbolFinder($this->uri);
|
$finder = new SymbolFinder($this->uri);
|
||||||
$traverser->addVisitor(new NameResolver);
|
$traverser->addVisitor(new NameResolver);
|
||||||
$traverser->addVisitor(new ColumnCalculator($content));
|
$traverser->addVisitor(new ColumnCalculator($this->content));
|
||||||
$traverser->addVisitor($finder);
|
$traverser->addVisitor($finder);
|
||||||
$traverser->traverse($stmts);
|
$traverser->traverse($stmts);
|
||||||
|
|
||||||
$this->stmts = $stmts;
|
|
||||||
$this->symbols = $finder->symbols;
|
$this->symbols = $finder->symbols;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $stmts;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -100,13 +113,14 @@ class PhpDocument
|
||||||
*/
|
*/
|
||||||
public function getFormattedText()
|
public function getFormattedText()
|
||||||
{
|
{
|
||||||
if (empty($this->stmts)) {
|
$stmts = $this->parse();
|
||||||
|
if (empty($stmts)) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
$prettyPrinter = new PrettyPrinter();
|
$prettyPrinter = new PrettyPrinter();
|
||||||
$edit = new TextEdit();
|
$edit = new TextEdit();
|
||||||
$edit->range = new Range(new Position(0, 0), new Position(PHP_INT_MAX, PHP_INT_MAX));
|
$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];
|
return [$edit];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,7 +57,7 @@ class TextDocument
|
||||||
*/
|
*/
|
||||||
public function didOpen(TextDocumentItem $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)
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue