Skip to content

Commit 17602aa

Browse files
committed
globalDefinitions cache to speedup autocomplete
1 parent 18f2f4c commit 17602aa

File tree

4 files changed

+73
-23
lines changed

4 files changed

+73
-23
lines changed

src/CompletionProvider.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -314,26 +314,22 @@ public function provideCompletion(PhpDocument $doc, Position $pos): CompletionLi
314314
// Suggest global symbols that either
315315
// - start with the current namespace + prefix, if the Name node is not fully qualified
316316
// - start with just the prefix, if the Name node is fully qualified
317-
foreach ($this->index->getDefinitions() as $fqn => $def) {
317+
foreach ($this->index->getGlobalDefinitions() as $fqn => $def) {
318318

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

321321
if (
322-
// Exclude methods, properties etc.
323-
!$def->isMember
324-
&& (
325-
!$prefix
322+
!$prefix
323+
|| (
324+
// Either not qualified, but a matching prefix with global fallback
325+
($def->roamed && !$isQualified && $fqnStartsWithPrefix)
326+
// Or not in a namespace or a fully qualified name or AND matching the prefix
327+
|| ((!$namespaceNode || $isFullyQualified) && $fqnStartsWithPrefix)
328+
// Or in a namespace, not fully qualified and matching the prefix + current namespace
326329
|| (
327-
// Either not qualified, but a matching prefix with global fallback
328-
($def->roamed && !$isQualified && $fqnStartsWithPrefix)
329-
// Or not in a namespace or a fully qualified name or AND matching the prefix
330-
|| ((!$namespaceNode || $isFullyQualified) && $fqnStartsWithPrefix)
331-
// Or in a namespace, not fully qualified and matching the prefix + current namespace
332-
|| (
333-
$namespaceNode
334-
&& !$isFullyQualified
335-
&& substr($fqn, 0, $namespacedPrefixLen) === $namespacedPrefix
336-
)
330+
$namespaceNode
331+
&& !$isFullyQualified
332+
&& substr($fqn, 0, $namespacedPrefixLen) === $namespacedPrefix
337333
)
338334
)
339335
// Only suggest classes for `new`

src/Index/AbstractAggregateIndex.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function isStaticComplete(): bool
100100

101101
/**
102102
* Returns an associative array [string => Definition] that maps fully qualified symbol names
103-
* to Definitions
103+
* to Definitions (global or not)
104104
*
105105
* @return Definition[]
106106
*/
@@ -115,6 +115,23 @@ public function getDefinitions(): array
115115
return $defs;
116116
}
117117

118+
/**
119+
* Returns an associative array [string => Definition] that maps fully qualified symbol names
120+
* to global Definitions
121+
*
122+
* @return Definition[]
123+
*/
124+
public function getGlobalDefinitions(): array
125+
{
126+
$defs = [];
127+
foreach ($this->getIndexes() as $index) {
128+
foreach ($index->getGlobalDefinitions() as $fqn => $def) {
129+
$defs[$fqn] = $def;
130+
}
131+
}
132+
return $defs;
133+
}
134+
118135
/**
119136
* Returns the Definitions that are in the given namespace
120137
*

src/Index/Index.php

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,19 @@ class Index implements ReadableIndex, \Serializable
1515
use EmitterTrait;
1616

1717
/**
18-
* An associative array that maps fully qualified symbol names to Definitions
18+
* An associative array that maps fully qualified symbol names to Definitions (global or not)
1919
*
2020
* @var Definition[]
2121
*/
2222
private $definitions = [];
2323

24+
/**
25+
* An associative array that maps fully qualified symbol names to global Definitions
26+
*
27+
* @var Definition[]
28+
*/
29+
private $globalDefinitions = [];
30+
2431
/**
2532
* An associative array that maps namespaces to an associative array of FQN to Definitions
2633
*
@@ -92,7 +99,7 @@ public function isStaticComplete(): bool
9299

93100
/**
94101
* Returns an associative array [string => Definition] that maps fully qualified symbol names
95-
* to Definitions
102+
* to Definitions (global or not)
96103
*
97104
* @return Definition[]
98105
*/
@@ -101,6 +108,17 @@ public function getDefinitions(): array
101108
return $this->definitions;
102109
}
103110

111+
/**
112+
* Returns an associative array [string => Definition] that maps fully qualified symbol names
113+
* to global Definitions
114+
*
115+
* @return Definition[]
116+
*/
117+
public function getGlobalDefinitions(): array
118+
{
119+
return $this->globalDefinitions;
120+
}
121+
104122
/**
105123
* Returns the Definitions that are in the given namespace
106124
*
@@ -144,6 +162,7 @@ public function getDefinition(string $fqn, bool $globalFallback = false)
144162
public function setDefinition(string $fqn, Definition $definition)
145163
{
146164
$this->definitions[$fqn] = $definition;
165+
$this->setGlobalDefinition($fqn, $definition);
147166
$this->setNamespaceDefinition($fqn, $definition);
148167
$this->emit('definition-added');
149168
}
@@ -158,6 +177,7 @@ public function setDefinition(string $fqn, Definition $definition)
158177
public function removeDefinition(string $fqn)
159178
{
160179
unset($this->definitions[$fqn]);
180+
unset($this->globalDefinitions[$fqn]);
161181
unset($this->references[$fqn]);
162182
$this->removeNamespaceDefinition($fqn);
163183
}
@@ -227,10 +247,15 @@ public function removeReferenceUri(string $fqn, string $uri)
227247
public function unserialize($serialized)
228248
{
229249
$data = unserialize($serialized);
250+
230251
foreach ($data as $prop => $val) {
231252
$this->$prop = $val;
232253
}
233-
$this->buildNamespaceDefinitionsIndex();
254+
255+
foreach ($this->definitions as $fqn => $definition) {
256+
$this->setGlobalDefinition($fqn, $definition);
257+
$this->setNamespaceDefinition($fqn, $definition);
258+
}
234259
}
235260

236261
/**
@@ -248,12 +273,16 @@ public function serialize()
248273
}
249274

250275
/**
276+
* Registers a definition to the global definitions index if it is global
277+
*
278+
* @param string $fqn The fully qualified name of the symbol
279+
* @param Definition $definition The Definition object
251280
* @return void
252281
*/
253-
private function buildNamespaceDefinitionsIndex()
282+
private function setGlobalDefinition(string $fqn, Definition $definition)
254283
{
255-
foreach ($this->definitions as $fqn => $definition) {
256-
$this->setNamespaceDefinition($fqn, $definition);
284+
if ($definition->isGlobal) {
285+
$this->globalDefinitions[$fqn] = $definition;
257286
}
258287
}
259288

src/Index/ReadableIndex.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,20 @@ public function isStaticComplete(): bool;
3131

3232
/**
3333
* Returns an associative array [string => Definition] that maps fully qualified symbol names
34-
* to Definitions
34+
* to Definitions (global or not)
3535
*
3636
* @return Definitions[]
3737
*/
3838
public function getDefinitions(): array;
3939

40+
/**
41+
* Returns an associative array [string => Definition] that maps fully qualified symbol names
42+
* to global Definitions
43+
*
44+
* @return Definitions[]
45+
*/
46+
public function getGlobalDefinitions(): array;
47+
4048
/**
4149
* Returns the Definitions that are in the given namespace
4250
*

0 commit comments

Comments
 (0)