Multi-language command-line source code formatter [closed]

跟風遠走 提交于 2019-12-10 12:32:43

问题


Is there a command-line Unix tool that will format/indent/prettify source code in different languages? I'm especially interested in Java, JavaScript, PHP, and XML, but ideally it would handle others.

(I'm not looking for something to generate syntax-highlighting markup; I already know of a few tools that do that.)


回答1:


Artistic Style. http://astyle.sourceforge.net/




回答2:


If you have set your auto-formatting options as project-specific settings in Eclipse, you can do something like:

/opt/local/eclipse/eclipse -nosplash
-application org.eclipse.jdt.core.JavaCodeFormatter
-verbose
-config .settings/org.eclipse.jdt.core.prefs
src/ tests/ doc/examples/

This means that you practically install and configure Eclipse for this purpose if only for using it's autoformatting features, regardless of what editor you use normally. :)

Source: http://blogs.operationaldynamics.com/andrew/software/java-gnome/eclipse-code-format-from-command-line

Additional Notes

On Mac OS X:

/Applications/eclipse/java-oxygen/Eclipse.app/Contents/MacOS/eclipse  -nosplash -application org.eclipse.jdt.core.JavaCodeFormatter -verbose -config ~/my-eclipse-workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.core.prefs MyClass.java



回答3:


I've always found Vim's code formatter a great option. It is aware of many languages and can be reasonably customized.

You can pipe the relevant commands into vim like this:

vim MyClass.java <<< gg=G:wq

Explanation:

  • gg=G formats the file
  • :wq saves the file and returns to the command prompt



回答4:


Check out indent and enscript.




回答5:


Vim generally has automatic syntax highlighting and is available on most Unix-based systems when you install. For formatting and indentation in Vim I use the :set autoindent and :set tabstop=4 automatically when I start it. autoindent keeps the current indentation you are at when you start a new line, and tabstop sets how much your code is indented when you press tab (only for indentation, for tab in general use shiftwidth). To have these options configured whenever you start Vim put them in a ~/.vimrc file.




回答6:


For XML and HTML I have used htb.

If you are an Eclipse user then JTidy is another option.

For Java there is Jalopy.




回答7:


So, I bring to your attention Style Revisor, source code formatter with GUI and command-line interface. It will be support different languages, include JavaScript and PHP. If you're interested in command-line usage - you can define your own formatting style as addon. Of course, you can also use many predefined styles. Example:

./Style\ Revisor --lang=PHP --style=GNU --path=~/to-your-project-root-dir

Currently, Style Revisor supports two languages: C and Objective-C. Welcome: http://style-revisor.com/

Sincerely.



来源:https://stackoverflow.com/questions/1731357/multi-language-command-line-source-code-formatter

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