Add Position::compare() and Range::includes()
parent
fbdf1aa414
commit
827ab4c842
|
@ -4,7 +4,7 @@ declare(strict_types = 1);
|
||||||
namespace LanguageServer\NodeVisitors;
|
namespace LanguageServer\NodeVisitors;
|
||||||
|
|
||||||
use PhpParser\{NodeVisitorAbstract, Node};
|
use PhpParser\{NodeVisitorAbstract, Node};
|
||||||
use LanguageServer\Protocol\Position;
|
use LanguageServer\Protocol\{Position, Range};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds the Node at a specified position
|
* Finds the Node at a specified position
|
||||||
|
@ -34,13 +34,11 @@ class NodeAtPositionFinder extends NodeVisitorAbstract
|
||||||
|
|
||||||
public function leaveNode(Node $node)
|
public function leaveNode(Node $node)
|
||||||
{
|
{
|
||||||
if (
|
$range = new Range(
|
||||||
!isset($this->node)
|
new Position($node->getAttribute('startLine') - 1, $node->getAttribute('startColumn') - 1),
|
||||||
&& $node->getAttribute('startLine') <= $this->position->line + 1
|
new Position($node->getAttribute('endLine') - 1, $node->getAttribute('endColumn') - 1)
|
||||||
&& $node->getAttribute('endLine') >= $this->position->line + 1
|
);
|
||||||
&& $node->getAttribute('startColumn') <= $this->position->character + 1
|
if (!isset($this->node) && $range->includes($this->position)) {
|
||||||
&& $node->getAttribute('endColumn') >= $this->position->character + 1
|
|
||||||
) {
|
|
||||||
$this->node = $node;
|
$this->node = $node;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,4 +26,27 @@ class Position
|
||||||
$this->line = $line;
|
$this->line = $line;
|
||||||
$this->character = $character;
|
$this->character = $character;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compares this position to another position
|
||||||
|
* Returns
|
||||||
|
* - 0 if the positions match
|
||||||
|
* - a negative number if $this is before $position
|
||||||
|
* - a positive number otherwise
|
||||||
|
*
|
||||||
|
* @param Position $position
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function compare(Position $position): int
|
||||||
|
{
|
||||||
|
if ($this->line === $position->line && $this->character === $position->character) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->line !== $position->line) {
|
||||||
|
return $this->line - $position->line;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->character - $position->character;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,4 +26,15 @@ class Range
|
||||||
$this->start = $start;
|
$this->start = $start;
|
||||||
$this->end = $end;
|
$this->end = $end;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a position is within the range
|
||||||
|
*
|
||||||
|
* @param Position $position
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function includes(Position $position): bool
|
||||||
|
{
|
||||||
|
return $this->start->compare($position) <= 0 && $this->end->compare($position) >= 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue