1
0
Fork 0

Add fromNode() factories and correct columns

pull/49/head
Felix Becker 2016-10-09 14:34:30 +02:00
parent 3a880934e5
commit 7322a6c658
7 changed files with 55 additions and 35 deletions

View File

@ -34,10 +34,7 @@ class NodeAtPositionFinder extends NodeVisitorAbstract
public function leaveNode(Node $node) public function leaveNode(Node $node)
{ {
$range = new Range( $range = Range::fromNode($node);
new Position($node->getAttribute('startLine') - 1, $node->getAttribute('startColumn') - 1),
new Position($node->getAttribute('endLine') - 1, $node->getAttribute('endColumn') - 1)
);
// Workaround for https://github.com/nikic/PHP-Parser/issues/311 // Workaround for https://github.com/nikic/PHP-Parser/issues/311
$parent = $node->getAttribute('parentNode'); $parent = $node->getAttribute('parentNode');
if (isset($parent) && $parent instanceof Node\Stmt\GroupUse && $parent->prefix === $node) { if (isset($parent) && $parent instanceof Node\Stmt\GroupUse && $parent->prefix === $node) {

View File

@ -113,16 +113,14 @@ class PhpDocument
]; ];
$symbols = []; $symbols = [];
foreach ($this->definitions as $fqn => $node) { foreach ($this->definitions as $fqn => $node) {
$class = get_class($node);
if (!isset($nodeSymbolKindMap[$class])) {
continue;
}
$symbol = new SymbolInformation(); $symbol = new SymbolInformation();
$symbol->kind = $nodeSymbolKindMap[get_class($node)]; $symbol->kind = $nodeSymbolKindMap[$class];
$symbol->name = (string)$node->name; $symbol->name = (string)$node->name;
$symbol->location = new Location( $symbol->location = Location::fromNode($node);
$this->getUri(),
new Range(
new Position($node->getAttribute('startLine') - 1, $node->getAttribute('startColumn') - 1),
new Position($node->getAttribute('endLine') - 1, $node->getAttribute('endColumn'))
)
);
$parts = preg_split('/(::|\\\\)/', $fqn); $parts = preg_split('/(::|\\\\)/', $fqn);
array_pop($parts); array_pop($parts);
$symbol->containerName = implode('\\', $parts); $symbol->containerName = implode('\\', $parts);

View File

@ -2,6 +2,8 @@
namespace LanguageServer\Protocol; namespace LanguageServer\Protocol;
use PhpParser\Node;
/** /**
* Represents a location inside a resource, such as a line inside a text file. * Represents a location inside a resource, such as a line inside a text file.
*/ */
@ -17,6 +19,17 @@ class Location
*/ */
public $range; 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) public function __construct(string $uri = null, Range $range = null)
{ {
$this->uri = $uri; $this->uri = $uri;

View File

@ -2,6 +2,8 @@
namespace LanguageServer\Protocol; namespace LanguageServer\Protocol;
use PhpParser\Node;
/** /**
* A range in a text document expressed as (zero-based) start and end positions. * A range in a text document expressed as (zero-based) start and end positions.
*/ */
@ -21,6 +23,20 @@ class Range
*/ */
public $end; 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) public function __construct(Position $start = null, Position $end = null)
{ {
$this->start = $start; $this->start = $start;

View File

@ -2,6 +2,8 @@
namespace LanguageServer\Protocol; namespace LanguageServer\Protocol;
use PhpParser\Node;
/** /**
* Represents information about programming constructs like variables, classes, * Represents information about programming constructs like variables, classes,
* interfaces etc. * interfaces etc.

View File

@ -109,12 +109,6 @@ class TextDocument
if ($def === null) { if ($def === null) {
return null; return null;
} }
return new Location( return Location::fromNode($def);
$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)
)
);
} }
} }

View File

@ -39,7 +39,7 @@ class DefinitionTest extends TestCase
], ],
'end' => [ 'end' => [
'line' => 21, 'line' => 21,
'character' => 0 'character' => 1
] ]
] ]
], json_decode(json_encode($result), true)); ], json_decode(json_encode($result), true));
@ -59,7 +59,7 @@ class DefinitionTest extends TestCase
], ],
'end' => [ 'end' => [
'line' => 21, 'line' => 21,
'character' => 0 'character' => 1
] ]
] ]
], json_decode(json_encode($result), true)); ], json_decode(json_encode($result), true));
@ -79,7 +79,7 @@ class DefinitionTest extends TestCase
], ],
'end' => [ 'end' => [
'line' => 31, 'line' => 31,
'character' => 0 'character' => 1
] ]
] ]
], json_decode(json_encode($result), true)); ], json_decode(json_encode($result), true));
@ -99,7 +99,7 @@ class DefinitionTest extends TestCase
], ],
'end' => [ 'end' => [
'line' => 31, 'line' => 31,
'character' => 0 'character' => 1
] ]
] ]
], json_decode(json_encode($result), true)); ], json_decode(json_encode($result), true));
@ -119,7 +119,7 @@ class DefinitionTest extends TestCase
], ],
'end' => [ 'end' => [
'line' => 8, 'line' => 8,
'character' => 31 'character' => 32
] ]
] ]
], json_decode(json_encode($result), true)); ], json_decode(json_encode($result), true));
@ -139,7 +139,7 @@ class DefinitionTest extends TestCase
], ],
'end' => [ 'end' => [
'line' => 4, 'line' => 4,
'character' => 21 'character' => 22
] ]
] ]
], json_decode(json_encode($result), true)); ], json_decode(json_encode($result), true));
@ -159,7 +159,7 @@ class DefinitionTest extends TestCase
], ],
'end' => [ 'end' => [
'line' => 15, 'line' => 15,
'character' => 4 'character' => 5
] ]
] ]
], json_decode(json_encode($result), true)); ], json_decode(json_encode($result), true));
@ -179,7 +179,7 @@ class DefinitionTest extends TestCase
], ],
'end' => [ 'end' => [
'line' => 9, 'line' => 9,
'character' => 36 'character' => 37
] ]
] ]
], json_decode(json_encode($result), true)); ], json_decode(json_encode($result), true));
@ -199,7 +199,7 @@ class DefinitionTest extends TestCase
], ],
'end' => [ 'end' => [
'line' => 20, 'line' => 20,
'character' => 4 'character' => 5
] ]
] ]
], json_decode(json_encode($result), true)); ], json_decode(json_encode($result), true));
@ -219,7 +219,7 @@ class DefinitionTest extends TestCase
], ],
'end' => [ 'end' => [
'line' => 10, 'line' => 10,
'character' => 23 'character' => 24
] ]
] ]
], json_decode(json_encode($result), true)); ], json_decode(json_encode($result), true));
@ -239,7 +239,7 @@ class DefinitionTest extends TestCase
], ],
'end' => [ 'end' => [
'line' => 12, 'line' => 12,
'character' => 9 'character' => 10
] ]
] ]
], json_decode(json_encode($result), true)); ], json_decode(json_encode($result), true));
@ -259,7 +259,7 @@ class DefinitionTest extends TestCase
], ],
'end' => [ 'end' => [
'line' => 21, 'line' => 21,
'character' => 0 'character' => 1
] ]
] ]
], json_decode(json_encode($result), true)); ], json_decode(json_encode($result), true));
@ -278,7 +278,7 @@ class DefinitionTest extends TestCase
], ],
'end' => [ 'end' => [
'line' => 21, 'line' => 21,
'character' => 0 'character' => 1
] ]
] ]
], json_decode(json_encode($result), true)); ], json_decode(json_encode($result), true));
@ -298,7 +298,7 @@ class DefinitionTest extends TestCase
], ],
'end' => [ 'end' => [
'line' => 15, 'line' => 15,
'character' => 33 'character' => 34
] ]
] ]
], json_decode(json_encode($result), true)); ], json_decode(json_encode($result), true));
@ -318,7 +318,7 @@ class DefinitionTest extends TestCase
], ],
'end' => [ 'end' => [
'line' => 19, 'line' => 19,
'character' => 25 'character' => 26
] ]
] ]
], json_decode(json_encode($result), true)); ], json_decode(json_encode($result), true));
@ -338,7 +338,7 @@ class DefinitionTest extends TestCase
], ],
'end' => [ 'end' => [
'line' => 36, 'line' => 36,
'character' => 0 'character' => 1
] ]
] ]
], json_decode(json_encode($result), true)); ], json_decode(json_encode($result), true));