Skip to content

Commit

Permalink
Revert "differenciate member and non member definitions"
Browse files Browse the repository at this point in the history
This reverts commit 48bbbb5.
  • Loading branch information
felixfbecker committed Nov 23, 2017
1 parent 67dd980 commit 91ca99a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 105 deletions.
14 changes: 8 additions & 6 deletions src/CompletionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ 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) {
// Collect fqn definitions
foreach ($this->index->getDescendantDefinitionsForFqn($parentFqn, true) as $fqn => $def) {
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) {
if (substr($fqn, 0, strlen($prefix)) === $prefix && $def->isMember) {
$list->items[] = CompletionItem::fromDefinition($def);
}
}
Expand All @@ -251,10 +251,10 @@ 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) {
// Collect fqn definitions
foreach ($this->index->getDescendantDefinitionsForFqn($parentFqn, true) as $fqn => $def) {
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) {
if (substr(strtolower($fqn), 0, strlen($prefix)) === $prefix && $def->isMember) {
$list->items[] = CompletionItem::fromDefinition($def);
}
}
Expand Down Expand Up @@ -321,12 +321,14 @@ public function provideCompletion(PhpDocument $doc, Position $pos, CompletionCon
// Suggest global (ie non member) symbols that either
// - start with the current namespace + prefix, if the Name node is not fully qualified
// - start with just the prefix, if the Name node is fully qualified
foreach ($this->index->getDefinitions(false) as $fqn => $def) {
foreach ($this->index->getDefinitions() as $fqn => $def) {

$fqnStartsWithPrefix = substr($fqn, 0, $prefixLen) === $prefix;

if (
(
// Exclude methods, properties etc.
!$def->isMember
&& (
!$prefix
|| (
// Either not qualified, but a matching prefix with global fallback
Expand Down
10 changes: 4 additions & 6 deletions src/Index/AbstractAggregateIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,27 +102,25 @@ public function isStaticComplete(): bool
* Returns a Generator providing an associative array [string => Definition]
* that maps fully qualified symbol names to Definitions (global or not)
*
* @param boolean|null $member Indicates if we want member or non-member definitions (null for both, default null)
* @return \Generator yields Definition
*/
public function getDefinitions(bool $member = null): \Generator
public function getDefinitions(): \Generator
{
foreach ($this->getIndexes() as $index) {
yield from $index->getDefinitions($member);
yield from $index->getDefinitions();
}
}

/**
* Returns a Generator that yields all the descendant Definitions of a given FQN
*
* @param string $fqn
* @param boolean|null $member Indicates if we want member or non-member definitions (null for both, default null)
* @return \Generator yields Definition
*/
public function getDescendantDefinitionsForFqn(string $fqn, bool $member = null): \Generator
public function getDescendantDefinitionsForFqn(string $fqn): \Generator
{
foreach ($this->getIndexes() as $index) {
yield from $index->getDescendantDefinitionsForFqn($fqn, $member);
yield from $index->getDescendantDefinitionsForFqn($fqn);
}
}

Expand Down
91 changes: 17 additions & 74 deletions src/Index/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,21 @@ class Index implements ReadableIndex, \Serializable

/**
* An associative array that maps splitted fully qualified symbol names
* to non-member definitions, eg :
* to definitions, eg :
* [
* 'Psr' => [
* '\Log' => [
* '\LoggerInterface' => [
* '' => $definition,
* '' => $def1, // definition for 'Psr\Log\LoggerInterface' which is non-member
* '->log()' => $def2, // definition for 'Psr\Log\LoggerInterface->log()' which is a member definition
* ],
* ],
* ],
* ]
*
* @var array
*/
private $nonMemberDefinitions = [];

/**
* An associative array that maps splitted fully qualified symbol names
* to member definitions, eg :
* [
* 'Psr' => [
* '\Log' => [
* '\LoggerInterface' => [
* '->log()' => $definition,
* ],
* ],
* ],
* ]
*
* @var array
*/
private $memberDefinitions = [];
private $definitions = [];

/**
* An associative array that maps fully qualified symbol names
Expand Down Expand Up @@ -115,29 +99,20 @@ public function isStaticComplete(): bool
* Returns a Generator providing an associative array [string => Definition]
* that maps fully qualified symbol names to Definitions (global or not)
*
* @param boolean|null $member Indicates if we want member or non-member definitions (null for both, default null)
* @return \Generator yields Definition
*/
public function getDefinitions(bool $member = null): \Generator
public function getDefinitions(): \Generator
{
if (true === $member) {
yield from $this->yieldDefinitionsRecursively($this->memberDefinitions);
} elseif (false === $member) {
yield from $this->yieldDefinitionsRecursively($this->nonMemberDefinitions);
} else {
yield from $this->yieldDefinitionsRecursively($this->memberDefinitions);
yield from $this->yieldDefinitionsRecursively($this->nonMemberDefinitions);
}
yield from $this->yieldDefinitionsRecursively($this->definitions);
}

/**
* Returns a Generator that yields all the descendant Definitions of a given FQN
*
* @param string $fqn
* @param boolean|null $member Indicates if we want member or non-member definitions (null for both, default null)
* @return \Generator yields Definition
*/
public function getDescendantDefinitionsForFqn(string $fqn, bool $member = null): \Generator
public function getDescendantDefinitionsForFqn(string $fqn): \Generator
{
$parts = $this->splitFqn($fqn);
if ('' === end($parts)) {
Expand All @@ -146,13 +121,12 @@ public function getDescendantDefinitionsForFqn(string $fqn, bool $member = null)
array_pop($parts);
}

if (true === $member) {
yield from $this->doGetDescendantDefinitionsForFqn($fqn, $parts, $this->memberDefinitions);
} elseif (false === $member) {
yield from $this->doGetDescendantDefinitionsForFqn($fqn, $parts, $this->nonMemberDefinitions);
} else {
yield from $this->doGetDescendantDefinitionsForFqn($fqn, $parts, $this->memberDefinitions);
yield from $this->doGetDescendantDefinitionsForFqn($fqn, $parts, $this->nonMemberDefinitions);
$result = $this->getIndexValue($parts, $this->definitions);

if ($result instanceof Definition) {
yield $fqn => $result;
} elseif (is_array($result)) {
yield from $this->yieldDefinitionsRecursively($result, $fqn);
}
}

Expand All @@ -166,13 +140,8 @@ public function getDescendantDefinitionsForFqn(string $fqn, bool $member = null)
public function getDefinition(string $fqn, bool $globalFallback = false)
{
$parts = $this->splitFqn($fqn);
$result = $this->getIndexValue($parts, $this->definitions);

$result = $this->getIndexValue($parts, $this->memberDefinitions);
if ($result instanceof Definition) {
return $result;
}

$result = $this->getIndexValue($parts, $this->nonMemberDefinitions);
if ($result instanceof Definition) {
return $result;
}
Expand All @@ -195,12 +164,7 @@ public function getDefinition(string $fqn, bool $globalFallback = false)
public function setDefinition(string $fqn, Definition $definition)
{
$parts = $this->splitFqn($fqn);

if ($definition->isMember) {
$this->indexDefinition(0, $parts, $this->memberDefinitions, $definition);
} else {
$this->indexDefinition(0, $parts, $this->nonMemberDefinitions, $definition);
}
$this->indexDefinition(0, $parts, $this->definitions, $definition);

$this->emit('definition-added');
}
Expand All @@ -215,8 +179,7 @@ public function setDefinition(string $fqn, Definition $definition)
public function removeDefinition(string $fqn)
{
$parts = $this->splitFqn($fqn);
$this->removeIndexedDefinition(0, $parts, $this->memberDefinitions, $this->memberDefinitions);
$this->removeIndexedDefinition(0, $parts, $this->nonMemberDefinitions, $this->nonMemberDefinitions);
$this->removeIndexedDefinition(0, $parts, $this->definitions, $this->definitions);

unset($this->references[$fqn]);
}
Expand Down Expand Up @@ -316,26 +279,6 @@ public function serialize()
]);
}

/**
* Returns a Generator that yields all the descendant Definitions of a given FQN
* in the given definition index.
*
* @param string $fqn
* @param string[] $parts The splitted FQN
* @param array &$storage The definitions index to look into
* @return \Generator yields Definition
*/
private function doGetDescendantDefinitionsForFqn(string $fqn, array $parts, array &$storage): \Generator
{
$result = $this->getIndexValue($parts, $storage);

if ($result instanceof Definition) {
yield $fqn => $result;
} elseif (is_array($result)) {
yield from $this->yieldDefinitionsRecursively($result, $fqn);
}
}

/**
* Returns a Generator that yields all the Definitions in the given $storage recursively.
* The generator yields key => value pairs, e.g.
Expand Down Expand Up @@ -488,7 +431,7 @@ private function removeIndexedDefinition(int $level, array $parts, array &$stora
$this->removeIndexedDefinition(0, array_slice($parts, 0, $level), $rootStorage, $rootStorage);
}
}
} elseif (isset($storage[$part])) {
} else {
$this->removeIndexedDefinition($level + 1, $parts, $storage[$part], $rootStorage);
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/Index/ReadableIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,17 @@ public function isStaticComplete(): bool;
* Returns a Generator providing an associative array [string => Definition]
* that maps fully qualified symbol names to Definitions (global or not)
*
* @param boolean|null $member Indicates if we want member or non-member definitions (null for both, default null)
* @return \Generator yields Definition
*/
public function getDefinitions(bool $member = null): \Generator;
public function getDefinitions(): \Generator;

/**
* Returns a Generator that yields all the descendant Definitions of a given FQN
*
* @param string $fqn
* @param boolean|null $member Indicates if we want member or non-member definitions (null for both, default null)
* @return \Generator yields Definition
*/
public function getDescendantDefinitionsForFqn(string $fqn, bool $member = null): \Generator;
public function getDescendantDefinitionsForFqn(string $fqn): \Generator;

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

0 comments on commit 91ca99a

Please sign in to comment.