我记得在IntelliJ或Eclipse中看到每当保存文件时重新格式化(清理)文件的设置。我怎么找到它(在设置中没有找到它)
当前回答
如果你有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的人编写一个简化版本。
其他回答
如果您正在Flutter中开发,在5/1/2018有一个新的实验性选项,允许您在保存时格式化代码。
我以为IntelliJ中有类似的东西,但我找不到。保存时发生的唯一清理是删除行尾的空白。我认为我必须在某一时刻指定这种行为,但在这一点上我没有看到任何相关的东西。
从2020.1版本开始,你可以直接在pretty插件的Preferences中激活Run on save for file:
以下是尼尔的最新回答。
智能13步:
Code ->重新格式化代码 Edit ->宏->开始宏录制 Code ->重新格式化代码 File ->保存全部 Edit ->宏->停止宏录制 命名宏(类似于“格式化保存”) File -> Settings -> Keymap 右键单击宏。添加键盘快捷键。将键盘快捷键设置为Control + S。 IntelliJ将通知您热键冲突。选择“remove”可删除其他赋值。
我把它设置为签到时自动清理,这对我来说通常已经足够好了。如果有些东西太难看,我就按快捷键(Ctrl-Alt-L,返回)。我看到他们有一个自动格式化粘贴代码的选项,尽管我从来没用过。
推荐文章
- 自定义数字格式字符串始终显示符号
- 如何在Python中拆分这一长行?
- 如何格式化Visual Studio Code项目中的所有文件?
- 转换为二进制并保持前导0
- 如何在IntelliJ IDEA中输入命令行参数?
- 用一个空格替换多个空格
- PhpStorm中的换行
- Java“此语言级别不支持lambda表达式”
- 在IntelliJ IDEA中导入Maven依赖项
- 为什么IntelliJ给我“包不存在”的错误?
- 如何在Java中打印带有2位小数的浮点数?
- 禁用IntelliJ星(包)导入?
- 在Windows批处理脚本中格式化日期和时间
- 如何在IntelliJ IDEA中添加目录到应用程序运行概要文件中的类路径?
- Intellij IDEA上的System.out.println()快捷方式