From 44a942e714e8cd0cdf1ffc70c4c9fc3c1386de0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Steitz?= Date: Thu, 2 Mar 2017 23:11:24 +0100 Subject: [PATCH] Implement didChangeConfiguration event --- src/Server/Workspace.php | 57 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/src/Server/Workspace.php b/src/Server/Workspace.php index 3a10b78..fb3807d 100644 --- a/src/Server/Workspace.php +++ b/src/Server/Workspace.php @@ -3,7 +3,7 @@ declare(strict_types = 1); namespace LanguageServer\Server; -use LanguageServer\{LanguageClient, Project, PhpDocumentLoader}; +use LanguageServer\{LanguageClient, Project, PhpDocumentLoader, Options, Indexer}; use LanguageServer\Index\{ProjectIndex, DependenciesIndex, Index}; use LanguageServer\Protocol\{SymbolInformation, SymbolDescriptor, ReferenceInformation, DependencyReference, Location}; use Sabre\Event\Promise; @@ -32,6 +32,16 @@ class Workspace */ private $sourceIndex; + /** + * @var Options + */ + private $options; + + /** + * @var Indexer + */ + private $indexer; + /** * @var \stdClass */ @@ -48,8 +58,10 @@ class Workspace * @param DependenciesIndex $sourceIndex Index that is used on a workspace/xreferences request * @param \stdClass $composerLock The parsed composer.lock of the project, if any * @param PhpDocumentLoader $documentLoader PhpDocumentLoader instance to load documents + * @param Indexer $indexer + * @param Options $options */ - public function __construct(ProjectIndex $index, DependenciesIndex $dependenciesIndex, Index $sourceIndex, \stdClass $composerLock = null, PhpDocumentLoader $documentLoader, \stdClass $composerJson = null) + public function __construct(ProjectIndex $index, DependenciesIndex $dependenciesIndex, Index $sourceIndex, \stdClass $composerLock = null, PhpDocumentLoader $documentLoader, \stdClass $composerJson = null, Indexer $indexer = null, Options $options = null) { $this->sourceIndex = $sourceIndex; $this->index = $index; @@ -57,6 +69,8 @@ class Workspace $this->composerLock = $composerLock; $this->documentLoader = $documentLoader; $this->composerJson = $composerJson; + $this->indexer = $indexer; + $this->options = $options; } /** @@ -171,8 +185,43 @@ class Workspace return $dependencyReferences; } - public function didChangeConfiguration($settings = null) + /** + * @param Options|null $settings + */ + public function didChangeConfiguration(Options $settings = null) { - $this->index->wipe(); + if ($settings === null) { + return; + } + + $changedOptions = $this->getChangedOptions($settings); + $this->options = $settings; + + if (!empty(array_intersect($changedOptions, $this->options->getIndexerOptions()))) { + // check list of options that changed since last time against the list of valid indexer options + + // start wiping from the main index + $this->index->wipe(); + + // check for existing indexer and start indexing + if ($this->indexer) { + $this->indexer->index()->otherwise('\\LanguageServer\\crash'); + } + } + } + + /** + * Get a list with all options that changed since last time + * + * @param Options $settings + * @return array List with changed options + */ + 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)); + + return array_keys(array_diff($old, $new)); } }