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


当前回答

喜乐!在IDEA 2021.2中,最后有“文件->设置->工具->保存操作”,在这里您可以选择“重新格式化代码”,“优化导入”,“重新排列代码”,“运行代码清理”,“运行eslint—fix”等。

其他回答

以下是尼尔的最新回答。

智能13步:

Code ->重新格式化代码 Edit ->宏->开始宏录制 Code ->重新格式化代码 File ->保存全部 Edit ->宏->停止宏录制 命名宏(类似于“格式化保存”) File -> Settings -> Keymap 右键单击宏。添加键盘快捷键。将键盘快捷键设置为Control + S。 IntelliJ将通知您热键冲突。选择“remove”可删除其他赋值。

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

Ctrl + Alt + O是优化导入

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

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

如果是关于“更漂亮”,那就使用File Watcher:

引用=>工具=>文件观察者=>单击+添加一个新的观察者=>更漂亮

https://prettier.io/docs/en/webstorm.html#running-prettier-on-save-using-file-watcher

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

制作一个宏(我使用了组织导入,格式化代码,保存全部) 给它分配一个击键(我重写了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设置。使用输入字段过滤内容,如截图所示。

我把它设置为签到时自动清理,这对我来说通常已经足够好了。如果有些东西太难看,我就按快捷键(Ctrl-Alt-L,返回)。我看到他们有一个自动格式化粘贴代码的选项,尽管我从来没用过。