- 
                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 23 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; | ||
                
       | 
||
| 
     | 
||
| // 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,49 @@ | ||
| <?php | ||
| 
     | 
||
| namespace LanguageServer; | ||
| 
     | 
||
| class Options | ||
| { | ||
| /** | ||
| * Filetypes the indexer should process | ||
| * | ||
| * @var string[] | ||
| */ | ||
| public $fileTypes = ['.php']; | ||
| 
     | 
||
| /** | ||
| * 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 | ||
                
       | 
||
| $fileTypes = array_filter($fileTypes, 'strlen'); // filter empty items | ||
| $fileTypes = array_values($fileTypes); //rebase indexes | ||
| 
     | 
||
| $this->fileTypes = !empty($fileTypes) ? $fileTypes : $this->fileTypes; | ||
| } | ||
| 
     | 
||
| /** | ||
| * 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,75 @@ public function xdependencies(): array | |
| } | ||
| return $dependencyReferences; | ||
| } | ||
| 
     | 
||
| /** | ||
| * 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($settings = null): bool | ||
| { | ||
| // List of options that affect the indexer | ||
| $indexerOptions = ['fileTypes']; | ||
| 
     | 
||
| if ($settings === null) { | ||
| 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); | ||
| 
     | 
||
| 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 | ||
| 
     | 
||
| // wipe main index and start reindexing | ||
| $this->index->wipe(); | ||
| $this->indexer->index()->otherwise('\\LanguageServer\\crash'); | ||
| } | ||
| 
     | 
||
| return true; | ||
| } | ||
| 
     | 
||
| /** | ||
| * 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.