Fix formatting makes LS non-responsive (#153)
parent
500ae5dc92
commit
50490d51ea
|
@ -28,7 +28,7 @@
|
||||||
"phpdocumentor/reflection-docblock": "^3.0",
|
"phpdocumentor/reflection-docblock": "^3.0",
|
||||||
"sabre/event": "^5.0",
|
"sabre/event": "^5.0",
|
||||||
"felixfbecker/advanced-json-rpc": "^2.0",
|
"felixfbecker/advanced-json-rpc": "^2.0",
|
||||||
"squizlabs/php_codesniffer" : "^2.7",
|
"squizlabs/php_codesniffer" : "3.0.0RC1",
|
||||||
"netresearch/jsonmapper": "^1.0",
|
"netresearch/jsonmapper": "^1.0",
|
||||||
"webmozart/path-util": "^2.3",
|
"webmozart/path-util": "^2.3",
|
||||||
"webmozart/glob": "^4.1",
|
"webmozart/glob": "^4.1",
|
||||||
|
|
|
@ -3,16 +3,22 @@ declare(strict_types = 1);
|
||||||
|
|
||||||
namespace LanguageServer;
|
namespace LanguageServer;
|
||||||
|
|
||||||
use LanguageServer\Protocol\ {
|
use LanguageServer\Protocol\{
|
||||||
TextEdit,
|
TextEdit,
|
||||||
Range,
|
Range,
|
||||||
Position
|
Position
|
||||||
};
|
};
|
||||||
use PHP_CodeSniffer;
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use PHP_CodeSniffer\{
|
||||||
|
Config,
|
||||||
|
Ruleset
|
||||||
|
};
|
||||||
|
use PHP_CodeSniffer\Files\DummyFile;
|
||||||
|
use PHP_CodeSniffer\Util\Tokens;
|
||||||
|
|
||||||
abstract class Formatter
|
abstract class Formatter
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate array of TextEdit changes for content formatting.
|
* Generate array of TextEdit changes for content formatting.
|
||||||
*
|
*
|
||||||
|
@ -24,10 +30,23 @@ abstract class Formatter
|
||||||
*/
|
*/
|
||||||
public static function format(string $content, string $uri)
|
public static function format(string $content, string $uri)
|
||||||
{
|
{
|
||||||
|
if (!defined('PHP_CODESNIFFER_CBF')) {
|
||||||
|
define('PHP_CODESNIFFER_CBF', true);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!defined('PHP_CODESNIFFER_VERBOSITY')) {
|
||||||
|
define('PHP_CODESNIFFER_VERBOSITY', false);
|
||||||
|
}
|
||||||
|
|
||||||
$path = uriToPath($uri);
|
$path = uriToPath($uri);
|
||||||
$cs = new PHP_CodeSniffer();
|
$config = new Config(['dummy'], false);
|
||||||
$cs->initStandard(self::findConfiguration($path));
|
$config->standards = self::findConfiguration($path);
|
||||||
$file = $cs->processFile(null, $content);
|
|
||||||
|
// Autoload class to set up a bunch of PHP_CodeSniffer-specific token type constants
|
||||||
|
spl_autoload_call(Tokens::class);
|
||||||
|
|
||||||
|
$file = new DummyFile($content, new Ruleset($config), $config);
|
||||||
|
$file->process();
|
||||||
$fixed = $file->fixer->fixFile();
|
$fixed = $file->fixer->fixFile();
|
||||||
if (!$fixed && $file->getErrorCount() > 0) {
|
if (!$fixed && $file->getErrorCount() > 0) {
|
||||||
throw new Exception('Unable to format file');
|
throw new Exception('Unable to format file');
|
||||||
|
@ -82,7 +101,7 @@ abstract class Formatter
|
||||||
$currentDir = dirname($currentDir);
|
$currentDir = dirname($currentDir);
|
||||||
} while ($currentDir !== '.' && $currentDir !== $lastDir);
|
} while ($currentDir !== '.' && $currentDir !== $lastDir);
|
||||||
|
|
||||||
$standard = PHP_CodeSniffer::getConfigData('default_standard') ?? 'PSR2';
|
$standard = Config::getConfigData('default_standard') ?? 'PSR2';
|
||||||
return explode(',', $standard);
|
return explode(',', $standard);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue