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?
Please sign in to leave a comment.
You can implement
AnActionListenerand itsbeforeActionPerformed/afterActionPerformed. This way you can track whenSaveAllActionorSaveDocumentActionare running. These two are the "explicit save" actions. You can use this info inside yourFileDocumentManagerListener: in case of an explicit save, its methods are called betweenbeforeActionPerformedandafterActionPerformed.Hi,
Please clarify your use case.
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.
Are my replies recognised? Do I need to start a new thread with examples of its use?
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
ActionOnSaveis 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.It worked perfectly, thank you so much!