From 4786fe173ce690eb176f847e89b0456df7d44596 Mon Sep 17 00:00:00 2001 From: Felix Becker Date: Sat, 8 Oct 2016 13:22:34 +0200 Subject: [PATCH] Decorate all nodes with parent, sibling references --- src/NodeVisitors/ReferencesAdder.php | 41 ++++++++++++++++++++++++++++ src/PhpDocument.php | 18 +++++++++--- 2 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 src/NodeVisitors/ReferencesAdder.php diff --git a/src/NodeVisitors/ReferencesAdder.php b/src/NodeVisitors/ReferencesAdder.php new file mode 100644 index 0000000..0d62a80 --- /dev/null +++ b/src/NodeVisitors/ReferencesAdder.php @@ -0,0 +1,41 @@ +stack)) { + $node->setAttribute('parentNode', end($this->stack)); + } + if (isset($this->previous) && $this->previous->getAttribute('parentNode') === $node->getAttribute('parentNode')) { + $node->setAttribute('previousSibling', $this->previous); + $this->previous->setAttribute('nextSibling', $node); + } + $this->stack[] = $node; + } + + public function leaveNode(Node $node) + { + $this->previous = $node; + array_pop($this->stack); + } +} diff --git a/src/PhpDocument.php b/src/PhpDocument.php index e394bc2..ac46a85 100644 --- a/src/PhpDocument.php +++ b/src/PhpDocument.php @@ -4,7 +4,7 @@ declare(strict_types = 1); namespace LanguageServer; use LanguageServer\Protocol\{Diagnostic, DiagnosticSeverity, Range, Position, SymbolKind, TextEdit}; -use LanguageServer\NodeVisitors\{SymbolFinder, ColumnCalculator}; +use LanguageServer\NodeVisitors\{ReferencesAdder, SymbolFinder, ColumnCalculator}; use PhpParser\{Error, Comment, Node, ParserFactory, NodeTraverser, Lexer, Parser}; use PhpParser\PrettyPrinter\Standard as PrettyPrinter; use PhpParser\NodeVisitor\NameResolver; @@ -138,13 +138,23 @@ class PhpDocument // $stmts can be null in case of a fatal parsing error if ($stmts) { $traverser = new NodeTraverser; - $finder = new SymbolFinder($this->uri); + + // Resolve aliased names to FQNs $traverser->addVisitor(new NameResolver); + + // Add parentNode, previousSibling, nextSibling attributes + $traverser->addVisitor(new ReferencesAdder); + + // Add column attributes to nodes $traverser->addVisitor(new ColumnCalculator($this->content)); - $traverser->addVisitor($finder); + + // Collect all symbols + $symbolFinder = new SymbolFinder($this->uri); + $traverser->addVisitor($symbolFinder); + $traverser->traverse($stmts); - $this->symbols = $finder->symbols; + $this->symbols = $symbolFinder->symbols; } return $stmts;