-
Notifications
You must be signed in to change notification settings - Fork 185
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 20 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
<?php |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,11 +167,12 @@ public function __construct(ProtocolReader $reader, ProtocolWriter $writer) | |
* @param ClientCapabilities $capabilities The capabilities provided by the client (editor) | ||
* @param string|null $rootPath The rootPath of the workspace. Is null if no folder is open. | ||
* @param int|null $processId The process Id of the parent process that started the server. Is null if the process has not been started by another process. If the parent process is not alive then the server should exit (see exit notification) its process. | ||
* @param Options $initializationOptions The options send from client to initialize the server | ||
* @return Promise <InitializeResult> | ||
*/ | ||
public function initialize(ClientCapabilities $capabilities, string $rootPath = null, int $processId = null): Promise | ||
public function initialize(ClientCapabilities $capabilities, string $rootPath = null, int $processId = null, Options $initializationOptions = null): Promise | ||
{ | ||
return coroutine(function () use ($capabilities, $rootPath, $processId) { | ||
return coroutine(function () use ($capabilities, $rootPath, $processId, $initializationOptions) { | ||
|
||
if ($capabilities->xfilesProvider) { | ||
$this->filesFinder = new ClientFilesFinder($this->client); | ||
|
@@ -190,6 +191,7 @@ public function initialize(ClientCapabilities $capabilities, string $rootPath = | |
$this->projectIndex = new ProjectIndex($sourceIndex, $dependenciesIndex, $this->composerJson); | ||
$stubsIndex = StubsIndex::read(); | ||
$this->globalIndex = new GlobalIndex($stubsIndex, $this->projectIndex); | ||
$initializationOptions = $initializationOptions ?? new Options; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This way There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't get you here, is this comment a part from another comment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You pass this variable to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm really sorry but I don't see a problem here. At which point will it be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh sorry, I think I missed the type annotation because I looked at the closure There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
initializationOptions
didChangeConfiguration
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's up to us to defined - I am just saying it's a bit confusing that they are different. It would be much easier for a client if we just document please pass the |
||
|
||
// The DefinitionResolver should look in stubs, the project source and dependencies | ||
$this->definitionResolver = new DefinitionResolver($this->globalIndex); | ||
|
@@ -235,7 +237,8 @@ public function initialize(ClientCapabilities $capabilities, string $rootPath = | |
$sourceIndex, | ||
$this->documentLoader, | ||
$this->composerLock, | ||
$this->composerJson | ||
$this->composerJson, | ||
$initializationOptions | ||
); | ||
$indexer->index()->otherwise('\\LanguageServer\\crash'); | ||
} | ||
|
@@ -259,7 +262,9 @@ public function initialize(ClientCapabilities $capabilities, string $rootPath = | |
$sourceIndex, | ||
$this->composerLock, | ||
$this->documentLoader, | ||
$this->composerJson | ||
$this->composerJson, | ||
$indexer, | ||
$initializationOptions | ||
); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace LanguageServer; | ||
|
||
class Options | ||
{ | ||
/** | ||
* Filetypes the indexer should process | ||
* | ||
* @var string[] | ||
*/ | ||
public $fileTypes = ['.php']; | ||
|
||
/** | ||
* List of options that affect the indexer | ||
*/ | ||
private $indexerOptions = ['fileTypes']; | ||
|
||
/** | ||
* Validate/Filter input and set options for file types | ||
* | ||
* @param array $fileTypes List of file types | ||
*/ | ||
public function setFileTypes(array $fileTypes) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These functions are not needed if you let JsonMapper take care of validation |
||
{ | ||
$fileTypes = filter_var_array($fileTypes, FILTER_SANITIZE_STRING); | ||
$fileTypes = filter_var($fileTypes, FILTER_CALLBACK, ['options' => [$this, 'filterFileTypes']]); // validate file type format | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is one space to much here. |
||
$fileTypes = array_filter($fileTypes, 'strlen'); // filter empty items | ||
$fileTypes = array_values($fileTypes); //rebase indexes | ||
|
||
$this->fileTypes = !empty($fileTypes) ? $fileTypes : $this->fileTypes; | ||
} | ||
|
||
/** | ||
* Get list with options that affect the indexer | ||
* | ||
* @return array | ||
*/ | ||
public function getIndexerOptions(): array | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like to keep |
||
{ | ||
return $this->indexerOptions; | ||
} | ||
|
||
/** | ||
* Filter valid file type | ||
* | ||
* @param string $fileType The file type to filter | ||
* @return string|bool If valid it returns the file type, otherwise false | ||
*/ | ||
private function filterFileTypes(string $fileType) | ||
{ | ||
$fileType = trim($fileType); | ||
|
||
if (empty($fileType)) { | ||
return $fileType; | ||
} | ||
|
||
if (substr($fileType, 0, 1) !== '.') { | ||
return false; | ||
} | ||
|
||
return $fileType; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
|
||
namespace LanguageServer\Server; | ||
|
||
use LanguageServer\{LanguageClient, Project, PhpDocumentLoader}; | ||
use LanguageServer\{LanguageClient, Project, PhpDocumentLoader, Options, Indexer}; | ||
use LanguageServer\Index\{ProjectIndex, DependenciesIndex, Index}; | ||
use LanguageServer\Protocol\{ | ||
FileChangeType, | ||
|
@@ -45,6 +45,16 @@ class Workspace | |
*/ | ||
private $sourceIndex; | ||
|
||
/** | ||
* @var Options | ||
*/ | ||
private $options; | ||
|
||
/** | ||
* @var Indexer | ||
*/ | ||
private $indexer; | ||
|
||
/** | ||
* @var \stdClass | ||
*/ | ||
|
@@ -62,8 +72,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(LanguageClient $client, ProjectIndex $index, DependenciesIndex $dependenciesIndex, Index $sourceIndex, \stdClass $composerLock = null, PhpDocumentLoader $documentLoader, \stdClass $composerJson = null) | ||
public function __construct(LanguageClient $client, ProjectIndex $index, DependenciesIndex $dependenciesIndex, Index $sourceIndex, \stdClass $composerLock = null, PhpDocumentLoader $documentLoader, \stdClass $composerJson = null, Indexer $indexer = null, Options $options = null) | ||
{ | ||
$this->client = $client; | ||
$this->sourceIndex = $sourceIndex; | ||
|
@@ -72,6 +84,8 @@ public function __construct(LanguageClient $client, ProjectIndex $index, Depende | |
$this->composerLock = $composerLock; | ||
$this->documentLoader = $documentLoader; | ||
$this->composerJson = $composerJson; | ||
$this->indexer = $indexer; | ||
$this->options = $options; | ||
} | ||
|
||
/** | ||
|
@@ -200,4 +214,69 @@ public function xdependencies(): array | |
} | ||
return $dependencyReferences; | ||
} | ||
|
||
/** | ||
* Fires when client changes settings in the client | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change this to the protocol wording:
|
||
* | ||
* 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this mean? Where is the problem in saying the PHP LS reads settings under the |
||
* | ||
* @param mixed|null $settings | ||
* @return void | ||
*/ | ||
public function didChangeConfiguration($settings = null) | ||
{ | ||
if ($settings === null) { | ||
return; | ||
} | ||
|
||
// VSC sends the settings with the config section as main key | ||
if ($settings instanceof \stdClass && $settings->phpIntelliSense) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not happy with this part.
|
||
$mapper = new \JsonMapper(); | ||
$settings = $mapper->map($settings->phpIntelliSense, new Options); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we just use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure we can. We only have to lookout for possible conflicts in the future when we use Possible config naming:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
if (!($settings instanceof Options)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Afaik JsonMapper will throw when the schema doesn't match There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you have to tell him to that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What case exactly did not throw an exception? It could be because you typed |
||
return; | ||
} | ||
|
||
$changedOptions = $this->getChangedOptions($settings); | ||
|
||
if (empty($changedOptions)) { | ||
return; | ||
} | ||
|
||
foreach (get_object_vars($settings) as $prop => $val) { | ||
$this->options->$prop = $val; | ||
} | ||
|
||
if ($this->indexer && !empty(array_intersect($changedOptions, $this->options->getIndexerOptions()))) { | ||
// check list of options that changed since last time against the list of valid indexer options | ||
|
||
// wipe main index and start reindexing | ||
$this->index->wipe(); | ||
$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 | ||
{ | ||
$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($changed); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace LanguageServer\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use LanguageServer\Options; | ||
|
||
class OptionsTest extends TestCase | ||
{ | ||
public function testFileTypesOption() | ||
{ | ||
$expected = [ | ||
'.php', | ||
'.valid' | ||
]; | ||
|
||
$options = new Options; | ||
$options->setFileTypes([ | ||
'.php', | ||
false, | ||
12345, | ||
'.valid' | ||
]); | ||
|
||
$this->assertSame($expected, $options->fileTypes); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.