2016-10-08 11:34:49 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2016-10-09 14:03:56 +00:00
|
|
|
namespace LanguageServer\NodeVisitor;
|
2016-10-08 11:34:49 +00:00
|
|
|
|
2017-02-17 21:07:44 +00:00
|
|
|
use PhpParser\{NodeVisitorAbstract, Node, NodeTraverser};
|
2016-10-08 15:14:52 +00:00
|
|
|
use LanguageServer\Protocol\{Position, Range};
|
2016-10-08 11:34:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds the Node at a specified position
|
|
|
|
* Depends on ColumnCalculator
|
|
|
|
*/
|
|
|
|
class NodeAtPositionFinder extends NodeVisitorAbstract
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The node at the position, if found
|
|
|
|
*
|
2017-02-17 17:50:17 +00:00
|
|
|
* @var Node|null
|
2016-10-08 11:34:49 +00:00
|
|
|
*/
|
|
|
|
public $node;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Position
|
|
|
|
*/
|
|
|
|
private $position;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Position $position The position where the node is located
|
|
|
|
*/
|
|
|
|
public function __construct(Position $position)
|
|
|
|
{
|
|
|
|
$this->position = $position;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function leaveNode(Node $node)
|
|
|
|
{
|
2017-02-17 17:50:17 +00:00
|
|
|
if ($this->node === null) {
|
|
|
|
$range = Range::fromNode($node);
|
|
|
|
if ($range->includes($this->position)) {
|
|
|
|
$this->node = $node;
|
2017-02-17 21:07:44 +00:00
|
|
|
return NodeTraverser::STOP_TRAVERSAL
|
2017-02-17 17:50:17 +00:00
|
|
|
}
|
2016-10-08 11:34:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|