1
0
Fork 0

Changes after review

pull/35/head
Michal Niewrzal 2016-10-10 13:35:14 +02:00 committed by Felix Becker
parent 6611a30d90
commit 47482fc535
3 changed files with 34 additions and 24 deletions

View File

@ -3,56 +3,70 @@ declare(strict_types = 1);
namespace LanguageServer; namespace LanguageServer;
use LanguageServer\Protocol\{TextEdit, Range, Position, ErrorCode}; use LanguageServer\Protocol\ {
use AdvancedJsonRpc\ResponseError; TextEdit,
Range,
Position
};
use PHP_CodeSniffer; use PHP_CodeSniffer;
use Exception;
class Formatter abstract class Formatter
{ {
/** /**
* Generate array of TextEdit changes for content formatting.
* *
* @param string $content * @param string $content source code to format
* @param string $uri * @param string $uri URI of document
* *
* @return \LanguageServer\Protocol\TextEdit[] * @return \LanguageServer\Protocol\TextEdit[]
* @throws \AdvancedJsonRpc\ResponseError * @throws \Exception
*/ */
public function format(string $content, string $uri) public static function format(string $content, string $uri)
{ {
$path = uriToPath($uri); $path = uriToPath($uri);
$cs = new PHP_CodeSniffer(); $cs = new PHP_CodeSniffer();
$cs->initStandard($this->findConfiguration($path)); $cs->initStandard(self::findConfiguration($path));
$file = $cs->processFile(null, $content); $file = $cs->processFile(null, $content);
$fixed = $file->fixer->fixFile(); $fixed = $file->fixer->fixFile();
if (!$fixed && $file->getErrorCount() > 0) { if (!$fixed && $file->getErrorCount() > 0) {
throw new ResponseError('Unable to format file', ErrorCode::INTERNAL_ERROR); throw new Exception('Unable to format file');
} }
$new = $file->fixer->getContents(); $new = $file->fixer->getContents();
if ($content === $new) { if ($content === $new) {
return []; return [];
} }
return [new TextEdit(new Range(new Position(0, 0), $this->calculateEndPosition($content)), $new)]; return [new TextEdit(new Range(new Position(0, 0), self::calculateEndPosition($content)), $new)];
} }
/** /**
* @param string $content * Calculate position of last character.
*
* @param string $content document as string
*
* @return \LanguageServer\Protocol\Position * @return \LanguageServer\Protocol\Position
*/ */
private function calculateEndPosition(string $content): Position private static function calculateEndPosition(string $content): Position
{ {
$lines = explode("\n", $content); $lines = explode("\n", $content);
return new Position(count($lines) - 1, strlen(end($lines))); 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 * @param string $path path to file or directory
* @return string[] * @return string[]
*/ */
private function findConfiguration(string $path) private static function findConfiguration(string $path)
{ {
$currentDir = dirname($path); if (is_dir($path)) {
$currentDir = $path;
} else {
$currentDir = dirname($path);
}
do { do {
$default = $currentDir . DIRECTORY_SEPARATOR . 'phpcs.xml'; $default = $currentDir . DIRECTORY_SEPARATOR . 'phpcs.xml';
if (is_file($default)) { if (is_file($default)) {

View File

@ -218,17 +218,16 @@ class PhpDocument
} }
/** /**
* Returns this document as formatted text. * Returns array of TextEdit changes to format this document.
* *
* @return string * @return \LanguageServer\Protocol\TextEdit[]
*/ */
public function getFormattedText() public function getFormattedText()
{ {
if (empty($this->getContent())) { if (empty($this->getContent())) {
return []; return [];
} }
$formatter = new Formatter(); return Formatter::format($this->content, $this->uri);
return $formatter->format($this->getContent(), $this->getUri());
} }
/** /**

View File

@ -11,21 +11,18 @@ class FormatterTest extends TestCase
public function testFormat() public function testFormat()
{ {
$formatter = new Formatter();
$input = file_get_contents(__DIR__ . '/../fixtures/format.php'); $input = file_get_contents(__DIR__ . '/../fixtures/format.php');
$output = file_get_contents(__DIR__ . '/../fixtures/format_expected.php'); $output = file_get_contents(__DIR__ . '/../fixtures/format_expected.php');
$edits = $formatter->format($input, 'file:///whatever'); $edits = Formatter::format($input, 'file:///whatever');
$this->assertSame($output, $edits[0]->newText); $this->assertSame($output, $edits[0]->newText);
} }
public function testFormatNoChange() public function testFormatNoChange()
{ {
$formatter = new Formatter();
$expected = file_get_contents(__DIR__ . '/../fixtures/format_expected.php'); $expected = file_get_contents(__DIR__ . '/../fixtures/format_expected.php');
$edits = $formatter->format($expected, 'file:///whatever'); $edits = Formatter::format($expected, 'file:///whatever');
$this->assertSame([], $edits); $this->assertSame([], $edits);
} }
} }