-
Notifications
You must be signed in to change notification settings - Fork 185
Use PHP_CodeSniffer as a formatter #35
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6b03196
Use PHP_CodeSniffer as a formatter
552d645
Small cleanup
8de1884
Use PHP_CodeSniffer as a formatter
8268d15
Use PHP_CodeSniffer as a formatter
4c160e1
Added end position calculation
f1c7991
Minor code style fixes
felixfbecker a43d2b2
More code style fixes
felixfbecker 6611a30
Throw exception on invalid file URI
felixfbecker 47482fc
Changes after review
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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; | ||
|
||
use LanguageServer\Protocol\ { | ||
TextEdit, | ||
Range, | ||
Position | ||
}; | ||
use PHP_CodeSniffer; | ||
use Exception; | ||
|
||
abstract class Formatter | ||
{ | ||
/** | ||
* Generate array of TextEdit changes for content formatting. | ||
* | ||
* @param string $content source code to format | ||
* @param string $uri URI of document | ||
* | ||
* @return \LanguageServer\Protocol\TextEdit[] | ||
* @throws \Exception | ||
*/ | ||
public static function format(string $content, string $uri) | ||
{ | ||
$path = uriToPath($uri); | ||
$cs = new PHP_CodeSniffer(); | ||
$cs->initStandard(self::findConfiguration($path)); | ||
$file = $cs->processFile(null, $content); | ||
$fixed = $file->fixer->fixFile(); | ||
if (!$fixed && $file->getErrorCount() > 0) { | ||
throw new Exception('Unable to format file'); | ||
} | ||
|
||
$new = $file->fixer->getContents(); | ||
if ($content === $new) { | ||
return []; | ||
} | ||
return [new TextEdit(new Range(new Position(0, 0), self::calculateEndPosition($content)), $new)]; | ||
} | ||
|
||
/** | ||
* Calculate position of last character. | ||
* | ||
* @param string $content document as string | ||
* | ||
* @return \LanguageServer\Protocol\Position | ||
*/ | ||
private static function calculateEndPosition(string $content): Position | ||
{ | ||
$lines = explode("\n", $content); | ||
return new Position(count($lines) - 1, strlen(end($lines))); | ||
} | ||
|
||
/** | ||
* Search for PHP_CodeSniffer configuration file at given directory or its parents. | ||
* If no configuration found then PSR2 standard is loaded by default. | ||
* | ||
* @param string $path path to file or directory | ||
* @return string[] | ||
*/ | ||
private static function findConfiguration(string $path) | ||
{ | ||
if (is_dir($path)) { | ||
$currentDir = $path; | ||
} else { | ||
$currentDir = dirname($path); | ||
} | ||
do { | ||
$default = $currentDir . DIRECTORY_SEPARATOR . 'phpcs.xml'; | ||
if (is_file($default)) { | ||
return [$default]; | ||
} | ||
|
||
$default = $currentDir . DIRECTORY_SEPARATOR . 'phpcs.xml.dist'; | ||
if (is_file($default)) { | ||
return [$default]; | ||
} | ||
|
||
$lastDir = $currentDir; | ||
$currentDir = dirname($currentDir); | ||
} while ($currentDir !== '.' && $currentDir !== $lastDir); | ||
|
||
$standard = PHP_CodeSniffer::getConfigData('default_standard') ?? 'PSR2'; | ||
return explode(',', $standard); | ||
} | ||
|
||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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\Formatter; | ||
|
||
class FormatterTest extends TestCase | ||
{ | ||
|
||
public function testFormat() | ||
{ | ||
$input = file_get_contents(__DIR__ . '/../fixtures/format.php'); | ||
$output = file_get_contents(__DIR__ . '/../fixtures/format_expected.php'); | ||
|
||
$edits = Formatter::format($input, 'file:///whatever'); | ||
$this->assertSame($output, $edits[0]->newText); | ||
} | ||
|
||
public function testFormatNoChange() | ||
{ | ||
$expected = file_get_contents(__DIR__ . '/../fixtures/format_expected.php'); | ||
|
||
$edits = Formatter::format($expected, 'file:///whatever'); | ||
$this->assertSame([], $edits); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
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.
This is a little brittle. There are also implementations that use
|
instead of the colon on windows. You don't need to test against that, but it would be better to simply dostr_replace('/', DIRECTORY_SEPERATOR, $filepath)
https://en.wikipedia.org/wiki/File_URI_scheme#Windows
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.
Forget what I said. the directory separator should of course be determined by the URI, not the runtime OS.