Small code reorganization
parent
d6200fc07a
commit
6a6a93c679
|
@ -29,6 +29,11 @@ class CompletionContext
|
||||||
*/
|
*/
|
||||||
private $lines;
|
private $lines;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \LanguageServer\Protocol\Range
|
||||||
|
*/
|
||||||
|
private $replacementRange;
|
||||||
|
|
||||||
public function __construct(PhpDocument $phpDocument)
|
public function __construct(PhpDocument $phpDocument)
|
||||||
{
|
{
|
||||||
$this->phpDocument = $phpDocument;
|
$this->phpDocument = $phpDocument;
|
||||||
|
@ -36,6 +41,11 @@ class CompletionContext
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getReplacementRange(): Range
|
public function getReplacementRange(): Range
|
||||||
|
{
|
||||||
|
return $this->replacementRange;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function calculateReplacementRange(): Range
|
||||||
{
|
{
|
||||||
$line = $this->getLine($this->position->line);
|
$line = $this->getLine($this->position->line);
|
||||||
if (!empty($line)) {
|
if (!empty($line)) {
|
||||||
|
@ -64,6 +74,24 @@ class CompletionContext
|
||||||
public function setPosition(Position $position)
|
public function setPosition(Position $position)
|
||||||
{
|
{
|
||||||
$this->position = $position;
|
$this->position = $position;
|
||||||
|
$this->replacementRange = $this->calculateReplacementRange();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isObjectContext()
|
||||||
|
{
|
||||||
|
$line = $this->getLine($this->getPosition()->line);
|
||||||
|
if (empty($line)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$range = $this->getReplacementRange();
|
||||||
|
if (preg_match_all('@(\$this->|self::)@', $line, $matches, PREG_OFFSET_CAPTURE)) {
|
||||||
|
foreach ($matches[0] as $match) {
|
||||||
|
if (($match[1] + strlen($match[0])) === $range->start->character) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getLine(int $line)
|
public function getLine(int $line)
|
||||||
|
|
|
@ -18,7 +18,7 @@ class ClassMembersStrategy implements ICompletionStrategy
|
||||||
*/
|
*/
|
||||||
public function apply(CompletionContext $context, CompletionReporter $reporter)
|
public function apply(CompletionContext $context, CompletionReporter $reporter)
|
||||||
{
|
{
|
||||||
if (!$this->isValidContext($context)) {
|
if (!$context->isObjectContext()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$range = $context->getReplacementRange();
|
$range = $context->getReplacementRange();
|
||||||
|
@ -28,7 +28,7 @@ class ClassMembersStrategy implements ICompletionStrategy
|
||||||
$nodeRange = Range::fromNode($node);
|
$nodeRange = Range::fromNode($node);
|
||||||
if ($nodeRange->includes($context->getPosition())) {
|
if ($nodeRange->includes($context->getPosition())) {
|
||||||
foreach ($nodes as $childFqn => $child) {
|
foreach ($nodes as $childFqn => $child) {
|
||||||
if (stripos($childFqn, $fqn) == 0 && $childFqn !== $fqn) {
|
if (stripos($childFqn, $fqn) === 0 && $childFqn !== $fqn) {
|
||||||
$reporter->reportByNode($child, $range, $childFqn);
|
$reporter->reportByNode($child, $range, $childFqn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,20 +38,4 @@ class ClassMembersStrategy implements ICompletionStrategy
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function isValidContext(CompletionContext $context)
|
|
||||||
{
|
|
||||||
$line = $context->getLine($context->getPosition()->line);
|
|
||||||
if (empty($line)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
$range = $context->getReplacementRange($context);
|
|
||||||
if (preg_match_all('@(\$this->|self::)@', $line, $matches, PREG_OFFSET_CAPTURE)) {
|
|
||||||
foreach ($matches[0] as $match) {
|
|
||||||
if (($match[1] + strlen($match[0])) === $range->start->character) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,9 @@ class GlobalElementsStrategy implements ICompletionStrategy
|
||||||
*/
|
*/
|
||||||
public function apply(CompletionContext $context, CompletionReporter $reporter)
|
public function apply(CompletionContext $context, CompletionReporter $reporter)
|
||||||
{
|
{
|
||||||
|
if ($context->isObjectContext()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
$range = $context->getReplacementRange($context);
|
$range = $context->getReplacementRange($context);
|
||||||
$project = $context->getPhpDocument()->project;
|
$project = $context->getPhpDocument()->project;
|
||||||
foreach ($project->getSymbols() as $fqn => $symbol) {
|
foreach ($project->getSymbols() as $fqn => $symbol) {
|
||||||
|
|
|
@ -98,6 +98,9 @@ class KeywordsStrategy implements ICompletionStrategy
|
||||||
*/
|
*/
|
||||||
public function apply(CompletionContext $context, CompletionReporter $reporter)
|
public function apply(CompletionContext $context, CompletionReporter $reporter)
|
||||||
{
|
{
|
||||||
|
if ($context->isObjectContext()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
$range = $context->getReplacementRange();
|
$range = $context->getReplacementRange();
|
||||||
foreach (self::KEYWORDS as $keyword) {
|
foreach (self::KEYWORDS as $keyword) {
|
||||||
$reporter->report($keyword, CompletionItemKind::KEYWORD, $keyword, $range);
|
$reporter->report($keyword, CompletionItemKind::KEYWORD, $keyword, $range);
|
||||||
|
|
|
@ -59,7 +59,7 @@ class VariablesStrategy implements ICompletionStrategy
|
||||||
|
|
||||||
private function isValid(SymbolInformation $symbol)
|
private function isValid(SymbolInformation $symbol)
|
||||||
{
|
{
|
||||||
return $symbol->kind == SymbolKind::FUNCTION || $symbol->kind == SymbolKind::METHOD;
|
return $symbol->kind === SymbolKind::FUNCTION || $symbol->kind === SymbolKind::METHOD;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue