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

匆匆过客 提交于 2021-02-04 12:49:11

问题


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.

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

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

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


回答1:


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.

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




回答2:


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

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

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

  2. search for phpTidy -> hit enter

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

How to format you code:

  1. ctrl+shift+p -> type -> tidy -> Hit Enter
  2. ctrl+alt+t

Hope this help.




回答3:


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




回答4:


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 PHP under package management.

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




回答5:


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).

The files will be beautified on save.




回答6:


I'm using 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.




回答7:


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.



来源:https://stackoverflow.com/questions/10628372/is-it-possible-to-auto-format-php-in-sublime-text-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!