[英]How to auto format PHP code curly braces to new lines in VS Code
我目前正在使用 VS Code 的最新版本 1.41.1(在撰写本文时)。
我已经安装了 PHP CS Fixer 来自动格式化我的代码。 然而,有一种我不喜欢的行为。 它总是像这样格式化代码:
if (condition) {
// code...
} else {
// code
}
但这并没有给我很好的可读性。
我想实现这一目标:
if (condition)
{
// code...
}
else
{
// code
}
在 VS Code 中是否有任何扩展支持 PHP 的这种代码格式? 或者 PHP CS Fixer 中是否有任何选项可以跳过这些大括号的格式? 还有其他选择吗?
根据@Nicolas 的评论,我可以分两步完成。
.php_cs
将此代码块添加到文件中:
<?php return PhpCsFixer\\Config::create() ->setRules(array( 'braces' => array( 'position_after_anonymous_constructs' => 'next', 'position_after_control_structures' => 'next', ) ));
一切都完成了,就像一个魅力。 感谢@Nicolas 的帮助!
正在寻找同样的东西。 我尝试了 Radical_activity 给出的(接受的)答案,但这对我不起作用。 在这里找到了另一个片段:
https://stackoverflow.com/a/54883745/4638682
片段:
<?php
$finder = PhpCsFixer\Finder::create()
//->exclude('somedir')
//->notPath('src/Symfony/Component/Translation/Tests/fixtures/resources.php'
->in(__DIR__)
;
return PhpCsFixer\Config::create()
->setRules([
'@PSR2' => true,
'strict_param' => false,
'array_syntax' => ['syntax' => 'long'],
'braces' => [
'allow_single_line_closure' => true,
'position_after_functions_and_oop_constructs' => 'same'],
])
->setFinder($finder)
;
您还需要从中创建一个.php_cs
文件在您的存储库中。 那对我有用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.