Skip to content

Commit

Permalink
differenciate member and non member definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasmure committed Nov 19, 2017
1 parent 97ec127 commit 48bbbb5
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 47 deletions.
14 changes: 6 additions & 8 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) as $fqn => $def) {
foreach ($this->index->getDescendantDefinitionsForFqn($parentFqn, true) 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) {
if (substr($fqn, 0, strlen($prefix)) === $prefix) {
$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) as $fqn => $def) {
foreach ($this->index->getDescendantDefinitionsForFqn($parentFqn, true) 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) {
if (substr(strtolower($fqn), 0, strlen($prefix)) === $prefix) {
$list->items[] = CompletionItem::fromDefinition($def);
}
}
Expand Down Expand Up @@ -321,14 +321,12 @@ 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() as $fqn => $def) {
foreach ($this->index->getDefinitions(false) 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: 6 additions & 4 deletions src/Index/AbstractAggregateIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,27 @@ 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(): \Generator
public function getDefinitions(bool $member = null): \Generator
{
foreach ($this->getIndexes() as $index) {
yield from $index->getDefinitions();
yield from $index->getDefinitions($member);
}
}

/**
* 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): \Generator
public function getDescendantDefinitionsForFqn(string $fqn, bool $member = null): \Generator
{
foreach ($this->getIndexes() as $index) {
yield from $index->getDescendantDefinitionsForFqn($fqn);
yield from $index->getDescendantDefinitionsForFqn($fqn, $member);
}
}

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

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

/**
* An associative array that maps fully qualified symbol names
Expand Down Expand Up @@ -99,20 +115,29 @@ 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(): \Generator
public function getDefinitions(bool $member = null): \Generator
{
yield from $this->yieldDefinitionsRecursively($this->definitions);
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);
}
}

/**
* 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): \Generator
public function getDescendantDefinitionsForFqn(string $fqn, bool $member = null): \Generator
{
$parts = $this->splitFqn($fqn);
if ('' === end($parts)) {
Expand All @@ -121,12 +146,13 @@ public function getDescendantDefinitionsForFqn(string $fqn): \Generator
array_pop($parts);
}

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

if ($result instanceof Definition) {
yield $fqn => $result;
} elseif (is_array($result)) {
yield from $this->yieldDefinitionsRecursively($result, $fqn);
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);
}
}

Expand All @@ -140,8 +166,13 @@ public function getDescendantDefinitionsForFqn(string $fqn): \Generator
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 @@ -164,7 +195,12 @@ public function getDefinition(string $fqn, bool $globalFallback = false)
public function setDefinition(string $fqn, Definition $definition)
{
$parts = $this->splitFqn($fqn);
$this->indexDefinition(0, $parts, $this->definitions, $definition);

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

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

unset($this->references[$fqn]);
}
Expand Down Expand Up @@ -279,6 +316,26 @@ 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 @@ -431,7 +488,7 @@ private function removeIndexedDefinition(int $level, array $parts, array &$stora
$this->removeIndexedDefinition(0, array_slice($parts, 0, $level), $rootStorage, $rootStorage);
}
}
} else {
} elseif (isset($storage[$part])) {
$this->removeIndexedDefinition($level + 1, $parts, $storage[$part], $rootStorage);
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/Index/ReadableIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,19 @@ 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(): \Generator;
public function getDefinitions(bool $member = null): \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): \Generator;
public function getDescendantDefinitionsForFqn(string $fqn, bool $member = null): \Generator;

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

0 comments on commit 48bbbb5

Please sign in to comment.