1
0
Fork 0

WIP: Implement didChangeConfiguration with reindexing

pull/308/head
Jürgen Steitz 2018-08-31 14:40:23 +02:00
parent e317e8c743
commit a1c3845c9f
8 changed files with 381 additions and 12 deletions

View File

@ -147,4 +147,14 @@ abstract class AbstractAggregateIndex implements ReadableIndex
} }
return $refs; return $refs;
} }
/**
* Wipe all indexes for a reindex
*/
public function wipe()
{
foreach ($this->getIndexes() as $index) {
$index->wipe();
}
}
} }

View File

@ -222,4 +222,15 @@ class Index implements ReadableIndex, \Serializable
'staticComplete' => $this->staticComplete 'staticComplete' => $this->staticComplete
]); ]);
} }
/**
* Clear indexed references and definitions
*/
public function wipe()
{
$this->definitions = [];
$this->references = [];
$this->complete = false;
$this->staticComplete = false;
}
} }

View File

@ -68,6 +68,16 @@ class Indexer
*/ */
private $composerJson; private $composerJson;
/**
* @var bool
*/
private $hasCancellationSignal;
/**
* @var bool
*/
private $isIndexing;
/** /**
* @param FilesFinder $filesFinder * @param FilesFinder $filesFinder
* @param string $rootPath * @param string $rootPath
@ -101,6 +111,8 @@ class Indexer
$this->options = $options; $this->options = $options;
$this->composerLock = $composerLock; $this->composerLock = $composerLock;
$this->composerJson = $composerJson; $this->composerJson = $composerJson;
$this->hasCancellationSignal = false;
$this->isIndexing = false;
} }
/** /**
@ -118,6 +130,7 @@ class Indexer
$count = count($uris); $count = count($uris);
$startTime = microtime(true); $startTime = microtime(true);
$this->client->window->logMessage(MessageType::INFO, "$count files total"); $this->client->window->logMessage(MessageType::INFO, "$count files total");
$this->isIndexing = true;
/** @var string[] */ /** @var string[] */
$source = []; $source = [];
@ -195,6 +208,7 @@ class Indexer
} }
} }
$this->isIndexing = false;
$duration = (int)(microtime(true) - $startTime); $duration = (int)(microtime(true) - $startTime);
$mem = (int)(memory_get_usage(true) / (1024 * 1024)); $mem = (int)(memory_get_usage(true) / (1024 * 1024));
$this->client->window->logMessage( $this->client->window->logMessage(
@ -204,6 +218,34 @@ class Indexer
}); });
} }
/**
* 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;
});
}
/** /**
* @param array $files * @param array $files
* @return Promise * @return Promise
@ -212,6 +254,10 @@ class Indexer
{ {
return coroutine(function () use ($files) { return coroutine(function () use ($files) {
foreach ($files as $i => $uri) { foreach ($files as $i => $uri) {
if ($this->hasCancellationSignal) {
return;
}
// Skip open documents // Skip open documents
if ($this->documentLoader->isOpen($uri)) { if ($this->documentLoader->isOpen($uri)) {
continue; continue;

View File

@ -329,6 +329,7 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher
$dependenciesIndex, $dependenciesIndex,
$sourceIndex, $sourceIndex,
$options, $options,
$indexer,
$this->composerLock, $this->composerLock,
$this->documentLoader, $this->documentLoader,
$this->composerJson $this->composerJson

View File

@ -3,11 +3,12 @@ declare(strict_types = 1);
namespace LanguageServer\Server; namespace LanguageServer\Server;
use LanguageServer\{LanguageClient, PhpDocumentLoader}; use LanguageServer\{Indexer, LanguageClient, Options, PhpDocumentLoader};
use LanguageServer\Index\{ProjectIndex, DependenciesIndex, Index}; use LanguageServer\Index\{ProjectIndex, DependenciesIndex, Index};
use LanguageServer\Protocol\{ use LanguageServer\Protocol\{
FileChangeType, FileChangeType,
FileEvent, FileEvent,
MessageType,
SymbolInformation, SymbolInformation,
SymbolDescriptor, SymbolDescriptor,
ReferenceInformation, ReferenceInformation,
@ -45,6 +46,16 @@ class Workspace
*/ */
private $sourceIndex; private $sourceIndex;
/**
* @var Options
*/
private $options;
/**
* @var Indexer
*/
private $indexer;
/** /**
* @var \stdClass * @var \stdClass
*/ */
@ -60,11 +71,22 @@ class Workspace
* @param ProjectIndex $projectIndex Index that is used to wait for full index completeness * @param ProjectIndex $projectIndex Index that is used to wait for full index completeness
* @param DependenciesIndex $dependenciesIndex Index that is used on a workspace/xreferences request * @param DependenciesIndex $dependenciesIndex Index that is used on a workspace/xreferences request
* @param DependenciesIndex $sourceIndex Index that is used on a workspace/xreferences request * @param DependenciesIndex $sourceIndex Index that is used on a workspace/xreferences request
* @param Options $options Initialization options that are used on a workspace/didChangeConfiguration
* @param Indexer $indexer
* @param \stdClass $composerLock The parsed composer.lock of the project, if any * @param \stdClass $composerLock The parsed composer.lock of the project, if any
* @param PhpDocumentLoader $documentLoader PhpDocumentLoader instance to load documents * @param PhpDocumentLoader $documentLoader PhpDocumentLoader instance to load documents
*/ */
public function __construct(LanguageClient $client, ProjectIndex $projectIndex, DependenciesIndex $dependenciesIndex, Index $sourceIndex, \stdClass $composerLock = null, PhpDocumentLoader $documentLoader, \stdClass $composerJson = null) public function __construct(
{ LanguageClient $client,
ProjectIndex $projectIndex,
DependenciesIndex $dependenciesIndex,
Index $sourceIndex,
Options $options,
Indexer $indexer,
\stdClass $composerLock = null,
PhpDocumentLoader $documentLoader,
\stdClass $composerJson = null
) {
$this->client = $client; $this->client = $client;
$this->sourceIndex = $sourceIndex; $this->sourceIndex = $sourceIndex;
$this->projectIndex = $projectIndex; $this->projectIndex = $projectIndex;
@ -72,10 +94,13 @@ class Workspace
$this->composerLock = $composerLock; $this->composerLock = $composerLock;
$this->documentLoader = $documentLoader; $this->documentLoader = $documentLoader;
$this->composerJson = $composerJson; $this->composerJson = $composerJson;
$this->options = $options;
$this->indexer = $indexer;
} }
/** /**
* The workspace symbol request is sent from the client to the server to list project-wide symbols matching the query string. * The workspace symbol request is sent from the client to the server to list
* project-wide symbols matching the query string.
* *
* @param string $query * @param string $query
* @return Promise <SymbolInformation[]> * @return Promise <SymbolInformation[]>
@ -98,7 +123,8 @@ class Workspace
} }
/** /**
* The watched files notification is sent from the client to the server when the client detects changes to files watched by the language client. * The watched files notification is sent from the client to the server when
* the client detects changes to files watched by the language client.
* *
* @param FileEvent[] $changes * @param FileEvent[] $changes
* @return void * @return void
@ -113,7 +139,8 @@ class Workspace
} }
/** /**
* The workspace references request is sent from the client to the server to locate project-wide references to a symbol given its description / metadata. * The workspace references request is sent from the client to the server to
* locate project-wide references to a symbol given its description / metadata.
* *
* @param SymbolDescriptor $query Partial metadata about the symbol that is being searched for. * @param SymbolDescriptor $query Partial metadata about the symbol that is being searched for.
* @param string[] $files An optional list of files to restrict the search to. * @param string[] $files An optional list of files to restrict the search to.
@ -174,4 +201,47 @@ class Workspace
} }
return $dependencyReferences; return $dependencyReferences;
} }
/**
* A notification sent from the client to the server to signal the change of configuration settings.
*
* @param \stdClass $settings Settings as JSON object structure with php as primary key
* @return Promise
*/
public function didChangeConfiguration(\stdClass $settings): Promise
{
xdebug_break();
return coroutine(function () use ($settings) {
try {
xdebug_break();
$mapper = new \JsonMapper();
$settings = $mapper->map($settings->php, new Options);
if ($this->options == $settings) {
return;
}
// @TODO: get changed settings and apply them
// @TODO: check settings that affect the indexer
if ($this->indexer->isIndexing()) {
yield $this->indexer->cancel();
}
$this->projectIndex->wipe();
$this->indexer->index();
$this->client->window->showMessage(
MessageType::INFO,
'Reindexing with new settings.'
);
} catch (\JsonMapper_Exception $exception) {
$this->client->window->showMessage(
MessageType::ERROR,
'Settings could not be applied. For more information see logs.'
);
$this->client->window->logMessage(MessageType::ERROR, $exception->getMessage());
}
});
}
} }

View File

@ -5,9 +5,7 @@ namespace LanguageServer\Tests\Server;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use LanguageServer\Tests\MockProtocolStream; use LanguageServer\Tests\MockProtocolStream;
use LanguageServer\{ use LanguageServer\{Options, Server, LanguageClient, PhpDocumentLoader, DefinitionResolver};
Server, LanguageClient, PhpDocumentLoader, DefinitionResolver
};
use LanguageServer\Index\{ProjectIndex, DependenciesIndex, Index}; use LanguageServer\Index\{ProjectIndex, DependenciesIndex, Index};
use LanguageServer\ContentRetriever\FileSystemContentRetriever; use LanguageServer\ContentRetriever\FileSystemContentRetriever;
use LanguageServer\Protocol\{Position, Location, Range}; use LanguageServer\Protocol\{Position, Location, Range};
@ -46,6 +44,7 @@ abstract class ServerTestCase extends TestCase
public function setUp() public function setUp()
{ {
$options = new Options();
$sourceIndex = new Index; $sourceIndex = new Index;
$dependenciesIndex = new DependenciesIndex; $dependenciesIndex = new DependenciesIndex;
$projectIndex = new ProjectIndex($sourceIndex, $dependenciesIndex); $projectIndex = new ProjectIndex($sourceIndex, $dependenciesIndex);
@ -55,7 +54,7 @@ abstract class ServerTestCase extends TestCase
$client = new LanguageClient(new MockProtocolStream, new MockProtocolStream); $client = new LanguageClient(new MockProtocolStream, new MockProtocolStream);
$this->documentLoader = new PhpDocumentLoader(new FileSystemContentRetriever, $projectIndex, $definitionResolver); $this->documentLoader = new PhpDocumentLoader(new FileSystemContentRetriever, $projectIndex, $definitionResolver);
$this->textDocument = new Server\TextDocument($this->documentLoader, $definitionResolver, $client, $projectIndex); $this->textDocument = new Server\TextDocument($this->documentLoader, $definitionResolver, $client, $projectIndex);
$this->workspace = new Server\Workspace($client, $projectIndex, $dependenciesIndex, $sourceIndex, null, $this->documentLoader); $this->workspace = new Server\Workspace($client, $projectIndex, $dependenciesIndex, $sourceIndex, $options, null, $this->documentLoader);
$globalSymbolsUri = pathToUri(realpath(__DIR__ . '/../../fixtures/global_symbols.php')); $globalSymbolsUri = pathToUri(realpath(__DIR__ . '/../../fixtures/global_symbols.php'));
$globalReferencesUri = pathToUri(realpath(__DIR__ . '/../../fixtures/global_references.php')); $globalReferencesUri = pathToUri(realpath(__DIR__ . '/../../fixtures/global_references.php'));

View File

@ -0,0 +1,231 @@
<?php
/**
* Copyright (c) 2018 Jürgen Steitz
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
declare(strict_types=1);
namespace LanguageServer\Tests\Server\Workspace;
use Exception;
use LanguageServer\Cache\FileSystemCache;
use LanguageServer\ContentRetriever\FileSystemContentRetriever;
use LanguageServer\DefinitionResolver;
use LanguageServer\FilesFinder\FileSystemFilesFinder;
use LanguageServer\Index\DependenciesIndex;
use LanguageServer\Index\Index;
use LanguageServer\Index\ProjectIndex;
use LanguageServer\Indexer;
use LanguageServer\LanguageClient;
use LanguageServer\Options;
use LanguageServer\PhpDocumentLoader;
use LanguageServer\Protocol\Message;
use LanguageServer\Protocol\MessageType;
use LanguageServer\Server;
use LanguageServer\Tests\MockProtocolStream;
use LanguageServer\Tests\Server\ServerTestCase;
use Sabre\Event\Promise;
class DidChangeConfigurationTest extends ServerTestCase
{
public function testFailsWithInvalidOptionsTypeOrFormat()
{
$promise = new Promise;
$sourceIndex = new Index;
$dependenciesIndex = new DependenciesIndex;
$projectIndex = new ProjectIndex($sourceIndex, $dependenciesIndex);
$projectIndex->setComplete();
$rootPath = realpath(__DIR__ . '/../../../fixtures/');
$filesFinder = new FileSystemFilesFinder;
$cache = new FileSystemCache;
$initialOptions = new Options;
$input = new MockProtocolStream;
$output = new MockProtocolStream;
$definitionResolver = new DefinitionResolver($projectIndex);
$client = new LanguageClient($input, $output);
$documentLoader = new PhpDocumentLoader(new FileSystemContentRetriever, $projectIndex, $definitionResolver);
$textDocument = new Server\TextDocument($documentLoader, $definitionResolver, $client, $projectIndex);
$indexer = new Indexer(
$filesFinder,
$rootPath,
$client,
$cache,
$dependenciesIndex,
$sourceIndex,
$documentLoader,
$initialOptions
);
$workspace = new Server\Workspace(
$client,
$projectIndex,
$dependenciesIndex,
$sourceIndex,
$initialOptions,
null,
$documentLoader
);
$output->on('message', function (Message $msg) use ($promise) {
if ($msg->body->method === 'window/showMessage' && $promise->state === Promise::PENDING) {
$hasMessage = strpos(
$msg->body->params->message,
'Settings could not be applied. For more information see logs.'
) !== false;
if ($msg->body->params->type === MessageType::ERROR && $hasMessage) {
$promise->fulfill(true);
}
if ($msg->body->params->type !== MessageType::ERROR) {
$promise->reject(new Exception($msg->body->params->message));
}
}
});
$settings = new \stdClass();
$settings->php = new \stdClass();
$settings->php->fileTypes = 'not an array';
$workspace->didChangeConfiguration($settings);
$this->assertTrue($promise->wait());
}
public function testNoChangedOptions()
{
$promise = new Promise;
$sourceIndex = new Index;
$dependenciesIndex = new DependenciesIndex;
$projectIndex = new ProjectIndex($sourceIndex, $dependenciesIndex);
$projectIndex->setComplete();
$rootPath = realpath(__DIR__ . '/../../../fixtures/');
$filesFinder = new FileSystemFilesFinder;
$cache = new FileSystemCache;
$initialOptions = new Options;
$input = new MockProtocolStream;
$output = new MockProtocolStream;
$definitionResolver = new DefinitionResolver($projectIndex);
$client = new LanguageClient($input, $output);
$documentLoader = new PhpDocumentLoader(new FileSystemContentRetriever, $projectIndex, $definitionResolver);
$textDocument = new Server\TextDocument($documentLoader, $definitionResolver, $client, $projectIndex);
$indexer = new Indexer(
$filesFinder,
$rootPath,
$client,
$cache,
$dependenciesIndex,
$sourceIndex,
$documentLoader,
$initialOptions
);
$workspace = new Server\Workspace(
$client,
$projectIndex,
$dependenciesIndex,
$sourceIndex,
$initialOptions,
null,
$documentLoader
);
$output->on('message', function (Message $msg) use ($promise) {
$promise->reject(new Exception($msg->body->message));
});
$settings = new \stdClass();
$settings->php = new \stdClass();
$settings->php->fileTypes = ['.php'];
$this->expectException(\LogicException::class);
$workspace->didChangeConfiguration($settings);
$promise->wait();
}
public function testDetectsChangedOptions()
{
$promise = new Promise;
$sourceIndex = new Index;
$dependenciesIndex = new DependenciesIndex;
$projectIndex = new ProjectIndex($sourceIndex, $dependenciesIndex);
$projectIndex->setComplete();
$rootPath = realpath(__DIR__ . '/../../../fixtures/');
$filesFinder = new FileSystemFilesFinder;
$cache = new FileSystemCache;
$initialOptions = new Options;
$input = new MockProtocolStream;
$output = new MockProtocolStream;
$definitionResolver = new DefinitionResolver($projectIndex);
$client = new LanguageClient($input, $output);
$documentLoader = new PhpDocumentLoader(new FileSystemContentRetriever, $projectIndex, $definitionResolver);
$textDocument = new Server\TextDocument($documentLoader, $definitionResolver, $client, $projectIndex);
$indexer = new Indexer(
$filesFinder,
$rootPath,
$client,
$cache,
$dependenciesIndex,
$sourceIndex,
$documentLoader,
$initialOptions
);
$workspace = new Server\Workspace(
$client,
$projectIndex,
$dependenciesIndex,
$sourceIndex,
$initialOptions,
null,
$documentLoader
);
$output->on('message', function (Message $msg) use ($promise) {
if ($msg->body->method === 'window/showMessage' && $promise->state === Promise::PENDING) {
$hasMessage = strpos(
$msg->body->params->message,
'You must restart your editor for the changes to take effect.'
) !== false;
if ($msg->body->params->type === MessageType::INFO && $hasMessage) {
$promise->fulfill(true);
}
if ($msg->body->params->type === MessageType::ERROR) {
$promise->reject(new Exception($msg->body->params->message));
}
}
});
$settings = new \stdClass();
$settings->php = new \stdClass();
$settings->php->fileTypes = ['.php', '.php5']; // default is only .php
$workspace->didChangeConfiguration($settings);
$this->assertTrue($promise->wait());
}
}

View File

@ -4,7 +4,7 @@ declare(strict_types = 1);
namespace LanguageServer\Tests\Server\Workspace; namespace LanguageServer\Tests\Server\Workspace;
use LanguageServer\ContentRetriever\FileSystemContentRetriever; use LanguageServer\ContentRetriever\FileSystemContentRetriever;
use LanguageServer\{DefinitionResolver, LanguageClient, PhpDocumentLoader, Server}; use LanguageServer\{DefinitionResolver, LanguageClient, Options, PhpDocumentLoader, Server};
use LanguageServer\Index\{DependenciesIndex, Index, ProjectIndex}; use LanguageServer\Index\{DependenciesIndex, Index, ProjectIndex};
use LanguageServer\Protocol\{FileChangeType, FileEvent, Message}; use LanguageServer\Protocol\{FileChangeType, FileEvent, Message};
use LanguageServer\Tests\MockProtocolStream; use LanguageServer\Tests\MockProtocolStream;
@ -16,11 +16,12 @@ class DidChangeWatchedFilesTest extends ServerTestCase
{ {
public function testDeletingFileClearsAllDiagnostics() public function testDeletingFileClearsAllDiagnostics()
{ {
$options = new Options();
$client = new LanguageClient(new MockProtocolStream(), $writer = new MockProtocolStream()); $client = new LanguageClient(new MockProtocolStream(), $writer = new MockProtocolStream());
$projectIndex = new ProjectIndex($sourceIndex = new Index(), $dependenciesIndex = new DependenciesIndex()); $projectIndex = new ProjectIndex($sourceIndex = new Index(), $dependenciesIndex = new DependenciesIndex());
$definitionResolver = new DefinitionResolver($projectIndex); $definitionResolver = new DefinitionResolver($projectIndex);
$loader = new PhpDocumentLoader(new FileSystemContentRetriever(), $projectIndex, $definitionResolver); $loader = new PhpDocumentLoader(new FileSystemContentRetriever(), $projectIndex, $definitionResolver);
$workspace = new Server\Workspace($client, $projectIndex, $dependenciesIndex, $sourceIndex, null, $loader, null); $workspace = new Server\Workspace($client, $projectIndex, $dependenciesIndex, $sourceIndex, $options, null, $loader, null);
$fileEvent = new FileEvent('my uri', FileChangeType::DELETED); $fileEvent = new FileEvent('my uri', FileChangeType::DELETED);