From 09fbec247c22f2e5355652c47f55c6d2d54fd2f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Steitz?= Date: Wed, 29 Aug 2018 21:34:50 +0200 Subject: [PATCH] Refactor pull request * merge latest upstream * remove currently not required code blocks * fix tests --- src/Index/AbstractAggregateIndex.php | 10 -- src/Index/Index.php | 8 - src/Indexer.php | 34 ++-- src/LanguageServer.php | 1 + src/Options.php | 11 +- tests/LanguageServerTest.php | 16 +- .../Workspace/DidChangeConfigurationTest.php | 153 ------------------ 7 files changed, 31 insertions(+), 202 deletions(-) delete mode 100644 tests/Server/Workspace/DidChangeConfigurationTest.php diff --git a/src/Index/AbstractAggregateIndex.php b/src/Index/AbstractAggregateIndex.php index 33b86e5..5377c3a 100644 --- a/src/Index/AbstractAggregateIndex.php +++ b/src/Index/AbstractAggregateIndex.php @@ -147,14 +147,4 @@ abstract class AbstractAggregateIndex implements ReadableIndex } return $refs; } - - /** - * Wipe all indexes for a reindex - */ - public function wipe() - { - foreach ($this->getIndexes() as $index) { - $index->wipe(); - } - } } diff --git a/src/Index/Index.php b/src/Index/Index.php index a88c7ba..9cb975e 100644 --- a/src/Index/Index.php +++ b/src/Index/Index.php @@ -222,12 +222,4 @@ class Index implements ReadableIndex, \Serializable 'staticComplete' => $this->staticComplete ]); } - - public function wipe() - { - $this->definitions = []; - $this->references = []; - $this->complete = false; - $this->staticComplete = false; - } } diff --git a/src/Indexer.php b/src/Indexer.php index fd59aa3..e411054 100644 --- a/src/Indexer.php +++ b/src/Indexer.php @@ -53,6 +53,11 @@ class Indexer */ private $documentLoader; + /** + * @var Options + */ + private $options; + /** * @var \stdClasss */ @@ -64,20 +69,15 @@ class Indexer private $composerJson; /** - * @var Options - */ - private $options; - - /** - * @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 - * @param Options|null $options + * @param FilesFinder $filesFinder + * @param string $rootPath + * @param LanguageClient $client + * @param Cache $cache + * @param DependenciesIndex $dependenciesIndex + * @param Index $sourceIndex + * @param Options $options + * @param PhpDocumentLoader $documentLoader + * @param \stdClass|null $composerLock */ public function __construct( FilesFinder $filesFinder, @@ -87,9 +87,9 @@ class Indexer DependenciesIndex $dependenciesIndex, Index $sourceIndex, PhpDocumentLoader $documentLoader, + Options $options, \stdClass $composerLock = null, - \stdClass $composerJson = null, - Options $options = null + \stdClass $composerJson = null ) { $this->filesFinder = $filesFinder; $this->rootPath = $rootPath; @@ -98,9 +98,9 @@ class Indexer $this->dependenciesIndex = $dependenciesIndex; $this->sourceIndex = $sourceIndex; $this->documentLoader = $documentLoader; + $this->options = $options; $this->composerLock = $composerLock; $this->composerJson = $composerJson; - $this->options = $options; } /** diff --git a/src/LanguageServer.php b/src/LanguageServer.php index 193b0a1..b29cb1d 100644 --- a/src/LanguageServer.php +++ b/src/LanguageServer.php @@ -232,6 +232,7 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher $dependenciesIndex, $sourceIndex, $this->documentLoader, + $initializationOptions, $this->composerLock, $this->composerJson, $initializationOptions diff --git a/src/Options.php b/src/Options.php index 99f1aab..48ba540 100644 --- a/src/Options.php +++ b/src/Options.php @@ -1,11 +1,12 @@ [$this, 'filterFileTypes']]); // validate file type format - $fileTypes = array_filter($fileTypes, 'strlen'); // filter empty items - $fileTypes = array_values($fileTypes); //rebase indexes + $fileTypes = filter_var($fileTypes, FILTER_CALLBACK, ['options' => [$this, 'filterFileTypes']]); + $fileTypes = array_filter($fileTypes, 'strlen'); + $fileTypes = array_values($fileTypes); $this->fileTypes = !empty($fileTypes) ? $fileTypes : $this->fileTypes; } diff --git a/tests/LanguageServerTest.php b/tests/LanguageServerTest.php index 3354b68..5896eac 100644 --- a/tests/LanguageServerTest.php +++ b/tests/LanguageServerTest.php @@ -30,7 +30,7 @@ class LanguageServerTest extends TestCase public function testInitialize() { $server = new LanguageServer(new MockProtocolStream, new MockProtocolStream); - $result = $server->initialize(new ClientCapabilities, __DIR__, getmypid())->wait(); + $result = $server->initialize(new ClientCapabilities, __DIR__, getmypid(), new Options)->wait(); $serverCapabilities = new ServerCapabilities(); $serverCapabilities->textDocumentSync = TextDocumentSyncKind::FULL; @@ -67,7 +67,7 @@ class LanguageServerTest extends TestCase }); $server = new LanguageServer($input, $output); $capabilities = new ClientCapabilities; - $server->initialize($capabilities, realpath(__DIR__ . '/../fixtures'), getmypid()); + $server->initialize($capabilities, realpath(__DIR__ . '/../fixtures'), getmypid(), new Options); $promise->wait(); } @@ -115,7 +115,7 @@ class LanguageServerTest extends TestCase $capabilities = new ClientCapabilities; $capabilities->xfilesProvider = true; $capabilities->xcontentProvider = true; - $server->initialize($capabilities, $rootPath, getmypid()); + $server->initialize($capabilities, $rootPath, getmypid(), new Options); $promise->wait(); $this->assertTrue($filesCalled); $this->assertTrue($contentCalled); @@ -127,24 +127,22 @@ class LanguageServerTest extends TestCase $input = new MockProtocolStream; $output = new MockProtocolStream; $options = new Options; - $options->setFileTypes([ '.php', '.inc' ]); - - $output->on('message', function (Message $msg) use ($promise, &$foundFiles) { + $output->on('message', function (Message $msg) use ($promise, &$allFilesParsed) { if ($msg->body->method === 'window/logMessage' && $promise->state === Promise::PENDING) { if ($msg->body->params->type === MessageType::ERROR) { $promise->reject(new Exception($msg->body->params->message)); - } else if (strpos($msg->body->params->message, 'All 27 PHP files parsed') !== false) { - $promise->fulfill(); + } elseif (preg_match('/All \d+ PHP files parsed/', $msg->body->params->message)) { + $promise->fulfill(true); } } }); $server = new LanguageServer($input, $output); $capabilities = new ClientCapabilities; $server->initialize($capabilities, realpath(__DIR__ . '/../fixtures'), getmypid(), $options); - $promise->wait(); + $this->assertTrue($promise->wait()); } } diff --git a/tests/Server/Workspace/DidChangeConfigurationTest.php b/tests/Server/Workspace/DidChangeConfigurationTest.php deleted file mode 100644 index 6028c6c..0000000 --- a/tests/Server/Workspace/DidChangeConfigurationTest.php +++ /dev/null @@ -1,153 +0,0 @@ -didChangeConfiguration(); - $this->assertFalse($result); - } - - /** - * When the passed options/settings do not differ from the previous, it has nothing to do - */ - public function testFailsWithInvalidOptionsTypeOrFormat() - { - $options = new Options; - $client = new LanguageClient(new MockProtocolStream(), $writer = new MockProtocolStream()); - $projectIndex = new ProjectIndex($sourceIndex = new Index(), $dependenciesIndex = new DependenciesIndex()); - $definitionResolver = new DefinitionResolver($projectIndex); - $loader = new PhpDocumentLoader(new FileSystemContentRetriever(), $projectIndex, $definitionResolver); - $workspace = new Server\Workspace($client, $projectIndex, $dependenciesIndex, $sourceIndex, null, $loader, null, null, null, null, $options); - - $this->expectException(\Exception::class); - $this->workspace->didChangeConfiguration(['invalid' => 'options format']); - } - - /** - * When the passed options/settings do not differ from the previous, it has nothing to do - */ - public function testNoChangedOptions() - { - $options = new Options; - $client = new LanguageClient(new MockProtocolStream(), $writer = new MockProtocolStream()); - $projectIndex = new ProjectIndex($sourceIndex = new Index(), $dependenciesIndex = new DependenciesIndex()); - $definitionResolver = new DefinitionResolver($projectIndex); - $loader = new PhpDocumentLoader(new FileSystemContentRetriever(), $projectIndex, $definitionResolver); - $workspace = new Server\Workspace($client, $projectIndex, $dependenciesIndex, $sourceIndex, null, $loader, null, null, null, null, $options); - - $result = $this->workspace->didChangeConfiguration($options); - $this->assertFalse($result); - } - - /** - * Verify that the required methods for a reindex are called - */ - public function testFileTypesOptionTriggersAReindex() - { - $sourceIndex = new Index; - $dependenciesIndex = new DependenciesIndex; - $projectIndex = $this->getMockBuilder('LanguageServer\Index\ProjectIndex') - ->setConstructorArgs([$sourceIndex, $dependenciesIndex]) - ->setMethods(['wipe']) - ->getMock(); - $projectIndex->setComplete(); - - $rootPath = realpath(__DIR__ . '/../../../fixtures/'); - $filesFinder = new FileSystemFilesFinder; - $cache = new FileSystemCache; - - $definitionResolver = new DefinitionResolver($projectIndex); - $client = new LanguageClient(new MockProtocolStream, new MockProtocolStream); - $documentLoader = new PhpDocumentLoader(new FileSystemContentRetriever, $projectIndex, $definitionResolver); - $textDocument = new Server\TextDocument($documentLoader, $definitionResolver, $client, $projectIndex); - $indexer = $this->getMockBuilder('LanguageServer\Indexer') - ->setConstructorArgs([$filesFinder, $rootPath, $client, $cache, $dependenciesIndex, $sourceIndex, $documentLoader, null, null, new Options]) - ->setMethods(['index']) - ->getMock(); - $workspace = new Server\Workspace($client, $projectIndex, $dependenciesIndex, $sourceIndex, null, $documentLoader, null, $indexer, new Options); - - - $options = new Options; - $options->fileTypes = [ - '.inc' - ]; - - $projectIndex->expects($this->once())->method('wipe'); - $indexer->expects($this->once())->method('index'); - - // invoke event - $result = $workspace->didChangeConfiguration($options); - $this->assertTrue($result); - } - - /** - * Be sure that the indexer gets the new options/settings and uses them - */ - public function testIndexerUsesNewOptions() - { - $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, null, null, $initialOptions); - $workspace = new Server\Workspace($client, $projectIndex, $dependenciesIndex, $sourceIndex, null, $documentLoader, null, $indexer, $initialOptions); - - $output->on('message', function (Message $msg) use ($promise) { - if ($msg->body->method === 'window/logMessage' && $promise->state === Promise::PENDING) { - if ($msg->body->params->type === MessageType::ERROR) { - $promise->reject(new Exception($msg->body->params->message)); - } elseif (strpos($msg->body->params->message, 'All 1 PHP files parsed') !== false) { - $promise->fulfill(); - } - } - }); - - $options = new Options; - $options->fileTypes = [ - '.inc' - ]; - - $result = $workspace->didChangeConfiguration($options); - $this->assertTrue($result); - $promise->wait(); - } -}