-
Notifications
You must be signed in to change notification settings - Fork 187
Expand file tree
/
Copy pathIndexer.php
More file actions
299 lines (263 loc) Β· 9.53 KB
/
Indexer.php
File metadata and controls
299 lines (263 loc) Β· 9.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
declare(strict_types = 1);
namespace LanguageServer;
use LanguageServer\Cache\Cache;
use LanguageServer\FilesFinder\FilesFinder;
use LanguageServer\Index\{DependenciesIndex, Index};
use LanguageServer\Protocol\MessageType;
use Webmozart\PathUtil\Path;
use Sabre\Event\Promise;
use function Sabre\Event\coroutine;
class Indexer
{
/**
* @var int The prefix for every cache item
*/
const CACHE_VERSION = 2;
/**
* @var FilesFinder
*/
private $filesFinder;
/**
* @var string
*/
private $rootPath;
/**
* @var LanguageClient
*/
private $client;
/**
* @var Cache
*/
private $cache;
/**
* @var DependenciesIndex
*/
private $dependenciesIndex;
/**
* @var Index
*/
private $sourceIndex;
/**
* @var PhpDocumentLoader
*/
private $documentLoader;
/**
* @var Options
*/
private $options;
/**
* @var \stdClasss
*/
private $composerLock;
/**
* @var \stdClasss
*/
private $composerJson;
/**
* @var bool
*/
private $hasCancellationSignal;
/**
* @var bool
*/
private $isIndexing;
/**
* @param FilesFinder $filesFinder
* @param string $rootPath
* @param LanguageClient $client
* @param Cache $cache
* @param DependenciesIndex $dependenciesIndex
* @param Index $sourceIndex
* @param PhpDocumentLoader $documentLoader
* @param \stdClass|null $composerLock
*/
public function __construct(
FilesFinder $filesFinder,
string $rootPath,
LanguageClient $client,
Cache $cache,
DependenciesIndex $dependenciesIndex,
Index $sourceIndex,
PhpDocumentLoader $documentLoader,
\stdClass $composerLock = null,
\stdClass $composerJson = null
) {
$this->filesFinder = $filesFinder;
$this->rootPath = $rootPath;
$this->client = $client;
$this->cache = $cache;
$this->dependenciesIndex = $dependenciesIndex;
$this->sourceIndex = $sourceIndex;
$this->documentLoader = $documentLoader;
$this->composerLock = $composerLock;
$this->composerJson = $composerJson;
$this->hasCancellationSignal = false;
$this->isIndexing = false;
$this->options = new Options();
}
/**
* @param Options $options
*/
public function setOptions(Options $options)
{
$this->options = $options;
}
public function getOptions(): Options
{
return $this->options;
}
/**
* Will read and parse the passed source files in the project and add them to the appropiate indexes
*
* @return Promise <void>
*/
public function index(): Promise
{
return coroutine(function () {
$fileTypes = implode(',', $this->options->fileTypes);
$pattern = Path::makeAbsolute('**/*{' . $fileTypes . '}', $this->rootPath);
$uris = yield $this->filesFinder->find($pattern);
$count = count($uris);
$startTime = microtime(true);
$this->client->window->logMessage(MessageType::INFO, "$count files total");
$this->isIndexing = true;
/** @var string[] */
$source = [];
/** @var string[][] */
$deps = [];
foreach ($uris as $uri) {
$packageName = getPackageName($uri, $this->composerJson);
if ($this->composerLock !== null && $packageName) {
// Dependency file
if (!isset($deps[$packageName])) {
$deps[$packageName] = [];
}
$deps[$packageName][] = $uri;
} else {
// Source file
$source[] = $uri;
}
}
// Index source
// Definitions and static references
$this->client->window->logMessage(MessageType::INFO, 'Indexing project for definitions and static references');
yield $this->indexFiles($source);
$this->sourceIndex->setStaticComplete();
// Dynamic references
$this->client->window->logMessage(MessageType::INFO, 'Indexing project for dynamic references');
yield $this->indexFiles($source);
$this->sourceIndex->setComplete();
// Index dependencies
$this->client->window->logMessage(MessageType::INFO, count($deps) . ' Packages');
foreach ($deps as $packageName => $files) {
// Find version of package and check cache
$packageKey = null;
$cacheKey = null;
$index = null;
foreach (array_merge($this->composerLock->packages, (array)$this->composerLock->{'packages-dev'}) as $package) {
// Check if package name matches and version is absolute
// Dynamic constraints are not cached, because they can change every time
$packageVersion = ltrim($package->version, 'v');
if ($package->name === $packageName && strpos($packageVersion, 'dev') === false) {
$packageKey = $packageName . ':' . $packageVersion;
$cacheKey = self::CACHE_VERSION . ':' . $packageKey;
// Check cache
$index = yield $this->cache->get($cacheKey);
break;
}
}
if ($index !== null) {
// Cache hit
$this->dependenciesIndex->setDependencyIndex($packageName, $index);
$this->client->window->logMessage(MessageType::INFO, "Restored $packageKey from cache");
} else {
// Cache miss
$index = $this->dependenciesIndex->getDependencyIndex($packageName);
// Index definitions and static references
$this->client->window->logMessage(MessageType::INFO, 'Indexing ' . ($packageKey ?? $packageName) . ' for definitions and static references');
yield $this->indexFiles($files);
$index->setStaticComplete();
// Index dynamic references
$this->client->window->logMessage(MessageType::INFO, 'Indexing ' . ($packageKey ?? $packageName) . ' for dynamic references');
yield $this->indexFiles($files);
$index->setComplete();
// If we know the version (cache key), save index for the dependency in the cache
if ($cacheKey !== null) {
$this->client->window->logMessage(MessageType::INFO, "Storing $packageKey in cache");
$this->cache->set($cacheKey, $index);
} else {
$this->client->window->logMessage(MessageType::WARNING, "Could not compute cache key for $packageName");
}
}
}
$this->isIndexing = false;
$duration = (int)(microtime(true) - $startTime);
$mem = (int)(memory_get_usage(true) / (1024 * 1024));
$this->client->window->logMessage(
MessageType::INFO,
"All $count PHP files parsed in $duration seconds. $mem MiB allocated."
);
});
}
/**
* Return current indexing state
*
* @return bool
*/
public function isIndexing(): bool
{
return $this->isIndexing;
}
/**
* Cancel all running indexing processes
*
* @return Promise
*/
public function cancel(): Promise
{
return coroutine(function () {
$this->hasCancellationSignal = true;
while ($this->isIndexing()) {
yield timeout();
}
$this->hasCancellationSignal = false;
$this->client->window->logMessage(MessageType::INFO, 'Indexing project canceled');
});
}
/**
* @param array $files
* @return Promise
*/
private function indexFiles(array $files): Promise
{
return coroutine(function () use ($files) {
foreach ($files as $i => $uri) {
// abort current running indexing
if ($this->hasCancellationSignal) {
return;
}
// Skip open documents
if ($this->documentLoader->isOpen($uri)) {
continue;
}
// Give LS to the chance to handle requests while indexing
yield timeout();
$this->client->window->logMessage(MessageType::LOG, "Parsing $uri");
try {
$document = yield $this->documentLoader->load($uri);
if (!isVendored($document, $this->composerJson)) {
$this->client->textDocument->publishDiagnostics($uri, $document->getDiagnostics());
}
} catch (ContentTooLargeException $e) {
$this->client->window->logMessage(
MessageType::INFO,
"Ignoring file {$uri} because it exceeds size limit of {$e->limit} bytes ({$e->size})"
);
} catch (\Exception $e) {
$this->client->window->logMessage(MessageType::ERROR, "Error parsing $uri: " . (string)$e);
}
}
});
}
}