Listen to the "save" event of an in-memory VirtualFile

Answered

Hi,

I'm creating an in-memory json file using the following code:

VirtualFile virtualFile = new LightVirtualFile(fileName,
FileTypeManager.getInstance().getFileTypeByExtension("json"), "");

Then I open it in the editor with:

OpenFileDescriptor fileDescriptor = new OpenFileDescriptor(project, virtualFile);
FileEditorManager.getInstance(project).openEditor(fileDescriptor, true);

But how do I listen to the event when the user tries to save the file? Previously I was using physical files, so I could use something like FileDocumentSynchronizationVetoer, but now I have to keep it in memory only. Is there a way to do that?

best,

 

0
3 comments

Hi Denis,

Could you please clarify your use case (also from user's perspective)?

0

Hi Karol Lewandowski!

I'm building a plugin to edit documents from a document database. So the user can click on the JSON document and edit it in the editor (it is just way easier to do that VS running the update commands). Once the user tries to"save" the document, I should save it in the database.

The thing is that saving the file locally adds a lot of overhead for me to clean up resources properly, especially when the user has multiple instances of the IDE open. So keeping it in memory will not just be easier to manage, but also more secure, as there is data saved on the user's machine.  But I haven't found a way to listen to the event of the user trying to save a file that is in memory only (if there is one at all) 

0

Hi Denis,

Please try implementing com.intellij.openapi.fileEditor.FileDocumentManagerListener#beforeDocumentSaving.

0

Please sign in to leave a comment.