1
0
Fork 0

Fix formatting makes LS non-responsive (#153)

pull/166/head
Michal Niewrzal 2016-11-18 14:25:05 +01:00 committed by Felix Becker
parent 500ae5dc92
commit 50490d51ea
2 changed files with 26 additions and 7 deletions

View File

@ -28,7 +28,7 @@
"phpdocumentor/reflection-docblock": "^3.0",
"sabre/event": "^5.0",
"felixfbecker/advanced-json-rpc": "^2.0",
"squizlabs/php_codesniffer" : "^2.7",
"squizlabs/php_codesniffer" : "3.0.0RC1",
"netresearch/jsonmapper": "^1.0",
"webmozart/path-util": "^2.3",
"webmozart/glob": "^4.1",

View File

@ -3,16 +3,22 @@ declare(strict_types = 1);
namespace LanguageServer;
use LanguageServer\Protocol\ {
use LanguageServer\Protocol\{
TextEdit,
Range,
Position
};
use PHP_CodeSniffer;
use Exception;
use PHP_CodeSniffer\{
Config,
Ruleset
};
use PHP_CodeSniffer\Files\DummyFile;
use PHP_CodeSniffer\Util\Tokens;
abstract class Formatter
{
/**
* Generate array of TextEdit changes for content formatting.
*
@ -24,10 +30,23 @@ abstract class Formatter
*/
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);
$cs = new PHP_CodeSniffer();
$cs->initStandard(self::findConfiguration($path));
$file = $cs->processFile(null, $content);
$config = new Config(['dummy'], false);
$config->standards = self::findConfiguration($path);
// 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();
if (!$fixed && $file->getErrorCount() > 0) {
throw new Exception('Unable to format file');
@ -82,7 +101,7 @@ abstract class Formatter
$currentDir = dirname($currentDir);
} while ($currentDir !== '.' && $currentDir !== $lastDir);
$standard = PHP_CodeSniffer::getConfigData('default_standard') ?? 'PSR2';
$standard = Config::getConfigData('default_standard') ?? 'PSR2';
return explode(',', $standard);
}
}