After Save Hooks

Answered

It may sound crazy, but I want to implement a plugin that does something everytime I save. But not before the file is actually saved I need to know when it is saved. I tried to find the documentation on what happens (what events are fired) when the user saves a document. But I didn't find what I was looking for. This post: https://intellij-support.jetbrains.com/hc/en-us/community/posts/206377699-After-save-hook?input_string=After%20Save%20Hooks Doesn't help me. I don't even want to trigger something external. 

I hope my question is clear

 

Donut

8 comments
Comment actions Permalink

Check out com.intellij.openapi.fileEditor.FileDocumentManagerListener.

0
Comment actions Permalink

I have done that. The only method I came across, that hopefully does what I need is fileContentReloaded. Is every document reloaded after it was saved?

0
Comment actions Permalink

I think you can only get an event before file is saved currently.

0
Comment actions Permalink

I found a "solution". It might not be the correct/efficient way or best practice, but it works for me. I directly register a VirtualFileListener to the VirtualFileManager

VirtualFileManager.getInstance().addVirtualFileListener(new VirtualFileAdapter() {
@Override
public void contentsChanged(@NotNull VirtualFileEvent event) {
if (event.isFromSave()) {
//do stuff
}
}
});

Any thoughts on this workaround?

0
Comment actions Permalink

The solution above is quite correct. Hope you aren't going to modify files on a disk after the save, aren't you?

 

> But not before the file is actually saved I need to know when it is saved.

This part is somewhat obscure; could you please clarify?

 

0
Comment actions Permalink

> Hope you aren't going to modify files on a disk after the save, aren't you?

Of course not. What do you think of me? :-)

The idea (pun not intended) is to run a specific task every time I save "something". This something might get more and more defined over the next iterations, but for now it is just any document. The task I want to run is a test runner (e.g jUnit). This is why I think I need to run it right after everything was saved. I don't want to run with an outdated version of the file.

0
Comment actions Permalink

OK :)

For this use case, the solution is definitely correct.

0
Comment actions Permalink

Thank you for the feedback. :-)

I will continue developing and might have some other questions (about run configurations etc.) in other threads.

0

Please sign in to leave a comment.