From 13241a7be995824f7d8bc2fe96b6cdf673e01a51 Mon Sep 17 00:00:00 2001 From: Sara Itani Date: Thu, 2 Feb 2017 16:43:33 -0800 Subject: [PATCH] initialize tolerant-php-parser --- .gitignore | 1 + composer.json | 9 ++++++++- src/CompletionProvider.php | 1 + src/ComposerScripts.php | 4 +++- src/PhpDocument.php | 7 +++++-- src/PhpDocumentLoader.php | 8 ++++++++ src/Server/TextDocument.php | 7 ------- tests/NodeVisitor/DefinitionCollectorTest.php | 7 +++++-- tests/PhpDocumentTest.php | 4 +++- 9 files changed, 34 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 4791ea2..e7997bf 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ vendor/ .phpls/ composer.lock stubs +*.ast \ No newline at end of file diff --git a/composer.json b/composer.json index 268a989..7792440 100644 --- a/composer.json +++ b/composer.json @@ -38,9 +38,16 @@ "webmozart/glob": "^4.1", "sabre/uri": "^2.0", "jetbrains/phpstorm-stubs": "dev-master", - "composer/composer": "^1.3" + "composer/composer": "^1.3", + "Microsoft/tolerant-php-parser": "master" }, "minimum-stability": "dev", + "repositories": [ + { + "type": "git", + "url": "https://github.com/Microsoft/tolerant-php-parser.git" + } + ], "prefer-stable": true, "autoload": { "psr-4": { diff --git a/src/CompletionProvider.php b/src/CompletionProvider.php index 99a5d57..2e3443e 100644 --- a/src/CompletionProvider.php +++ b/src/CompletionProvider.php @@ -121,6 +121,7 @@ class CompletionProvider */ public function provideCompletion(PhpDocument $doc, Position $pos): CompletionList { + // This can be made much more performant if the tree follows specific invariants. $node = $doc->getNodeAtPosition($pos); if ($node instanceof Node\Expr\Error) { diff --git a/src/ComposerScripts.php b/src/ComposerScripts.php index c84a176..fa321bf 100644 --- a/src/ComposerScripts.php +++ b/src/ComposerScripts.php @@ -10,6 +10,7 @@ use phpDocumentor\Reflection\DocBlockFactory; use Webmozart\PathUtil\Path; use Sabre\Uri; use function Sabre\Event\coroutine; +use Microsoft\PhpParser as Tolerant; foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../autoload.php', __DIR__ . '/../vendor/autoload.php'] as $file) { if (file_exists($file)) { @@ -30,6 +31,7 @@ class ComposerScripts $contentRetriever = new FileSystemContentRetriever; $docBlockFactory = DocBlockFactory::createInstance(); $parser = new Parser; + $tolerantParser = new Tolerant\Parser(); $definitionResolver = new DefinitionResolver($index); $stubsLocation = null; @@ -55,7 +57,7 @@ class ComposerScripts $parts['scheme'] = 'phpstubs'; $uri = Uri\build($parts); - $document = new PhpDocument($uri, $content, $index, $parser, $docBlockFactory, $definitionResolver); + $document = new PhpDocument($uri, $content, $index, $parser, $tolerantParser, $docBlockFactory, $definitionResolver); } $index->setComplete(); diff --git a/src/PhpDocument.php b/src/PhpDocument.php index 3a25c23..b8110f9 100644 --- a/src/PhpDocument.php +++ b/src/PhpDocument.php @@ -17,6 +17,7 @@ use PhpParser\{Error, ErrorHandler, Node, NodeTraverser}; use PhpParser\NodeVisitor\NameResolver; use phpDocumentor\Reflection\DocBlockFactory; use Sabre\Uri; +use Microsoft\PhpParser as Tolerant; class PhpDocument { @@ -108,12 +109,14 @@ class PhpDocument string $content, Index $index, Parser $parser, + Tolerant\Parser $tolerantParser, DocBlockFactory $docBlockFactory, DefinitionResolver $definitionResolver ) { $this->uri = $uri; $this->index = $index; $this->parser = $parser; + $this->tolerantParser = $tolerantParser; $this->docBlockFactory = $docBlockFactory; $this->definitionResolver = $definitionResolver; $this->updateContent($content); @@ -133,7 +136,7 @@ class PhpDocument /** * Updates the content on this document. * Re-parses a source file, updates symbols and reports parsing errors - * that may have occured as diagnostics. + * that may have occurred as diagnostics. * * @param string $content * @return void @@ -168,7 +171,7 @@ class PhpDocument $this->diagnostics[] = Diagnostic::fromError($error, $this->content, DiagnosticSeverity::ERROR, 'php'); } - // $stmts can be null in case of a fatal parsing error + // $stmts can be null in case of a fatal parsing error <- Interesting. When do fatal parsing errors occur? if ($stmts) { $traverser = new NodeTraverser; diff --git a/src/PhpDocumentLoader.php b/src/PhpDocumentLoader.php index 728225d..64bd35c 100644 --- a/src/PhpDocumentLoader.php +++ b/src/PhpDocumentLoader.php @@ -8,6 +8,7 @@ use LanguageServer\Index\ProjectIndex; use phpDocumentor\Reflection\DocBlockFactory; use Sabre\Event\Promise; use function Sabre\Event\coroutine; +use Microsoft\PhpParser as Tolerant; /** * Takes care of loading documents and managing "open" documents @@ -36,6 +37,11 @@ class PhpDocumentLoader */ private $parser; + /** + * @var Tolerant\Parser + */ + private $tolerantParser; + /** * @var DocBlockFactory */ @@ -60,6 +66,7 @@ class PhpDocumentLoader $this->projectIndex = $projectIndex; $this->definitionResolver = $definitionResolver; $this->parser = new Parser; + $this->tolerantParser = new Tolerant\Parser(); $this->docBlockFactory = DocBlockFactory::createInstance(); } @@ -130,6 +137,7 @@ class PhpDocumentLoader $content, $this->projectIndex->getIndexForUri($uri), $this->parser, + $this->tolerantParser, $this->docBlockFactory, $this->definitionResolver ); diff --git a/src/Server/TextDocument.php b/src/Server/TextDocument.php index aa76ec2..3141a99 100644 --- a/src/Server/TextDocument.php +++ b/src/Server/TextDocument.php @@ -3,7 +3,6 @@ declare(strict_types = 1); namespace LanguageServer\Server; -use PhpParser\PrettyPrinter\Standard as PrettyPrinter; use PhpParser\{Node, NodeTraverser}; use LanguageServer\{LanguageClient, PhpDocumentLoader, PhpDocument, DefinitionResolver, CompletionProvider}; use LanguageServer\NodeVisitor\VariableReferencesCollector; @@ -49,11 +48,6 @@ class TextDocument */ protected $project; - /** - * @var PrettyPrinter - */ - protected $prettyPrinter; - /** * @var DefinitionResolver */ @@ -97,7 +91,6 @@ class TextDocument ) { $this->documentLoader = $documentLoader; $this->client = $client; - $this->prettyPrinter = new PrettyPrinter(); $this->definitionResolver = $definitionResolver; $this->completionProvider = new CompletionProvider($this->definitionResolver, $index); $this->index = $index; diff --git a/tests/NodeVisitor/DefinitionCollectorTest.php b/tests/NodeVisitor/DefinitionCollectorTest.php index 3ee9995..7243ad3 100644 --- a/tests/NodeVisitor/DefinitionCollectorTest.php +++ b/tests/NodeVisitor/DefinitionCollectorTest.php @@ -14,6 +14,7 @@ use LanguageServer\Index\{ProjectIndex, Index, DependenciesIndex}; use LanguageServer\Tests\MockProtocolStream; use LanguageServer\NodeVisitor\{ReferencesAdder, DefinitionCollector}; use function LanguageServer\pathToUri; +use Microsoft\PhpParser as Tolerant; class DefinitionCollectorTest extends TestCase { @@ -22,11 +23,12 @@ class DefinitionCollectorTest extends TestCase $path = realpath(__DIR__ . '/../../fixtures/symbols.php'); $uri = pathToUri($path); $parser = new Parser; + $tolerantParser = new Tolerant\Parser(); $docBlockFactory = DocBlockFactory::createInstance(); $index = new Index; $definitionResolver = new DefinitionResolver($index); $content = file_get_contents($path); - $document = new PhpDocument($uri, $content, $index, $parser, $docBlockFactory, $definitionResolver); + $document = new PhpDocument($uri, $content, $index, $parser, $tolerantParser, $docBlockFactory, $definitionResolver); $stmts = $parser->parse($content); $traverser = new NodeTraverser; @@ -70,11 +72,12 @@ class DefinitionCollectorTest extends TestCase $path = realpath(__DIR__ . '/../../fixtures/references.php'); $uri = pathToUri($path); $parser = new Parser; + $tolerantParser = new Tolerant\Parser(); $docBlockFactory = DocBlockFactory::createInstance(); $index = new Index; $definitionResolver = new DefinitionResolver($index); $content = file_get_contents($path); - $document = new PhpDocument($uri, $content, $index, $parser, $docBlockFactory, $definitionResolver); + $document = new PhpDocument($uri, $content, $index, $parser, $tolerantParser, $docBlockFactory, $definitionResolver); $stmts = $parser->parse($content); $traverser = new NodeTraverser; diff --git a/tests/PhpDocumentTest.php b/tests/PhpDocumentTest.php index 011a231..81f998f 100644 --- a/tests/PhpDocumentTest.php +++ b/tests/PhpDocumentTest.php @@ -13,16 +13,18 @@ use LanguageServer\Protocol\{SymbolKind, Position, ClientCapabilities}; use LanguageServer\Index\{Index, ProjectIndex, DependenciesIndex}; use PhpParser\Node; use function LanguageServer\isVendored; +use Microsoft\PhpParser as Tolerant; class PhpDocumentTest extends TestCase { public function createDocument(string $uri, string $content) { $parser = new Parser; + $tolerantParser = new Tolerant\Parser(); $docBlockFactory = DocBlockFactory::createInstance(); $index = new Index; $definitionResolver = new DefinitionResolver($index); - return new PhpDocument($uri, $content, $index, $parser, $docBlockFactory, $definitionResolver); + return new PhpDocument($uri, $content, $index, $parser, $tolerantParser, $docBlockFactory, $definitionResolver); } public function testParsesVariableVariables()