简体   繁体   English

在IntelliJ IDEA中保存操作

[英]Save actions in IntelliJ IDEA

The only thing that exists in Eclipse, which I miss in IntelliJ IDEA is "save actions" - possibility to do some actions (usually formatting) when a file is saved. Eclipse中唯一存在的东西,我在IntelliJ IDEA中遗漏的是“保存操作” - 在保存文件时可以执行某些操作(通常是格式化)。

Is there some plugin or configuration for IDEA which enables such functionality? 是否有一些IDEA的插件或配置能够实现这样的功能?

You will need to Install Save Actions plugin for Intellij IDEA 您需要为Intellij IDEA安装Save Actions插件

  1. Click: ctrl+shift+a 单击:ctrl + shift + a
  2. Type: Plugins 类型:插件
  3. Click: enter Plugins 单击:输入插件
  4. Click: Browse repositories 单击:浏览存储库
  5. Type: Save Actions to search field 键入:将操作保存到搜索字段
  6. Click "install plugin" after you found it 找到后点击“安装插件”
  7. Restart intellij idea 重新启动intellij的想法

Save actions now work. 现在保存操作有效。 You can configure them in File -> Settings -> Other settings 您可以在文件 - >设置 - >其他设置中配置它们

It doesn't exist as in Eclipse, but you can format your code on commit, and the shortcut ctrl + alt + L will allow you to format easily your code. 它不像Eclipse中那样存在,但您可以在提交时格式化代码,快捷键ctrl + alt + L将允许您轻松格式化代码。
After a time formatting the code will be a reflex and you won't even think about it. 经过一段时间格式化代码将是一个反射,你甚至不会考虑它。


On the same topic : 在同一主题上:

You may use a Macro to simulate save actions: 您可以使用宏来模拟保存操作:

Edit --> Macros --> Start Macro Recording 编辑 - >宏 - >开始宏录制

==> now execute all actions you need (eg reformat, rearrange, optimize imports) ==>现在执行您需要的所有操作(例如重新格式化,重新排列,优化导入)

Edit --> Macros --> Stop Macro Recording 编辑 - >宏 - >停止宏录制

Give your new Macro a name. 为您的新宏命名。 You can also assign a shortcut to your macro. 您还可以为宏指定快捷方式。 Just enter it's name in the settings search and you will find it under Macro. 只需在设置搜索中输入它的名称,您就可以在Macro下找到它。

Have fun! 玩得开心!

There is something similar - possibility to do some actions, when a file is committed. 有类似的东西 - 在提交文件时可以执行某些操作。 In a file commit window there is a "Before commit" section which contains checkboxes like "optimize imports", "Reformat code", etc. 在文件提交窗口中有一个“提交前”部分,其中包含“优化导入”,“重新格式化代码”等复选框。

See here: Intellij reformat on file save . 请参阅此处: Intellij重新格式化文件保存

The answer I linked to explains in a few easy steps how to record a macro for optimize imports and reformat and then run that macro when you save your files (bind it to Ctrl+S ). 我链接的答案通过几个简单的步骤解释了如何记录宏以优化导入重新格式化 ,然后在保存文件时运行该宏(将其绑定到Ctrl+S )。

Had the same problem, but Save Actions were not flexible enough. 有同样的问题,但Save Actions不够灵活。 I went for File Watchers plugin which allows for running custom programs each time a file of a given type changes. 我去了File Watchers插件,它允许每次给定类型的文件更改时运行自定义程序。

I think it might break the philosophy that JetBrains have. 我认为这可能会打破JetBrains的理念。 Nothing should be done unless the user/developer requests it (and no Wizards). 除非用户/开发人员请求(并且没有向导),否则不应该做任何事情。 So you are stuck with Ctrl-Alt-L or just indent Ctrl-Alt-I. 所以你坚持使用Ctrl-Alt-L或者只是缩进Ctrl-Alt-I。 I personally only use the latter since the code usually formats itself as you type. 我个人只使用后者,因为代码通常会在您键入时自行格式化。

Also Idea saves files as soon as you leave window or (I think) wait for a certain time, this might really mess up things. 此外,当您离开窗口或(我认为)等待一段时间后,Idea会立即保存文件,这可能会让事情变得非常糟糕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM