Synchronize virtual file to the physical file in t

2019-06-22 07:55发布

I'm implementing the plugin for Intellij IDEA that needs file to be saved before executing action. Action is shell command, it requires file name to be passed as the command-line parameter.

AFAIK Idea saves (synchronizes) files on frame deactivation, so if I right-click on the file, and click on my action - old version of file will be used. If I go to other window, return to Idea and click my action - current version of the file will be used.

I've read this doc about Virtual File System, and found that I can trigger file to be loaded from file system (e.g. VirtualFileManager.syncRefresh() or VirtualFileManager.asyncRefresh()). I tried this hoping it would work, but it doesn't.

Question is: how to manually (programmatically) save file?

1条回答
聊天终结者
2楼-- · 2019-06-22 08:45

While formatting my question I checked one more time, and this worked for me.

FileDocumentManager.getInstance().saveAllDocuments();

EDIT
Finally came up with the solution

FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
Document document = fileDocumentManager.getDocument(file);
if (document != null) {
    fileDocumentManager.saveDocument(document);
}
查看更多
登录 后发表回答