Minor code style fixes
parent
4c160e1209
commit
f1c799159e
|
@ -5,10 +5,10 @@ namespace LanguageServer;
|
||||||
|
|
||||||
use LanguageServer\Protocol\{TextEdit, Range, Position, ErrorCode};
|
use LanguageServer\Protocol\{TextEdit, Range, Position, ErrorCode};
|
||||||
use AdvancedJsonRpc\ResponseError;
|
use AdvancedJsonRpc\ResponseError;
|
||||||
|
use PHP_CodeSniffer;
|
||||||
|
|
||||||
class Formatter
|
class Formatter
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param string $content
|
* @param string $content
|
||||||
|
@ -20,7 +20,7 @@ class Formatter
|
||||||
public function format(string $content, string $uri)
|
public 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($this->findConfiguration($path));
|
||||||
$file = $cs->processFile(null, $content);
|
$file = $cs->processFile(null, $content);
|
||||||
$fixed = $file->fixer->fixFile();
|
$fixed = $file->fixer->fixFile();
|
||||||
|
@ -42,25 +42,25 @@ class Formatter
|
||||||
private function calculateEndPosition(string $content): Position
|
private function calculateEndPosition(string $content): Position
|
||||||
{
|
{
|
||||||
$lines = explode("\n", $content);
|
$lines = explode("\n", $content);
|
||||||
return new Position(sizeof($lines) - 1, strlen(end($lines)));
|
return new Position(count($lines) - 1, strlen(end($lines)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param string $uri
|
* @param string $path
|
||||||
* @return string[]
|
* @return string[]
|
||||||
*/
|
*/
|
||||||
private function findConfiguration(string $uri)
|
private function findConfiguration(string $path)
|
||||||
{
|
{
|
||||||
$currentDir = dirname($uri);
|
$currentDir = dirname($path);
|
||||||
do {
|
do {
|
||||||
$default = $currentDir . DIRECTORY_SEPARATOR . 'phpcs.xml';
|
$default = $currentDir . DIRECTORY_SEPARATOR . 'phpcs.xml';
|
||||||
if (is_file($default) === true) {
|
if (is_file($default)) {
|
||||||
return [$default];
|
return [$default];
|
||||||
}
|
}
|
||||||
|
|
||||||
$default = $currentDir . DIRECTORY_SEPARATOR . 'phpcs.xml.dist';
|
$default = $currentDir . DIRECTORY_SEPARATOR . 'phpcs.xml.dist';
|
||||||
if (is_file($default) === true) {
|
if (is_file($default)) {
|
||||||
return [$default];
|
return [$default];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,7 +68,7 @@ class Formatter
|
||||||
$currentDir = dirname($currentDir);
|
$currentDir = dirname($currentDir);
|
||||||
} while ($currentDir !== '.' && $currentDir !== $lastDir);
|
} while ($currentDir !== '.' && $currentDir !== $lastDir);
|
||||||
|
|
||||||
$standard = \PHP_CodeSniffer::getConfigData('default_standard') ?? 'PSR2';
|
$standard = PHP_CodeSniffer::getConfigData('default_standard') ?? 'PSR2';
|
||||||
return explode(',', $standard);
|
return explode(',', $standard);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@ class FormatterTest extends TestCase
|
||||||
$output = file_get_contents(__DIR__ . '/../fixtures/format_expected.php');
|
$output = file_get_contents(__DIR__ . '/../fixtures/format_expected.php');
|
||||||
|
|
||||||
$edits = $formatter->format($input, 'whatever');
|
$edits = $formatter->format($input, 'whatever');
|
||||||
$this->assertTrue($edits[0]->newText === $output);
|
$this->assertSame($output, $edits[0]->newText);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFormatNoChange()
|
public function testFormatNoChange()
|
||||||
|
@ -26,7 +26,6 @@ class FormatterTest extends TestCase
|
||||||
$expected = file_get_contents(__DIR__ . '/../fixtures/format_expected.php');
|
$expected = file_get_contents(__DIR__ . '/../fixtures/format_expected.php');
|
||||||
|
|
||||||
$edits = $formatter->format($expected, 'whatever');
|
$edits = $formatter->format($expected, 'whatever');
|
||||||
$this->assertTrue($edits == []);
|
$this->assertSame([], $edits);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -4,50 +4,51 @@ declare(strict_types = 1);
|
||||||
namespace LanguageServer\Tests\Utils;
|
namespace LanguageServer\Tests\Utils;
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use function LanguageServer\{pathToUri, uriToPath};
|
||||||
|
|
||||||
class FileUriTest extends TestCase
|
class FileUriTest extends TestCase
|
||||||
{
|
{
|
||||||
public function testPathToUri()
|
public function testPathToUri()
|
||||||
{
|
{
|
||||||
$uri = \LanguageServer\pathToUri('var/log');
|
$uri = pathToUri('var/log');
|
||||||
$this->assertEquals('file:///var/log', $uri);
|
$this->assertEquals('file:///var/log', $uri);
|
||||||
|
|
||||||
$uri = \LanguageServer\pathToUri('/usr/local/bin');
|
$uri = pathToUri('/usr/local/bin');
|
||||||
$this->assertEquals('file:///usr/local/bin', $uri);
|
$this->assertEquals('file:///usr/local/bin', $uri);
|
||||||
|
|
||||||
$uri = \LanguageServer\pathToUri('a/b/c/test.txt');
|
$uri = pathToUri('a/b/c/test.txt');
|
||||||
$this->assertEquals('file:///a/b/c/test.txt', $uri);
|
$this->assertEquals('file:///a/b/c/test.txt', $uri);
|
||||||
|
|
||||||
$uri = \LanguageServer\pathToUri('/d/e/f');
|
$uri = pathToUri('/d/e/f');
|
||||||
$this->assertEquals('file:///d/e/f', $uri);
|
$this->assertEquals('file:///d/e/f', $uri);
|
||||||
|
|
||||||
// special chars are escaped
|
// special chars are escaped
|
||||||
$uri = \LanguageServer\pathToUri('c:/path/to/file/dürüm döner.php');
|
$uri = pathToUri('c:/path/to/file/dürüm döner.php');
|
||||||
$this->assertEquals('file:///c%3A/path/to/file/d%C3%BCr%C3%BCm+d%C3%B6ner.php', $uri);
|
$this->assertEquals('file:///c%3A/path/to/file/d%C3%BCr%C3%BCm+d%C3%B6ner.php', $uri);
|
||||||
|
|
||||||
//backslashes are transformed
|
//backslashes are transformed
|
||||||
$uri = \LanguageServer\pathToUri('c:\\foo\\bar.baz');
|
$uri = pathToUri('c:\\foo\\bar.baz');
|
||||||
$this->assertEquals('file:///c%3A/foo/bar.baz', $uri);
|
$this->assertEquals('file:///c%3A/foo/bar.baz', $uri);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testUriToPath()
|
public function testUriToPath()
|
||||||
{
|
{
|
||||||
$uri = 'file:///var/log';
|
$uri = 'file:///var/log';
|
||||||
$this->assertEquals('/var/log', \LanguageServer\uriToPath($uri));
|
$this->assertEquals('/var/log', uriToPath($uri));
|
||||||
|
|
||||||
$uri = 'file:///usr/local/bin';
|
$uri = 'file:///usr/local/bin';
|
||||||
$this->assertEquals('/usr/local/bin', \LanguageServer\uriToPath($uri));
|
$this->assertEquals('/usr/local/bin', uriToPath($uri));
|
||||||
|
|
||||||
$uri = 'file:///a/b/c/test.txt';
|
$uri = 'file:///a/b/c/test.txt';
|
||||||
$this->assertEquals('/a/b/c/test.txt', \LanguageServer\uriToPath($uri));
|
$this->assertEquals('/a/b/c/test.txt', uriToPath($uri));
|
||||||
|
|
||||||
$uri = 'file:///d/e/f';
|
$uri = 'file:///d/e/f';
|
||||||
$this->assertEquals('/d/e/f', \LanguageServer\uriToPath($uri));
|
$this->assertEquals('/d/e/f', uriToPath($uri));
|
||||||
|
|
||||||
$uri = 'file:///c:/path/to/file/d%C3%BCr%C3%BCm+d%C3%B6ner.php';
|
$uri = 'file:///c:/path/to/file/d%C3%BCr%C3%BCm+d%C3%B6ner.php';
|
||||||
$this->assertEquals('c:\\path\\to\\file\\dürüm döner.php', \LanguageServer\uriToPath($uri));
|
$this->assertEquals('c:\\path\\to\\file\\dürüm döner.php', uriToPath($uri));
|
||||||
|
|
||||||
$uri = 'file:///c:/foo/bar.baz';
|
$uri = 'file:///c:/foo/bar.baz';
|
||||||
$this->assertEquals('c:\\foo\\bar.baz', \LanguageServer\uriToPath($uri));
|
$this->assertEquals('c:\\foo\\bar.baz', uriToPath($uri));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue