-
Notifications
You must be signed in to change notification settings - Fork 185
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
Feature: configure file size limit #668
Open
JSteitz
wants to merge
29
commits into
felixfbecker:master
Choose a base branch
from
JSteitz:feature/configure-file-size-limit
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 28 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
cdb5b56
Add support to index multiple file extensions
5f096c4
Add test for indexing multiple file types
7dc4477
Fix wrong phpDoc type
f7175bc
Filter invalid file types and use default list as fallback
9433694
Let JsonMapper intialize the options
39cfbda
Add test for fileTypes option
3c33e7f
Initialize options with default values when not provided by client
d2e5048
Update testIndexingMultipleFileTypes
b9d0d1b
Add missing namespace in OptionsTest
9067b44
Fix wrong classname for options test
1e319c7
Wipe index when on configuration change
58c82e6
Add list of valid indexer options
44a942e
Implement didChangeConfiguration event
940eb97
Pass options and indexer to workspace
5b1b6bf
Add tests
707c97f
Merge branch 'master' of github.com:felixfbecker/php-language-server …
1e73d08
Improve gettting changed options
1f90b4e
Update options one by one to update all instance
ca225ff
Remove emitting wipe events
c4568bf
Accept different types/formats from clients
5308e7a
Add new tests and update old ones
a06057b
Fix phpcs warnings/errors
23a40f0
Let didChangeConfiguration decide what options are interesting for th…
f4f1067
Change didChangeConfiguration doc to protocol wording
9cc2736
Merge remote-tracking branch 'upstream/master' into feature/allow-con…
JSteitz 09fbec2
Refactor pull request
JSteitz a5417cd
Fix risky test warning
JSteitz 24e3b77
Add option to set file size limit
JSteitz b7bcf00
Add basic test
JSteitz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<?php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace LanguageServer; | ||
|
||
class Options | ||
{ | ||
/** | ||
* File types the indexer should process | ||
* | ||
* @var string[] | ||
*/ | ||
public $fileTypes = ['.php']; | ||
|
||
/** | ||
* Maximum file size to index | ||
* | ||
* @var int | ||
*/ | ||
public $fileSizeLimit = 150000; | ||
|
||
/** | ||
* Validate/Filter input and set options for file types | ||
* | ||
* @param string[] $fileTypes List of file types | ||
*/ | ||
public function setFileTypes(array $fileTypes) | ||
{ | ||
$fileTypes = filter_var_array($fileTypes, FILTER_SANITIZE_STRING); | ||
$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; | ||
} | ||
|
||
/** | ||
* Validate/Filter input and set option for file size limit | ||
* | ||
* @param string $fileSizeLimit Size in human readable format or -1 for unlimited | ||
*/ | ||
public function setFileSizeLimit(string $fileSizeLimit) | ||
{ | ||
$fileSizeLimit = filter_var($fileSizeLimit, FILTER_SANITIZE_STRING); | ||
|
||
if ($fileSizeLimit === '-1') { | ||
$this->fileSizeLimit = PHP_INT_MAX; | ||
} else { | ||
$this->fileSizeLimit = $this->convertFileSize($fileSizeLimit); | ||
} | ||
} | ||
|
||
/** | ||
* 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; | ||
} | ||
|
||
/** | ||
* Convert human readable file size to byte | ||
* | ||
* @param string $fileSize | ||
* @return int | ||
*/ | ||
private function convertFileSize(string $fileSize) | ||
{ | ||
preg_match('/(\d+)(\w)/', $fileSize, $match); | ||
$sizes = 'KMG'; | ||
$size = (int) $match[1]; | ||
$factor = strpos($sizes, strtoupper($match[2])) + 1; | ||
|
||
return $size * pow(1000, $factor); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@felixfbecker I think this is the right place to pass the file size limit, what do you think?
Just asking, because I need to adjust a lot of tests for that.