-
Notifications
You must be signed in to change notification settings - Fork 187
Allow configurable file extension for indexing #308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 7 commits
cdb5b56
5f096c4
7dc4477
f7175bc
9433694
39cfbda
3c33e7f
d2e5048
b9d0d1b
9067b44
1e319c7
58c82e6
44a942e
940eb97
5b1b6bf
707c97f
1e73d08
1f90b4e
ca225ff
c4568bf
5308e7a
a06057b
23a40f0
f4f1067
9cc2736
09fbec2
a5417cd
e317e8c
a1c3845
a1e5654
a81bed9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -216,28 +216,55 @@ public function xdependencies(): array | |
| } | ||
|
|
||
| /** | ||
| * @param Options|null $settings | ||
| * Fires when client changes settings in the client | ||
| * | ||
| * The default paramter type is Options but it also accepts different types | ||
| * which will be transformed on demand. | ||
| * | ||
| * Currently only the vscode format is supported | ||
|
||
| * | ||
| * @param mixed|null $settings | ||
| * @return bool | ||
|
||
| * @throws \Exception Settings format not valid | ||
| */ | ||
| public function didChangeConfiguration(Options $settings = null) | ||
| public function didChangeConfiguration($settings = null): bool | ||
| { | ||
| // List of options that affect the indexer | ||
| $indexerOptions = ['fileTypes']; | ||
|
|
||
| if ($settings === null) { | ||
| return; | ||
| return false; | ||
| } | ||
|
|
||
| // VSC sends the settings with the config section as main key | ||
| if ($settings instanceof \stdClass && $settings->phpIntelliSense) { | ||
|
||
| $mapper = new \JsonMapper(); | ||
| $settings = $mapper->map($settings->phpIntelliSense, new Options); | ||
|
||
| } | ||
|
|
||
| if (!($settings instanceof Options)) { | ||
|
||
| throw new \Exception('Settings format not valid.'); | ||
|
||
| } | ||
|
|
||
| $changedOptions = $this->getChangedOptions($settings); | ||
| $this->options = $settings; | ||
|
|
||
| if (!empty(array_intersect($changedOptions, $this->options->getIndexerOptions()))) { | ||
| if (empty($changedOptions)) { | ||
| return false; | ||
| } | ||
|
|
||
| foreach (get_object_vars($settings) as $prop => $val) { | ||
| $this->options->$prop = $val; | ||
| } | ||
|
|
||
| if ($this->indexer && !empty(array_intersect($changedOptions, $indexerOptions))) { | ||
|
||
| // check list of options that changed since last time against the list of valid indexer options | ||
|
|
||
| // start wiping from the main index | ||
| // wipe main index and start reindexing | ||
| $this->index->wipe(); | ||
|
|
||
| // check for existing indexer and start indexing | ||
| if ($this->indexer) { | ||
| $this->indexer->index()->otherwise('\\LanguageServer\\crash'); | ||
| } | ||
| $this->indexer->index()->otherwise('\\LanguageServer\\crash'); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -248,10 +275,14 @@ public function didChangeConfiguration(Options $settings = null) | |
| */ | ||
| private function getChangedOptions(Options $settings): array | ||
| { | ||
| // squash nested array for comparing changed options | ||
| $old = array_map('json_encode', get_object_vars($this->options)); | ||
| $new = array_map('json_encode', get_object_vars($settings)); | ||
| $old = get_object_vars($this->options); | ||
| $new = get_object_vars($settings); | ||
| $changed = array_udiff($old, $new, function ($a, $b) { | ||
| // custom callback since array_diff uses strings for comparison | ||
|
|
||
| return $a <=> $b; | ||
| }); | ||
|
|
||
| return array_keys(array_diff($old, $new)); | ||
| return array_keys($changed); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,55 +3,139 @@ | |
|
|
||
| namespace LanguageServer\Tests\Server\Workspace; | ||
|
|
||
| use LanguageServer\Tests\MockProtocolStream; | ||
| use LanguageServer\Tests\Server\ServerTestCase; | ||
| use LanguageServer\{Server, Client, LanguageClient, Project, PhpDocument, Options}; | ||
| use LanguageServer\Protocol\{ | ||
| Message, | ||
| MessageType, | ||
| TextDocumentItem, | ||
| TextDocumentIdentifier, | ||
| SymbolInformation, | ||
| SymbolKind, | ||
| DiagnosticSeverity, | ||
| FormattingOptions, | ||
| Location, | ||
| Range, | ||
| Position | ||
| }; | ||
| use AdvancedJsonRpc\{Request as RequestBody, Response as ResponseBody}; | ||
| use function LanguageServer\pathToUri; | ||
| use LanguageServer\Tests\MockProtocolStream; | ||
| use LanguageServer\{Server, LanguageClient, PhpDocumentLoader, DefinitionResolver, Options, Indexer}; | ||
| use LanguageServer\Index\{ProjectIndex, StubsIndex, GlobalIndex, DependenciesIndex, Index}; | ||
| use LanguageServer\ContentRetriever\FileSystemContentRetriever; | ||
| use LanguageServer\Protocol\{Position, Location, Range, ClientCapabilities, Message, MessageType}; | ||
| use LanguageServer\FilesFinder\FileSystemFilesFinder; | ||
| use LanguageServer\Cache\FileSystemCache; | ||
| use LanguageServer\Server\Workspace; | ||
| use Sabre\Event\Promise; | ||
| use Exception; | ||
|
|
||
| class DidChangeConfigurationTest extends ServerTestCase | ||
| { | ||
| public function testWipingIndex() | ||
| /** | ||
| * didChangeConfiguration does not need to do anything when no options/settings are passed | ||
| */ | ||
| public function testNoOptionPassed() | ||
| { | ||
| $promise = new Promise; | ||
| $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); | ||
|
|
||
| $result = $workspace->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); | ||
|
|
||
| $this->projectIndex->on('wipe', function() use ($promise) { | ||
| $promise->fulfill(); | ||
| }); | ||
|
|
||
| $options = new Options; | ||
| $options->fileTypes = [ | ||
| '.inc' | ||
| ]; | ||
|
|
||
| $this->workspace->didChangeConfiguration($options); | ||
| $promise->wait(); | ||
| $projectIndex->expects($this->once())->method('wipe'); | ||
| $indexer->expects($this->once())->method('index'); | ||
|
|
||
| // invoke event | ||
| $result = $workspace->didChangeConfiguration($options); | ||
| $this->assertTrue($result); | ||
| } | ||
|
|
||
| public function testReindexingAfterWipe() | ||
| /** | ||
| * 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); | ||
|
|
||
| $this->output->on('message', function (Message $msg) use ($promise) { | ||
| $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 0 PHP files parsed') !== false) { | ||
| } elseif (strpos($msg->body->params->message, 'All 1 PHP files parsed') !== false) { | ||
| $promise->fulfill(); | ||
| } | ||
| } | ||
|
|
@@ -62,11 +146,8 @@ public function testReindexingAfterWipe() | |
| '.inc' | ||
| ]; | ||
|
|
||
| $this->workspace->didChangeConfiguration($options); | ||
| $result = $workspace->didChangeConfiguration($options); | ||
| $this->assertTrue($result); | ||
| $promise->wait(); | ||
| } | ||
|
|
||
| public function testGetChangedOptions() | ||
| { | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change this to the protocol wording:
https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#didchangeconfiguration-notification