是否可以在 Sublime Text 2 中自动格式化 PHP?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10628372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:39:23  来源:igfitidea点击:

Is it possible to auto format PHP in Sublime Text 2?

phpsublimetext2autoformatting

提问by Junior Mayhé

In Sublime Text 2, I am trying to type the code and have auto format command to rearrange and auto indent source code. If it is not possible to have a automatic formatter, would be nice to have a shortcut command to do it.

Sublime Text 2 中,我试图输入代码并使用自动格式化命令来重新排列和自动缩进源代码。如果不可能有自动格式化程序,最好有一个快捷命令来做到这一点。

I am searching for something else besides the built-in "Reindent" option and the hard-to-make-work "PHP Beautifier" plugin.

除了内置的“Reindent”选项和难以制作的“PHP Beautifier”插件之外,我还在寻找其他东西。

I would like to know if someone found any niceauto formatter for PHP: any formatter that won't mess our PHP code. JSFormat seems to be not useful.

我想知道是否有人找到了任何不错的 PHP 自动格式化程序:任何不会弄乱我们的 PHP 代码的格式化程序。JSFormat 似乎没有用。

Is there any easy to "install and use" plugin?

有没有易于“安装和使用”的插件?

采纳答案by MelkorNemesis

I've been searching this before also, but with no avail. Sublime text 2 probably doesn't support formatting PHP code natively. I have looked into ST2 packages - http://wbond.net/sublime_packages/community, but there is also nothing. Probably you'll have to do this in another editor.

我之前也一直在搜索这个,但没有用。Sublime text 2 可能不支持原生格式化 PHP 代码。我查看了 ST2 包 - http://wbond.net/sublime_packages/community,但也没有。可能您必须在另一个编辑器中执行此操作。

Of course it would be nice, if someone would write a package for this, 'cause packages for formatting JS, JSON, HTML.. already exist.

当然,如果有人为此编写一个包会很好,因为用于格式化 JS、JSON、HTML 的包已经存在。

回答by Vukasin

PHPTidy is definitely good choice for PHP code formatting in SublimeText2.

PHPTidy 绝对是 SublimeText2 中 PHP 代码格式化的好选择。

You need to install package control package to enable you to install packaged in sublime text: link.

您需要安装包控制包才能安装 sublime text: link 中的包

  1. ctrl+shift+p-> type -> Package Control: Install Package

  2. search for phpTidy -> hit enter

  1. ctrl+ shift+ p-> 类型 -> 包控制:安装包

  2. 搜索 phpTidy -> 点击 enter

that is it - now you can format your code in WordPress coding convention.

就是这样 - 现在您可以在 WordPress 编码约定中格式化您的代码。

How to format you code:

如何格式化代码:

  1. ctrl+shift+p-> type -> tidy -> Hit Enter
  2. ctrl+alt+t
  1. ctrl+ shift+ p-> 类型 -> 整洁 -> 命中Enter
  2. ctrl+ alt+t

Hope this help.

希望这有帮助。

回答by Philipp Michael

There is a PHP code formatter named PhpBeautifier. Hereyou can find a Sublime Text plugin for this program and you can also install it via package control.

有一个名为 PhpBeautifier 的 PHP 代码格式化程序。在这里你可以找到这个程序的 Sublime Text 插件,你也可以通过包控制来安装它。

回答by Louis

I have this set up as a keybind:

我将此设置为键绑定

[
    {
      "keys": ["alt+shift+f"], "command": "reindent",
      "args": {"single_line": false}
    }
]

If you're looking for a bit more than that there is Tidy PHPunder package management.

如果您正在寻找更多的东西,那就是Tidy PHP包管理。

Update: Been some time, I'm now using PHP Coding Standards along with PHP CS Fixer which automates a lot of the cleanup done by autoformatting. This takes a fair bit of work / understanding of php cs to setup but its not too hard, and well worth it. Link

更新:有一段时间了,我现在正在使用 PHP 编码标准和 PHP CS Fixer,它可以自动执行通过自动格式化完成的大量清理工作。这需要相当多的工作/理解 php cs 来设置,但它并不太难,而且非常值得。关联

回答by Marc Brillault

This is an old subject, but as I found it via Google, here's an answer :

这是一个古老的主题,但正如我通过谷歌找到的,这是一个答案:

You can use the phpfmt plugin for Sublime Text 3, and configure it to respect code conventions (Such as PSR-0, PSR-1 or PSR-2).

您可以使用Sublime Text 3phpfmt 插件,并将其配置为遵守代码约定(例如 PSR-0、PSR-1 或 PSR-2)。

The files will be beautified on save.

文件将在保存时美化。

回答by CodeBrauer

I'm using sublimetext-codeformatter

我正在使用sublimetext-codeformatter

It supports PHP, JavaScript/JSON, HTML, CSS, SCSS, Python, Visual Basic/VBScript and maybe more in the feature. For PHP formatting it uses a fork of the phpfmt which works well. All languages-specific formatting can be set in one config file.

它支持 PHP、JavaScript/JSON、HTML、CSS、SCSS、Python、Visual Basic/VBScript 以及更多功能。对于 PHP 格式化,它使用 phpfmt 的一个分支,效果很好。所有特定于语言的格式都可以在一个配置文件中设置。

回答by BigHeadCreations

Not sure if this is what you are looking for, but are you aware that when you save your file as .php, Sublime Text 2 automatically highlights and indents according to its built in PHP rules. You can also select this from going to View, and then Syntax, and selecting PHP. But again, this is automatically done when you save the file as .php.

不确定这是否是您要查找的内容,但是您是否知道当您将文件另存为 .php 时,Sublime Text 2 会根据其内置的 PHP 规则自动突出显示和缩进。您还可以从“查看”、“语法”和“PHP”中选择此项。但同样,当您将文件另存为 .php 时,这会自动完成。