我记得在IntelliJ或Eclipse中看到每当保存文件时重新格式化(清理)文件的设置。我怎么找到它(在设置中没有找到它)


当前回答

从2020.1版本开始,你可以直接在pretty插件的Preferences中激活Run on save for file:

其他回答

从2020.1版本开始,你可以直接在pretty插件的Preferences中激活Run on save for file:

这个解决方案对我来说效果更好:

制作一个宏(我使用了组织导入,格式化代码,保存全部) 给它分配一个击键(我重写了Ctrl+S)

注意:对于有组织的导入,您必须在第一次选中“不再显示此消息”复选框,但在此之后它将按预期工作。

IntelliJ 10.0的步骤:

Code -> "Optimize Imports...", if a dialogue box appears, check the box that says "Do not show this message again.", then click "Run". Tools -> "Start Macro Recording" Code -> "Optimize Imports..." Code -> "Reformat Code..." File -> "Save all" Tools -> "Stop Macro Recording" Name the macro (something like "formatted save") In File -> Settings -> Keymap, select your macro located at "Main Menu -> Tools -> "formatted save" Click "Add Keyboard Shortcut", then perform the keystroke you want. If you choose Ctrl+S like me, it will ask you what to do with the previous Ctrl+S shortcut. Remove it. You can always reassign it later if you want. Enjoy!

对于IntelliJ 11,请替换

步骤2。使用:编辑->宏->“开始宏录制” 步骤6。使用:编辑->宏->“停止宏录制”

其他一切都保持不变。

IntelliJ 12

8. 首选项包含Keymap设置。使用输入字段过滤内容,如截图所示。

如果你有InteliJ Idea Community 2018.2及以上版本,步骤如下:

In the top menu you click: Edit > Macros > Start Macro Recordings (you'll see a window lower right corner of your screen confirming that macros are being recorded) In the top menu you click: Code > Reformat Code (you'll see the option being selected in the lower right corner) In the top menu you click: Code > Optimize Imports (you'll see the option being selected in the lower right corner) In the top menu you click: File > Save All In the top menu you click: Edit > Macros > Stop Macro Recording You name the macro: "Format Code, Organize Imports, Save" In the top menu you clock: File > Settings. In the settings windows you click Keymap In the search box on the right you search "save". You'll find Save All (Ctrl+S). Right click on it and select "Remove Ctrl+S" Remove your search text from the box, press on the Collapse All button (Second button from the top left) Go to macros, press on the arrow to expand your macros, find your saved macro and right click on it. Select Add Keyboard Shortcut, and press Ctrl+S and okay.

重新启动IDE并尝试它。

我知道你要说什么,在我之前的人也写过同样的东西。但我在使用本文上面的步骤时感到困惑,我想为拥有最新版本IDE的人编写一个简化版本。

IntellIJ 14 && 15:当你在提交更改对话框签入代码时,勾选“重新格式化代码”复选框,然后IntellIJ将重新格式化你签入的所有代码。

来源:www.udemy.com/intellij-idea-secrets-double-your-coding-speed-in-2-hours

Ctrl + Alt + L是格式文件(包括下面两个)

Ctrl + Alt + O是优化导入

Ctrl + Alt +我将在特定的行上修复缩进

在提交工作之前,我通常会运行几次Ctrl + Alt + L。我宁愿它做清理/重新格式化在我的命令,而不是自动。