Skip to content

Commit

Permalink
perf: get direct children
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfbecker committed Nov 23, 2017
1 parent 91ca99a commit fa67f84
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
18 changes: 10 additions & 8 deletions src/CompletionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,12 @@ public function provideCompletion(PhpDocument $doc, Position $pos, CompletionCon

// The FQNs of the symbol and its parents (eg the implemented interfaces)
foreach ($this->expandParentFqns($fqns) as $parentFqn) {
// Add the object access operator to only get members of all parents
$prefix = $parentFqn . '->';
$prefixLen = strlen($prefix);
// Collect fqn definitions
foreach ($this->index->getDescendantDefinitionsForFqn($parentFqn) as $fqn => $def) {
// Add the object access operator to only get members of all parents
$prefix = $parentFqn . '->';
if (substr($fqn, 0, strlen($prefix)) === $prefix && $def->isMember) {
foreach ($this->index->getChildDefinitionsForFqn($parentFqn) as $fqn => $def) {
if (substr($fqn, 0, $prefixLen) === $prefix && $def->isMember) {

This comment has been minimized.

Copy link
@felixfbecker

felixfbecker Nov 23, 2017

Author Owner

I think I can get rid of the $def->isMember check given the prefix contains ->

$list->items[] = CompletionItem::fromDefinition($def);
}
}
Expand All @@ -250,11 +251,12 @@ public function provideCompletion(PhpDocument $doc, Position $pos, CompletionCon

// The FQNs of the symbol and its parents (eg the implemented interfaces)
foreach ($this->expandParentFqns($fqns) as $parentFqn) {
// Append :: operator to only get static members of all parents
$prefix = strtolower($parentFqn . '::');
$prefixLen = strlen($prefix);
// Collect fqn definitions
foreach ($this->index->getDescendantDefinitionsForFqn($parentFqn) as $fqn => $def) {
// Append :: operator to only get static members of all parents
$prefix = strtolower($parentFqn . '::');
if (substr(strtolower($fqn), 0, strlen($prefix)) === $prefix && $def->isMember) {
foreach ($this->index->getChildDefinitionsForFqn($parentFqn) as $fqn => $def) {
if (substr(strtolower($fqn), 0, $prefixLen) === $prefix && $def->isMember) {
$list->items[] = CompletionItem::fromDefinition($def);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Index/AbstractAggregateIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ public function getDefinitions(): \Generator
}

/**
* Returns a Generator that yields all the descendant Definitions of a given FQN
* Returns a Generator that yields all the direct child Definitions of a given FQN
*
* @param string $fqn
* @return \Generator yields Definition
*/
public function getDescendantDefinitionsForFqn(string $fqn): \Generator
public function getChildDefinitionsForFqn(string $fqn): \Generator
{
foreach ($this->getIndexes() as $index) {
yield from $index->getDescendantDefinitionsForFqn($fqn);
yield from $index->getChildDefinitionsForFqn($fqn);
}
}

Expand Down
20 changes: 12 additions & 8 deletions src/Index/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ public function getDefinitions(): \Generator
}

/**
* Returns a Generator that yields all the descendant Definitions of a given FQN
* Returns a Generator that yields all the direct child Definitions of a given FQN
*
* @param string $fqn
* @return \Generator yields Definition
*/
public function getDescendantDefinitionsForFqn(string $fqn): \Generator
public function getChildDefinitionsForFqn(string $fqn): \Generator
{
$parts = $this->splitFqn($fqn);
if ('' === end($parts)) {
Expand All @@ -122,11 +122,15 @@ public function getDescendantDefinitionsForFqn(string $fqn): \Generator
}

$result = $this->getIndexValue($parts, $this->definitions);

if ($result instanceof Definition) {
yield $fqn => $result;
} elseif (is_array($result)) {
yield from $this->yieldDefinitionsRecursively($result, $fqn);
if (!$result) {
return;
}
foreach ($result as $name => $item) {
// Don't yield the parent
if ($name === '') {
continue;
}
yield $fqn.$name => $item;
}
}

Expand Down Expand Up @@ -374,7 +378,7 @@ private function getIndexValue(array $parts, array &$storage)

$parts = array_slice($parts, 1);
// we've reached the last provided part
if (0 === count($parts)) {
if (empty($parts)) {
return $storage[$part];
}

Expand Down
4 changes: 2 additions & 2 deletions src/Index/ReadableIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public function isStaticComplete(): bool;
public function getDefinitions(): \Generator;

/**
* Returns a Generator that yields all the descendant Definitions of a given FQN
* Returns a Generator that yields all the direct child Definitions of a given FQN
*
* @param string $fqn
* @return \Generator yields Definition
*/
public function getDescendantDefinitionsForFqn(string $fqn): \Generator;
public function getChildDefinitionsForFqn(string $fqn): \Generator;

/**
* Returns the Definition object by a specific FQN
Expand Down

0 comments on commit fa67f84

Please sign in to comment.