Format code command for PHP/HTML in Visual Studio Code

前端 未结 7 1770
滥情空心
滥情空心 2020-12-29 04:32

Is it possible to get Visual Studio Code to format mixed HTML and PHP code on Windows Visual Studio Code version 0.7.1?

相关标签:
7条回答
  • 2020-12-29 04:34

    For those who found this from a Google search... I use bmewburn.vscode-intelephense-client and it works perfectly (other than if there's an syntax error).

    There is a premium version; I don't know how well that works.

    On GitHub

    On VSCode Marketplace

    Official Site

    0 讨论(0)
  • 2020-12-29 04:37

    For the best format setting for mixed PHP, HTML, and JavaScript code, just use 'PHP CS FIXER'.

    And then use this simple configuration on your setting.json file:

    "php-cs-fixer.executablePath": "${extensionPath}\\php-cs-fixer.phar",
    "[php]": {
        "editor.defaultFormatter": "junstyle.php-cs-fixer",
        "editor.formatOnSave": true
    },
    "php-cs-fixer.rules": "@PSR2",
    "php-cs-fixer.formatHtml": true,
    
    0 讨论(0)
  • 2020-12-29 04:42

    The problem with most of the solutions is that they all are registered as formatting providers and within Visual Studio Code you can only run one formatter on a save for a specific file type.

    So one may get you all the HTML, CSS, JavaScript code, but leave out the PHP code. Or if you use a regular PHP formatter there isn't one that exists that does the HTML correctly.

    I went ahead and made an extension that runs before the save hook and isn't registered as a PHP formatter, so it will do all the HTML with js-beautify and then you can use something like PHPCS + PHPCBF to format the HTML. So it's basically, as far as I'm concerned, the best solution currently available.

    Format HTML in PHP on the Visual Studio Code marketplace.

    0 讨论(0)
  • 2020-12-29 04:45

    Visual Studio Code has pretty awesome PHP support. What it lacks is covered by extensions. A quick search reveals at least three (1, 2, and 3) that claim to support PHP formatting.

    They mostly seem to use the standard shortcut of Alt + Shift + F on Windows/Linux, with varying shortcuts on Mac. If you're on Mac, give ⌥⇧F a try.

    0 讨论(0)
  • 2020-12-29 04:52

    I've tried just about every formatting extension for Visual Studio Code, and I don't think there is a single one that can handle correctly/consistently formatting of mixed PHP/HTML pages.

    0 讨论(0)
  • 2020-12-29 04:52

    Add the extension Format HTML in PHP to Visual Studio Code.

    Or search in extensions with "format HTML in PHP".

    After reload, use Shift + Alt + F.

    0 讨论(0)
提交回复
热议问题