1
0
Fork 0

use Ds\Set

pull/746/head
Robert Lu 2019-05-25 13:46:47 +08:00
parent 9dc1656592
commit bc07c19957
2 changed files with 26 additions and 29 deletions

View File

@ -28,6 +28,7 @@
"jetbrains/phpstorm-stubs": "dev-master", "jetbrains/phpstorm-stubs": "dev-master",
"microsoft/tolerant-php-parser": "0.0.*", "microsoft/tolerant-php-parser": "0.0.*",
"netresearch/jsonmapper": "^1.0", "netresearch/jsonmapper": "^1.0",
"php-ds/php-ds": "^1.2",
"phpdocumentor/reflection-docblock": "^4.0.0", "phpdocumentor/reflection-docblock": "^4.0.0",
"psr/log": "^1.0", "psr/log": "^1.0",
"sabre/event": "^5.0", "sabre/event": "^5.0",

View File

@ -1,8 +1,9 @@
<?php <?php
declare(strict_types = 1); declare(strict_types=1);
namespace LanguageServer\Index; namespace LanguageServer\Index;
use Ds\Set;
use LanguageServer\Definition; use LanguageServer\Definition;
use Sabre\Event\EmitterTrait; use Sabre\Event\EmitterTrait;
@ -36,7 +37,7 @@ class Index implements ReadableIndex, \Serializable
* An associative array that maps fully qualified symbol names * An associative array that maps fully qualified symbol names
* to arrays of document URIs that reference the symbol * to arrays of document URIs that reference the symbol
* *
* @var string[][] * @var Set[]
*/ */
private $references = []; private $references = [];
@ -131,9 +132,9 @@ class Index implements ReadableIndex, \Serializable
continue; continue;
} }
if ($item instanceof Definition) { if ($item instanceof Definition) {
yield $fqn.$name => $item; yield $fqn . $name => $item;
} elseif (is_array($item) && isset($item[''])) { } elseif (is_array($item) && isset($item[''])) {
yield $fqn.$name => $item['']; yield $fqn . $name => $item[''];
} }
} }
} }
@ -200,8 +201,10 @@ class Index implements ReadableIndex, \Serializable
*/ */
public function getReferenceUris(string $fqn): \Generator public function getReferenceUris(string $fqn): \Generator
{ {
foreach ($this->references[$fqn] ?? [] as $uri) { if ($this->references[$fqn]) {
yield $uri; foreach ($this->references[$fqn] as $uri) {
yield $uri;
}
} }
} }
@ -209,7 +212,7 @@ class Index implements ReadableIndex, \Serializable
* For test use. * For test use.
* Returns all references, keyed by fqn. * Returns all references, keyed by fqn.
* *
* @return string[][] * @return Set[]
*/ */
public function getReferences(): array public function getReferences(): array
{ {
@ -225,12 +228,9 @@ class Index implements ReadableIndex, \Serializable
public function addReferenceUri(string $fqn, string $uri) public function addReferenceUri(string $fqn, string $uri)
{ {
if (!isset($this->references[$fqn])) { if (!isset($this->references[$fqn])) {
$this->references[$fqn] = []; $this->references[$fqn] = new Set();
}
// TODO: use DS\Set instead of searching array
if (array_search($uri, $this->references[$fqn], true) === false) {
$this->references[$fqn][] = $uri;
} }
$this->references[$fqn]->add($uri);
} }
/** /**
@ -245,11 +245,7 @@ class Index implements ReadableIndex, \Serializable
if (!isset($this->references[$fqn])) { if (!isset($this->references[$fqn])) {
return; return;
} }
$index = array_search($fqn, $this->references[$fqn], true); $this->references[$fqn]->remove($uri);
if ($index === false) {
return;
}
array_splice($this->references[$fqn], $index, 1);
} }
/** /**
@ -299,9 +295,9 @@ class Index implements ReadableIndex, \Serializable
{ {
foreach ($storage as $key => $value) { foreach ($storage as $key => $value) {
if (!is_array($value)) { if (!is_array($value)) {
yield $prefix.$key => $value; yield $prefix . $key => $value;
} else { } else {
yield from $this->yieldDefinitionsRecursively($value, $prefix.$key); yield from $this->yieldDefinitionsRecursively($value, $prefix . $key);
} }
} }
} }
@ -365,8 +361,8 @@ class Index implements ReadableIndex, \Serializable
* It can be an index node or a Definition if the $parts are precise * It can be an index node or a Definition if the $parts are precise
* enough. Returns null when nothing is found. * enough. Returns null when nothing is found.
* *
* @param string[] $path The splitted FQN * @param string[] $path The splitted FQN
* @param array|Definition &$storage The current level to look for $path. * @param array|Definition &$storage The current level to look for $path.
* @return array|Definition|null * @return array|Definition|null
*/ */
private function getIndexValue(array $path, &$storage) private function getIndexValue(array $path, &$storage)
@ -389,10 +385,10 @@ class Index implements ReadableIndex, \Serializable
* Recursive function that stores the given Definition in the given $storage array represented * Recursive function that stores the given Definition in the given $storage array represented
* as a tree matching the given $parts. * as a tree matching the given $parts.
* *
* @param int $level The current level of FQN part * @param int $level The current level of FQN part
* @param string[] $parts The splitted FQN * @param string[] $parts The splitted FQN
* @param array &$storage The array in which to store the $definition * @param array &$storage The array in which to store the $definition
* @param Definition $definition The Definition to store * @param Definition $definition The Definition to store
*/ */
private function indexDefinition(int $level, array $parts, array &$storage, Definition $definition) private function indexDefinition(int $level, array $parts, array &$storage, Definition $definition)
{ {
@ -416,10 +412,10 @@ class Index implements ReadableIndex, \Serializable
* $storage array. The function also looks up recursively to remove the parents of the * $storage array. The function also looks up recursively to remove the parents of the
* definition which no longer has children to avoid to let empty arrays in the index. * definition which no longer has children to avoid to let empty arrays in the index.
* *
* @param int $level The current level of FQN part * @param int $level The current level of FQN part
* @param string[] $parts The splitted FQN * @param string[] $parts The splitted FQN
* @param array &$storage The current array in which to remove data * @param array &$storage The current array in which to remove data
* @param array &$rootStorage The root storage array * @param array &$rootStorage The root storage array
*/ */
private function removeIndexedDefinition(int $level, array $parts, array &$storage, array &$rootStorage) private function removeIndexedDefinition(int $level, array $parts, array &$storage, array &$rootStorage)
{ {