How can I hook into automatic and manual saves separately?

Answered

The methods for hooking into file saves (such as ActionOnSave or FileDocumentManagerListener) do not seem to be able to distinguish between manual saves (when the user actually presses Ctrl+S) and automatic saves (such as when the editor loses focus or when rust-analyzer performs an on-the-fly save). However, the file watcher plugin only executes commands in the case of manual saves. How can I distinguish between these types of saves?

0
6 comments
Official comment

You can implement AnActionListener and its beforeActionPerformed/afterActionPerformed. This way you can track when SaveAllAction or SaveDocumentAction are running. These two are the "explicit save" actions. You can use this info inside your FileDocumentManagerListener: in case of an explicit save, its methods are called between beforeActionPerformed and afterActionPerformed.

Hi,

Please clarify your use case.

0

I am creating an extension to a tool that does automatic formatting on save, similar to file watcher, and I want to prevent files from being formatted on auto-save.

0

Are my replies recognised? Do I need to start a new thread with examples of its use?

0

Hi,

I'm sorry for the delay. I asked my colleague responsible for the File Watchers plugin. Please be patient.

I would like to add that ActionOnSave is used for formatting code on save by JetBrains IDEs (also with external tools, like Prettier), and seems like a correct solution. I'm not sure why you want to avoid doing it on implicit save.

0

It worked perfectly, thank you so much!

0

Please sign in to leave a comment.