【问题标题】:Php format on save in Visual Studio Code在 Visual Studio Code 中保存的 PHP 格式
【发布时间】:2018-07-21 18:51:42
【问题描述】:

你可以format on savePHP 以及Beautify extension of Visual Code 吗?如果是,你如何设置它?如果不是,哪个扩展可以做到这一点?

【问题讨论】:

    标签: php formatting visual-studio-code


    【解决方案1】:

    您可以使用vscode-php-formatter 扩展名,它将按需(通过键绑定)或保存时格式化您的 PHP 代码。

    它是 PHP Coding Standards Fixer 的包装器,正如您所见,它允许非常灵活的配置,因此您可以根据自己的喜好进行调整。

    要添加自定义配置,请创建一个.php_cs 文件,将其添加到您的参数设置中:

    phpformatter.arguments: ["--custom-config=/path/to/file/config.php_cs"]
    

    并使用您的自定义规则创建一个文件:

    <?php
    $finder = Symfony\Component\Finder\Finder::create()
        ->files()
        ->in(__DIR__)
        ->exclude('vendor')
        ->exclude('resources/views')
        ->exclude('storage')
        ->exclude('public')
        ->notName("*.txt")
        ->ignoreDotFiles(true)
        ->ignoreVCS(true);
    
    $fixers = [
        '-psr0',
        '-php_closing_tag',
        'blankline_after_open_tag',
        // more custom rules
    ];
    
    return Symfony\CS\Config\Config::create()
        ->level(Symfony\CS\FixerInterface::PSR2_LEVEL)
        ->fixers($fixers)
        ->finder($finder)
        ->setUsingCache(true);
    

    Read more at the source

    【讨论】:

      【解决方案2】:

      试试 Ben Mewburn 的插件 PHP Intelephense。正如手册所说:

      无损 PSR-12 兼容文档/范围格式。组合格式 HTML/PHP/JS/CSS 文件也是。

      【讨论】:

        猜你喜欢
        • 2019-08-28
        • 2023-03-16
        • 2019-11-24
        • 2015-11-21
        • 1970-01-01
        • 2021-06-26
        • 2015-07-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多