我记得在IntelliJ或Eclipse中看到每当保存文件时重新格式化(清理)文件的设置。我怎么找到它(在设置中没有找到它)
当前回答
以下是尼尔的最新回答。
智能13步:
Code ->重新格式化代码 Edit ->宏->开始宏录制 Code ->重新格式化代码 File ->保存全部 Edit ->宏->停止宏录制 命名宏(类似于“格式化保存”) File -> Settings -> Keymap 右键单击宏。添加键盘快捷键。将键盘快捷键设置为Control + S。 IntelliJ将通知您热键冲突。选择“remove”可删除其他赋值。
其他回答
如果你有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的人编写一个简化版本。
对于PyCharm/IntelliJ IDEA:
安装黑色。
$ PIP安装黑色
找到您的黑色安装文件夹。
在macOS / Linux / BSD上:
$ which black
/usr/local/bin/black # possible location
在Windows上:
$ where black
%LocalAppData%\Programs\Python\Python36-32\Scripts\black.exe # possible location
请注意,如果您使用的是由PyCharm检测到的虚拟环境,这是一个不必要的步骤。在这种情况下,黑色的路径是$PyInterpreterDirectory$/black。
打开PyCharm/IntelliJ IDEA中的外部工具
macOS系统:PyCharm -> Preferences -> Tools -> External Tools
Windows / Linux / BSD: File -> Settings -> Tools -> External Tools
点击+图标添加一个新的外部工具,值如下:
Name: Black
Description: Black is the uncompromising Python code formatter.
Program: <install_location_from_step_2>
Arguments: "$FilePath$"
选择“工具->外部工具->黑色”,格式化当前打开的文件。
或者,您可以通过导航到首选项或设置-> Keymap ->外部工具->外部工具-黑色来设置键盘快捷键。
可选地,在每个文件保存时运行Black:
确保你已经安装了File Watchers插件。
转到首选项或设置->工具->文件监视器,然后单击+添加一个新的监视器:
Name: Black
File type: Python
Scope: Project Files
Program: <install_location_from_step_2>
Arguments: $FilePath$
Output paths to refresh: $FilePath$
Working directory: $ProjectFileDir$
取消“高级选项”中的“自动保存编辑过的文件以触发监控器”
要使用Black格式化Python文件,我遵循了以下指南,其中也使用了File Watcher: https://black.readthedocs.io/en/stable/editor_integration.html
喜乐!在IDEA 2021.2中,最后有“文件->设置->工具->保存操作”,在这里您可以选择“重新格式化代码”,“优化导入”,“重新排列代码”,“运行代码清理”,“运行eslint—fix”等。
以下是尼尔的最新回答。
智能13步:
Code ->重新格式化代码 Edit ->宏->开始宏录制 Code ->重新格式化代码 File ->保存全部 Edit ->宏->停止宏录制 命名宏(类似于“格式化保存”) File -> Settings -> Keymap 右键单击宏。添加键盘快捷键。将键盘快捷键设置为Control + S。 IntelliJ将通知您热键冲突。选择“remove”可删除其他赋值。
我重新绑定了重新格式化代码…操作到Ctrl-S,取代默认绑定保存全部。
乍听起来可能有些疯狂,但IntelliJ似乎节省了几乎每一个操作:运行测试、构建项目,甚至关闭编辑器选项卡。我有一个经常按Ctrl-S的习惯,所以这实际上对我来说很好。在重新格式化时,输入它当然比默认绑定更容易。
推荐文章
- 设置TextView文本从html格式的字符串资源在XML
- Intellij IDEA Java类在保存时不能自动编译
- Android Studio中的。iml文件是什么?
- 为什么IntelliJ 13 IDEA从12版本升级后这么慢?
- intellij是否可以像在Eclipse中那样组织导入?
- 格式化XML字符串以打印友好的XML字符串
- 导入时无法解析符号
- 如何从IntelliJ IDEA搜索中排除文件扩展名?
- IntelliJ IDEA输出窗口切割输出
- 如何让IntelliJ识别常见的Python模块?
- 如何在Intellij生成串行版本UID
- 如何同步项目到GitHub与Android工作室?
- IntelliJ IDEA with Junit 4.7”!!JUnit 3.8或更高版本:
- 如何显示默认隐藏字符(零宽度空间,即。& # 8203)
- 自定义数字格式字符串始终显示符号