Eclipse:您可以在保存时格式化代码吗?

时间:2022-06-29 10:41:29

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

在Eclipse中,在Windows - > Preference - > Java - > Code Style下,您可以为注释和代码定义代码模板,并且可以设置代码格式化程序。

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.

我想知道在Eclipse中是否有可能每次保存源文件时这些设置都会生效。基本上,我没有突出显示所有内容并按Ctrl + Shift + F,而是希望Eclipse负责确保我的代码格式正确。

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.

关于使用自动格式的注意事项:最好选择“格式编辑行”,因为当您重新格式化源控件中尚未正确格式化的整个文件时,合并更改变得非常困难。已经对同事这样做了。

4 个解决方案

#1


194  

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

在Preferences下,选择Java - > Editor - > Save Actions。选中“保存时执行所选操作”,然后选中“格式化源代码”框。

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

这可能在以前的Eclipse版本中可用,也可能不会。我知道它适用于:

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

#2


14  

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.

我强烈建议将eclipse格式的xml描述符检查到源代码控制中。这样,团队的所有成员都可以使用它,而且你不会来回重新格式化战斗。

#3


9  

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

我有一个设置的快照。请按照路径:

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

在Preferences窗口中,选择Java - > Editor - > Save Actions。

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

选中“保存时执行所选操作”,然后选中“格式化源代码”框。

Eclipse:您可以在保存时格式化代码吗?

#4


2  

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.

如果您发现在Java - > Editor下没有Save Actions首选项,可能是因为您使用的是旧版Eclipse。在这种情况下,您可以从此处安装格式保存插件。

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

然后,在“首选项”下,选择“Java - >格式”。选择“选择代码格式”操作下的“运行格式”选项

#1


194  

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

在Preferences下,选择Java - > Editor - > Save Actions。选中“保存时执行所选操作”,然后选中“格式化源代码”框。

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

这可能在以前的Eclipse版本中可用,也可能不会。我知道它适用于:

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

#2


14  

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.

我强烈建议将eclipse格式的xml描述符检查到源代码控制中。这样,团队的所有成员都可以使用它,而且你不会来回重新格式化战斗。

#3


9  

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

我有一个设置的快照。请按照路径:

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

在Preferences窗口中,选择Java - > Editor - > Save Actions。

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

选中“保存时执行所选操作”,然后选中“格式化源代码”框。

Eclipse:您可以在保存时格式化代码吗?

#4


2  

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.

如果您发现在Java - > Editor下没有Save Actions首选项,可能是因为您使用的是旧版Eclipse。在这种情况下,您可以从此处安装格式保存插件。

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

然后,在“首选项”下,选择“Java - >格式”。选择“选择代码格式”操作下的“运行格式”选项