From 6a6a93c679d66cdaca7ceec5de52618d63efcbae Mon Sep 17 00:00:00 2001 From: Michal Niewrzal Date: Fri, 21 Oct 2016 16:26:21 +0200 Subject: [PATCH] Small code reorganization --- src/Completion/CompletionContext.php | 28 +++++++++++++++++++ .../Strategies/ClassMembersStrategy.php | 20 ++----------- .../Strategies/GlobalElementsStrategy.php | 3 ++ .../Strategies/KeywordsStrategy.php | 3 ++ .../Strategies/VariablesStrategy.php | 2 +- 5 files changed, 37 insertions(+), 19 deletions(-) diff --git a/src/Completion/CompletionContext.php b/src/Completion/CompletionContext.php index 22220c1..cf0f994 100644 --- a/src/Completion/CompletionContext.php +++ b/src/Completion/CompletionContext.php @@ -29,6 +29,11 @@ class CompletionContext */ private $lines; + /** + * @var \LanguageServer\Protocol\Range + */ + private $replacementRange; + public function __construct(PhpDocument $phpDocument) { $this->phpDocument = $phpDocument; @@ -36,6 +41,11 @@ class CompletionContext } public function getReplacementRange(): Range + { + return $this->replacementRange; + } + + private function calculateReplacementRange(): Range { $line = $this->getLine($this->position->line); if (!empty($line)) { @@ -64,6 +74,24 @@ class CompletionContext public function setPosition(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) diff --git a/src/Completion/Strategies/ClassMembersStrategy.php b/src/Completion/Strategies/ClassMembersStrategy.php index 769ebd9..dd9932f 100644 --- a/src/Completion/Strategies/ClassMembersStrategy.php +++ b/src/Completion/Strategies/ClassMembersStrategy.php @@ -18,7 +18,7 @@ class ClassMembersStrategy implements ICompletionStrategy */ public function apply(CompletionContext $context, CompletionReporter $reporter) { - if (!$this->isValidContext($context)) { + if (!$context->isObjectContext()) { return; } $range = $context->getReplacementRange(); @@ -28,7 +28,7 @@ class ClassMembersStrategy implements ICompletionStrategy $nodeRange = Range::fromNode($node); if ($nodeRange->includes($context->getPosition())) { foreach ($nodes as $childFqn => $child) { - if (stripos($childFqn, $fqn) == 0 && $childFqn !== $fqn) { + if (stripos($childFqn, $fqn) === 0 && $childFqn !== $fqn) { $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; - } } diff --git a/src/Completion/Strategies/GlobalElementsStrategy.php b/src/Completion/Strategies/GlobalElementsStrategy.php index 0266731..bfa2ce1 100644 --- a/src/Completion/Strategies/GlobalElementsStrategy.php +++ b/src/Completion/Strategies/GlobalElementsStrategy.php @@ -20,6 +20,9 @@ class GlobalElementsStrategy implements ICompletionStrategy */ public function apply(CompletionContext $context, CompletionReporter $reporter) { + if ($context->isObjectContext()) { + return; + } $range = $context->getReplacementRange($context); $project = $context->getPhpDocument()->project; foreach ($project->getSymbols() as $fqn => $symbol) { diff --git a/src/Completion/Strategies/KeywordsStrategy.php b/src/Completion/Strategies/KeywordsStrategy.php index d259720..539f1b9 100644 --- a/src/Completion/Strategies/KeywordsStrategy.php +++ b/src/Completion/Strategies/KeywordsStrategy.php @@ -98,6 +98,9 @@ class KeywordsStrategy implements ICompletionStrategy */ public function apply(CompletionContext $context, CompletionReporter $reporter) { + if ($context->isObjectContext()) { + return; + } $range = $context->getReplacementRange(); foreach (self::KEYWORDS as $keyword) { $reporter->report($keyword, CompletionItemKind::KEYWORD, $keyword, $range); diff --git a/src/Completion/Strategies/VariablesStrategy.php b/src/Completion/Strategies/VariablesStrategy.php index d84323c..56d5922 100644 --- a/src/Completion/Strategies/VariablesStrategy.php +++ b/src/Completion/Strategies/VariablesStrategy.php @@ -59,7 +59,7 @@ class VariablesStrategy implements ICompletionStrategy private function isValid(SymbolInformation $symbol) { - return $symbol->kind == SymbolKind::FUNCTION || $symbol->kind == SymbolKind::METHOD; + return $symbol->kind === SymbolKind::FUNCTION || $symbol->kind === SymbolKind::METHOD; } }