1
0
Fork 0

[PR] Intellisense autocomplete also suggests private or protected method and properties

Checking the visibility and the context from where the node is: inside a method declaration or after an instantiation
pull/681/head
Gabriel Noé González 2018-11-09 21:35:05 +01:00
parent 18c6ccd137
commit 5835bd314b
2 changed files with 33 additions and 2 deletions

View File

@ -246,8 +246,10 @@ class CompletionProvider
// Collect all definitions that match any of the prefixes
foreach ($this->index->getDefinitions() as $fqn => $def) {
foreach ($prefixes as $prefix) {
if (substr($fqn, 0, strlen($prefix)) === $prefix && $def->isMember) {
$list->items[] = CompletionItemFactory::fromDefinition($def);
if (substr($fqn, 0, strlen($prefix)) === $prefix &&
$def->isMember &&
$def->isVisible($prefix, $prefixes[0], $node)) {
$list->items[] = CompletionItem::fromDefinition($def);
}
}
}

View File

@ -133,4 +133,33 @@ class Definition
}
}
}
/**
* Checks the definition's visibility.
* @return bool
*/
public function isVisible(string $match, string $caller, \Microsoft\PhpParser\Node $node): bool
{
$ancestor = $node->getFirstAncestor(\Microsoft\PhpParser\Node\MethodDeclaration::class);
if ($ancestor) {
if ($match !== $caller && $this->isPrivate()) {
return false;
}
} else if ($this->isProtected() || $this->isPrivate()) {
return false;
}
return true;
}
private function isPrivate(): bool
{
return 'private' === substr($this->declarationLine, 0, 7);
}
private function isProtected(): bool
{
return 'protected' === substr($this->declarationLine, 0, 9);
}
}