Eclipse: Can you format code on save?

时光怂恿深爱的人放手 提交于 2019-11-26 18:54:42

问题


In Eclipse, under Windows -> Preference -> Java -> Code Style, you can define code templates for comments and code, and you can setup a code formatter.

I'm wondering if it is possible in Eclipse to have these setting take affect every time I save a source file. Basically, instead of me highlighting everything and pressing Ctrl+Shift+F, I want Eclipse to be responsible for making sure my code is formatted properly.

Is this possible and how do you set it up?

Note On Using the Auto-Format: It's probably best to choose "Format Edited Lines" as merging changes becomes very difficult when you reformat a whole file that is in source control already that was not formatted properly. Already did this to a co-worker.


回答1:


Under Preferences, choose Java --> Editor --> Save Actions. Check the Perform the selected actions on save, and check the Format source code box.

This may or may not be available in previous versions of Eclipse. I know it works in:

Version: 3.3.3.r33x_r20080129-_19UEl7Ezk_gXF1kouft<br>
Build id: M20080221-1800



回答2:


I strongly recommend checking your eclipse format xml descriptor into source control. That way all members of the team can use it and you don't get to and fro reformatting battles.




回答3:


I have a snapshot of the setting. Please follow the path:

In the Preferences window, choose Java --> Editor --> Save Actions.

Check the Perform the selected actions on save, and check the Format source code box.




回答4:


If you find that you do not have a Save Actions preference under Java--> Editor, it may be because you are using an older version of Eclipse. In that case you can install the Format on save plugin from here.

Then, under Preferences, choose Java --> Format on save. Select the Run Format option under Select a code formatting action



来源:https://stackoverflow.com/questions/234594/eclipse-can-you-format-code-on-save

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