From 7322a6c658639e408e359252780ac4a4b1c35b6c Mon Sep 17 00:00:00 2001 From: Felix Becker Date: Sun, 9 Oct 2016 14:34:30 +0200 Subject: [PATCH] Add fromNode() factories and correct columns --- src/NodeVisitors/NodeAtPositionFinder.php | 5 +-- src/PhpDocument.php | 14 ++++----- src/Protocol/Location.php | 13 ++++++++ src/Protocol/Range.php | 16 ++++++++++ src/Protocol/SymbolInformation.php | 2 ++ src/Server/TextDocument.php | 8 +---- tests/Server/TextDocument/DefinitionTest.php | 32 ++++++++++---------- 7 files changed, 55 insertions(+), 35 deletions(-) diff --git a/src/NodeVisitors/NodeAtPositionFinder.php b/src/NodeVisitors/NodeAtPositionFinder.php index f68e39a..33f074e 100644 --- a/src/NodeVisitors/NodeAtPositionFinder.php +++ b/src/NodeVisitors/NodeAtPositionFinder.php @@ -34,10 +34,7 @@ class NodeAtPositionFinder extends NodeVisitorAbstract public function leaveNode(Node $node) { - $range = new Range( - new Position($node->getAttribute('startLine') - 1, $node->getAttribute('startColumn') - 1), - new Position($node->getAttribute('endLine') - 1, $node->getAttribute('endColumn') - 1) - ); + $range = Range::fromNode($node); // Workaround for https://github.com/nikic/PHP-Parser/issues/311 $parent = $node->getAttribute('parentNode'); if (isset($parent) && $parent instanceof Node\Stmt\GroupUse && $parent->prefix === $node) { diff --git a/src/PhpDocument.php b/src/PhpDocument.php index b378da4..12bae1b 100644 --- a/src/PhpDocument.php +++ b/src/PhpDocument.php @@ -113,16 +113,14 @@ class PhpDocument ]; $symbols = []; foreach ($this->definitions as $fqn => $node) { + $class = get_class($node); + if (!isset($nodeSymbolKindMap[$class])) { + continue; + } $symbol = new SymbolInformation(); - $symbol->kind = $nodeSymbolKindMap[get_class($node)]; + $symbol->kind = $nodeSymbolKindMap[$class]; $symbol->name = (string)$node->name; - $symbol->location = new Location( - $this->getUri(), - new Range( - new Position($node->getAttribute('startLine') - 1, $node->getAttribute('startColumn') - 1), - new Position($node->getAttribute('endLine') - 1, $node->getAttribute('endColumn')) - ) - ); + $symbol->location = Location::fromNode($node); $parts = preg_split('/(::|\\\\)/', $fqn); array_pop($parts); $symbol->containerName = implode('\\', $parts); diff --git a/src/Protocol/Location.php b/src/Protocol/Location.php index d6bdc14..a1d9861 100644 --- a/src/Protocol/Location.php +++ b/src/Protocol/Location.php @@ -2,6 +2,8 @@ namespace LanguageServer\Protocol; +use PhpParser\Node; + /** * Represents a location inside a resource, such as a line inside a text file. */ @@ -17,6 +19,17 @@ class Location */ public $range; + /** + * Returns the location of the node + * + * @param Node $node + * @return self + */ + public static function fromNode(Node $node) + { + return new self($node->getAttribute('ownerDocument')->getUri(), Range::fromNode($node)); + } + public function __construct(string $uri = null, Range $range = null) { $this->uri = $uri; diff --git a/src/Protocol/Range.php b/src/Protocol/Range.php index 56dba0c..17f31da 100644 --- a/src/Protocol/Range.php +++ b/src/Protocol/Range.php @@ -2,6 +2,8 @@ namespace LanguageServer\Protocol; +use PhpParser\Node; + /** * A range in a text document expressed as (zero-based) start and end positions. */ @@ -21,6 +23,20 @@ class Range */ public $end; + /** + * Returns the range the node spans + * + * @param Node $node + * @return self + */ + public static function fromNode(Node $node) + { + return new self( + new Position($node->getAttribute('startLine') - 1, $node->getAttribute('startColumn') - 1), + new Position($node->getAttribute('endLine') - 1, $node->getAttribute('endColumn')) + ); + } + public function __construct(Position $start = null, Position $end = null) { $this->start = $start; diff --git a/src/Protocol/SymbolInformation.php b/src/Protocol/SymbolInformation.php index 12e9ccc..e02fd9b 100644 --- a/src/Protocol/SymbolInformation.php +++ b/src/Protocol/SymbolInformation.php @@ -2,6 +2,8 @@ namespace LanguageServer\Protocol; +use PhpParser\Node; + /** * Represents information about programming constructs like variables, classes, * interfaces etc. diff --git a/src/Server/TextDocument.php b/src/Server/TextDocument.php index aaf3c25..39a9699 100644 --- a/src/Server/TextDocument.php +++ b/src/Server/TextDocument.php @@ -109,12 +109,6 @@ class TextDocument if ($def === null) { return null; } - return new Location( - $def->getAttribute('ownerDocument')->getUri(), - new Range( - new Position($def->getAttribute('startLine') - 1, $def->getAttribute('startColumn') - 1), - new Position($def->getAttribute('endLine') - 1, $def->getAttribute('endColumn') - 1) - ) - ); + return Location::fromNode($def); } } diff --git a/tests/Server/TextDocument/DefinitionTest.php b/tests/Server/TextDocument/DefinitionTest.php index eca8722..948fb35 100644 --- a/tests/Server/TextDocument/DefinitionTest.php +++ b/tests/Server/TextDocument/DefinitionTest.php @@ -39,7 +39,7 @@ class DefinitionTest extends TestCase ], 'end' => [ 'line' => 21, - 'character' => 0 + 'character' => 1 ] ] ], json_decode(json_encode($result), true)); @@ -59,7 +59,7 @@ class DefinitionTest extends TestCase ], 'end' => [ 'line' => 21, - 'character' => 0 + 'character' => 1 ] ] ], json_decode(json_encode($result), true)); @@ -79,7 +79,7 @@ class DefinitionTest extends TestCase ], 'end' => [ 'line' => 31, - 'character' => 0 + 'character' => 1 ] ] ], json_decode(json_encode($result), true)); @@ -99,7 +99,7 @@ class DefinitionTest extends TestCase ], 'end' => [ 'line' => 31, - 'character' => 0 + 'character' => 1 ] ] ], json_decode(json_encode($result), true)); @@ -119,7 +119,7 @@ class DefinitionTest extends TestCase ], 'end' => [ 'line' => 8, - 'character' => 31 + 'character' => 32 ] ] ], json_decode(json_encode($result), true)); @@ -139,7 +139,7 @@ class DefinitionTest extends TestCase ], 'end' => [ 'line' => 4, - 'character' => 21 + 'character' => 22 ] ] ], json_decode(json_encode($result), true)); @@ -159,7 +159,7 @@ class DefinitionTest extends TestCase ], 'end' => [ 'line' => 15, - 'character' => 4 + 'character' => 5 ] ] ], json_decode(json_encode($result), true)); @@ -179,7 +179,7 @@ class DefinitionTest extends TestCase ], 'end' => [ 'line' => 9, - 'character' => 36 + 'character' => 37 ] ] ], json_decode(json_encode($result), true)); @@ -199,7 +199,7 @@ class DefinitionTest extends TestCase ], 'end' => [ 'line' => 20, - 'character' => 4 + 'character' => 5 ] ] ], json_decode(json_encode($result), true)); @@ -219,7 +219,7 @@ class DefinitionTest extends TestCase ], 'end' => [ 'line' => 10, - 'character' => 23 + 'character' => 24 ] ] ], json_decode(json_encode($result), true)); @@ -239,7 +239,7 @@ class DefinitionTest extends TestCase ], 'end' => [ 'line' => 12, - 'character' => 9 + 'character' => 10 ] ] ], json_decode(json_encode($result), true)); @@ -259,7 +259,7 @@ class DefinitionTest extends TestCase ], 'end' => [ 'line' => 21, - 'character' => 0 + 'character' => 1 ] ] ], json_decode(json_encode($result), true)); @@ -278,7 +278,7 @@ class DefinitionTest extends TestCase ], 'end' => [ 'line' => 21, - 'character' => 0 + 'character' => 1 ] ] ], json_decode(json_encode($result), true)); @@ -298,7 +298,7 @@ class DefinitionTest extends TestCase ], 'end' => [ 'line' => 15, - 'character' => 33 + 'character' => 34 ] ] ], json_decode(json_encode($result), true)); @@ -318,7 +318,7 @@ class DefinitionTest extends TestCase ], 'end' => [ 'line' => 19, - 'character' => 25 + 'character' => 26 ] ] ], json_decode(json_encode($result), true)); @@ -338,7 +338,7 @@ class DefinitionTest extends TestCase ], 'end' => [ 'line' => 36, - 'character' => 0 + 'character' => 1 ] ] ], json_decode(json_encode($result), true));