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


当前回答

我重新绑定了重新格式化代码…操作到Ctrl-S,取代默认绑定保存全部。

乍听起来可能有些疯狂,但IntelliJ似乎节省了几乎每一个操作:运行测试、构建项目,甚至关闭编辑器选项卡。我有一个经常按Ctrl-S的习惯,所以这实际上对我来说很好。在重新格式化时,输入它当然比默认绑定更容易。

其他回答

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

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

如果您正在Flutter中开发,在5/1/2018有一个新的实验性选项,允许您在保存时格式化代码。

我建议使用save actions插件。它还支持优化导入和重新排列代码。

可以很好地与eclipse格式化器插件结合使用。

搜索并激活插件:

配置:

编辑:似乎它最近版本的Intellij保存动作插件是由自动Intellij保存触发的。这可能是相当恼人的,当它仍然在编辑。

这个插件的github问题给出了一些可能的解决方案:

https://github.com/dubreuia/intellij-plugin-save-actions/issues/63

我实际上试图将重新格式化分配给Ctrl+S,它工作得很好-保存现在是自动完成的。

对于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

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

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

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