如何在 Eclipse 中自动格式化代码?

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

How to auto-format code in Eclipse?

eclipseformattingformatcode-formatting

提问by HasanAboShally

How do you auto-format code in Eclipse?

你如何在 Eclipse 中自动格式化代码?

回答by HasanAboShally

On Windows and Linux: Ctrl+ Shift+ F

在Windows和LinuxCtrl+ Shift+F

On Mac: ?+ ?+ F

在Mac?+ ?+F

(Alternatively you can press Formatin Main Menu > Source)

(或者,您可以Format在主菜单 > 来源中按)

回答by kostja

Another option is to go to Window->Preferences->Java->Editor->SaveActionsand check the Format source codeoption. Then your source code will be formatted truly automatically each time you save it.

另一种选择是转到Window->Preferences->Java->Editor->SaveActions并检查Format source code选项。这样您的源代码将在每次保存时真正自动格式化。

回答by Tabrock

CTRL+ SHIFT+ Fwill auto format your code(whether it is highlighted or non highlighted).

CTRL+ SHIFT+F会自动格式化你的代码(无论是高亮或不高亮显示)。

回答by LimaNightHawk

This can also be done at the Project Level: In the Package Explorer, right-click on the project > Properties > Java Editor > Save Actions

这也可以在项目级别完成:在包资源管理器中,右键单击项目 > 属性 > Java 编辑器 > 保存操作

This might be preferable when working as a team so that everyone's code is saved with the same format settings.

当作为一个团队工作时,这可能更可取,以便每个人的代码都以相同的格式设置保存。

回答by Amado Saladino

Notice: It did not format the document unless I corrected all mistakes. Check your file before pressing CTRLSHIFTF.

注意:除非我更正了所有错误,否则它不会格式化文档。在按 之前检查您的文件CTRLSHIFTF

回答by Zakaria ouh

Press: Ctrl+ Aor highlight the part of the code you wish to indent and then press ctrl+ i.

按:Ctrl+A或突出显示要缩进的代码部分,然后按ctrl+ i

回答by gubs

Windows -> Preferences -> Java -> Editor -> save actions -> Format source code -> Format Edited lines (or) format all lines.

Windows -> Preferences -> Java -> Editor -> save actions -> Format source code -> Format Edited lines(或)格式化所有行。

Some time when you work as a team, lead don't want you to format all lines of the code in a source file (Huge track changes will be there on commit). So, select 'Format Edited lines'. This will edit and format only the lines you modified.

有时当你作为一个团队工作时,领导不希望你格式化源文件中的所有代码行(提交时会有巨大的跟踪更改)。因此,选择“格式化编辑行”。这将仅编辑和格式化您修改的行。

Gubs

古布斯

回答by Khang

You can do with some step bellow

你可以做一些下面的步骤

Step 1: press Ctr+ A(windows) or cmd+ A(Mac os)

第 1 步:按Ctr+ A(Windows)或cmd+ A(Mac 操作系统)

Step 2: Ctr+ Iin windows or cmd+ Iin Mac os

第2步:Ctr+I在Windows或cmd+I在Mac OS

It will auto format for you

它将为您自动格式化

Regards

问候

回答by AGE

The secret is simple: Ctrl+Shift+F

秘密很简单:Ctrl+ Shift+F

回答by Mohammed AZOUZ

We can make it by :
Ctrl+ior Ctrl+Shift+F

我们可以通过:
Ctrl+iCtrl+ Shift+F