Eclipse:你能在保存时格式化代码吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/234594/
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-09-10 18:30:02  来源:igfitidea点击:

Eclipse: Can you format code on save?

eclipsecode-formattingcode-standards

提问by Fostah

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.

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

回答by Neal Swearer

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

在 下Preferences,选择Java--> Editor--> Save Actions。选中Perform the selected actions on save,然后选中Format source code复选框。

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

回答by serg10

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 描述符检查到源代码管理中。这样,团队的所有成员都可以使用它,而您不必来回重新格式化战斗。

回答by VdeX

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

我有一个设置的快照。请遵循以下路径:

In the Preferenceswindow, 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.

检查Perform the selected actions on save,然后检查Format source code box

enter image description here

在此处输入图片说明

回答by VdeX

If you find that you do not have a Save Actionspreference 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.

如果您发现Save ActionsJava-->下没有首选项Editor,可能是因为您使用的是旧版本的 Eclipse。在这种情况下,您可以从此处安装 Format on save 插件。

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

然后,在 下Preferences,选择Java--> Format on save。选择下面的Run Format选项Select a code formatting action