Skip to content
This repository has been archived by the owner on Mar 6, 2022. It is now read-only.

IndexerHandler: use ProgressNotifier #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions lib/LanguageServerIndexer/Handler/IndexerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Phpactor\Indexer\Model\Indexer;
use Phpactor\LanguageServer\Core\Service\ServiceManager;
use Phpactor\LanguageServer\Core\Service\ServiceProvider;
use Phpactor\LanguageServer\WorkDoneProgress\ProgressNotifier;
use Phpactor\LanguageServer\WorkDoneProgress\WorkDoneToken;
use Phpactor\TextDocument\Exception\TextDocumentNotFound;
use Phpactor\TextDocument\TextDocumentBuilder;
use Psr\EventDispatcher\EventDispatcherInterface;
Expand Down Expand Up @@ -46,6 +48,11 @@ class IndexerHandler implements Handler, ServiceProvider
*/
private $clientApi;

/**
* @var ProgressNotifier
*/
private $progressNotifier;

/**
* @var ServiceManager
*/
Expand All @@ -65,13 +72,15 @@ public function __construct(
Indexer $indexer,
Watcher $watcher,
ClientApi $clientApi,
ProgressNotifier $progressNotifier,
LoggerInterface $logger,
EventDispatcherInterface $eventDispatcher
) {
$this->indexer = $indexer;
$this->watcher = $watcher;
$this->logger = $logger;
$this->clientApi = $clientApi;
$this->progressNotifier = $progressNotifier;
$this->eventDispatcher = $eventDispatcher;
}

Expand Down Expand Up @@ -103,22 +112,29 @@ public function indexer(CancellationToken $cancel): Promise
return \Amp\call(function () use ($cancel) {
$job = $this->indexer->getJob();
$size = $job->size();
$this->clientApi->window()->showMessage()->info(sprintf('Indexing "%s" PHP files', $size));
$workDoneToken = WorkDoneToken::generate();
$this->progressNotifier->create($workDoneToken);
$this->progressNotifier->begin(
$workDoneToken,
'Indexing',
sprintf('Indexing "%s" PHP files', $size),
0,
false,
);

$start = microtime(true);
$index = 0;
foreach ($job->generator() as $file) {
$index++;

if ($index % 500 === 0) {
if ($index % 50 === 0) {
$usage = MemoryUsage::create();
$this->clientApi->window()->showMessage()->info(sprintf(
'Indexed %s/%s (%s%%) %s',
$this->progressNotifier->report($workDoneToken, sprintf(
'Indexed %s/%s %s',
$index,
$size,
number_format($index / $size * 100, 2),
$usage->memoryUsageFormatted()
));
), (int) ($index / $size * 100), false);
}

try {
Expand All @@ -131,7 +147,7 @@ public function indexer(CancellationToken $cancel): Promise
}

$process = yield $this->watcher->watch();
$this->clientApi->window()->showMessage()->info(sprintf(
$this->progressNotifier->end($workDoneToken, sprintf(
'Done indexing (%ss, %s), watching with %s',
number_format(microtime(true) - $start, 2),
MemoryUsage::create()->memoryUsageFormatted(),
Expand Down
2 changes: 2 additions & 0 deletions lib/LanguageServerIndexer/LanguageServerIndexerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Phpactor\LanguageServerProtocol\ClientCapabilities;
use Phpactor\LanguageServer\Core\Server\ClientApi;
use Phpactor\LanguageServer\Core\Service\ServiceManager;
use Phpactor\LanguageServer\WorkDoneProgress\ProgressNotifier;
use Phpactor\MapResolver\Resolver;
use Phpactor\TextDocument\TextDocumentLocator;
use Psr\EventDispatcher\EventDispatcherInterface;
Expand Down Expand Up @@ -61,6 +62,7 @@ private function registerSessionHandler(ContainerBuilder $container): void
$container->get(Indexer::class),
$container->get(Watcher::class),
$container->get(ClientApi::class),
$container->get(ProgressNotifier::class),
$container->get(LoggingExtension::SERVICE_LOGGER),
$container->get(EventDispatcherInterface::class)
);
Expand Down