Skip to content

Commit 8801edb

Browse files
committed
also yield URIs to save memory
1 parent 8768b69 commit 8801edb

File tree

4 files changed

+35
-18
lines changed

4 files changed

+35
-18
lines changed

src/Index/AbstractAggregateIndex.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,19 +160,17 @@ public function getDefinition(string $fqn, bool $globalFallback = false)
160160
}
161161

162162
/**
163-
* Returns all URIs in this index that reference a symbol
163+
* Returns a Generator providing all URIs in this index that reference a symbol
164164
*
165165
* @param string $fqn The fully qualified name of the symbol
166-
* @return string[]
166+
* @return \Generator providing string[]
167167
*/
168-
public function getReferenceUris(string $fqn): array
168+
public function getReferenceUris(string $fqn): \Generator
169169
{
170-
$refs = [];
171170
foreach ($this->getIndexes() as $index) {
172-
foreach ($index->getReferenceUris($fqn) as $ref) {
173-
$refs[] = $ref;
171+
foreach ($index->getReferenceUris($fqn) as $uri) {
172+
yield $uri;
174173
}
175174
}
176-
return $refs;
177175
}
178176
}

src/Index/Index.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,21 @@ public function removeDefinition(string $fqn)
197197
}
198198

199199
/**
200-
* Returns all URIs in this index that reference a symbol
200+
* Returns a Generator providing all URIs in this index that reference a symbol
201201
*
202202
* @param string $fqn The fully qualified name of the symbol
203-
* @return string[]
203+
* @return \Generator providing string[]
204204
*/
205-
public function getReferenceUris(string $fqn): array
205+
public function getReferenceUris(string $fqn): \Generator
206206
{
207-
return $this->references[$fqn] ?? [];
207+
$uris = isset($this->references[$fqn])
208+
? $this->references[$fqn]
209+
: []
210+
;
211+
212+
foreach ($uris as $uri) {
213+
yield $uri;
214+
}
208215
}
209216

210217
/**

src/Index/ReadableIndex.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ public function getDefinitionsForNamespace(string $namespace): \Generator;
6363
public function getDefinition(string $fqn, bool $globalFallback = false);
6464

6565
/**
66-
* Returns all URIs in this index that reference a symbol
66+
* Returns a Generator providing all URIs in this index that reference a symbol
6767
*
6868
* @param string $fqn The fully qualified name of the symbol
69-
* @return string[]
69+
* @return \Generator providing string[]
7070
*/
71-
public function getReferenceUris(string $fqn): array;
71+
public function getReferenceUris(string $fqn): \Generator;
7272
}

src/Server/TextDocument.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,9 @@ public function references(
219219
return [];
220220
}
221221
}
222-
$refDocuments = yield Promise\all(array_map(
223-
[$this->documentLoader, 'getOrLoad'],
224-
$this->index->getReferenceUris($fqn)
225-
));
222+
$refDocuments = yield Promise\all(iterator_to_array(
223+
$this->getOrLoadReferences($fqn))
224+
);
226225
foreach ($refDocuments as $document) {
227226
$refs = $document->getReferenceNodesByFqn($fqn);
228227
if ($refs !== null) {
@@ -397,4 +396,17 @@ public function xdefinition(TextDocumentIdentifier $textDocument, Position $posi
397396
return [new SymbolLocationInformation($descriptor, $def->symbolInformation->location)];
398397
});
399398
}
399+
400+
/**
401+
* Gets or loads the documents referencing the given FQN.
402+
*
403+
* @param string $fqn
404+
* @return \Generator providing Promise
405+
*/
406+
private function getOrLoadReferences(string $fqn): \Generator
407+
{
408+
foreach ($this->index->getReferenceUris($fqn) as $ref) {
409+
yield $this->documentLoader->getOrLoad($ref);
410+
}
411+
}
400412
}

0 commit comments

Comments
 (0)