[英]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.