1
0
Fork 0

Small code reorganization

pull/38/head
Michal Niewrzal 2016-10-21 16:26:21 +02:00
parent d6200fc07a
commit 6a6a93c679
5 changed files with 37 additions and 19 deletions

View File

@ -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)

View File

@ -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;
}
} }

View File

@ -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) {

View File

@ -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);

View File

@ -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;
} }
} }