将虚拟文件同步到Intellij IDEA插件中的物理文件

Dan*_*any 5 intellij-idea intellij-plugin

我正在实现Intellij IDEA的插件,需要在执行操作之前保存文件.Action是shell命令,它要求将文件名作为命令行参数传递.

AFAIK Idea在帧停用时保存(同步)文件,因此如果我右键单击该文件,然后单击我的操作 - 将使用旧版本的文件.如果我转到其他窗口,返回到Idea并单击我的操作 - 将使用该文件的当前版本.

我已经阅读了这篇关于虚拟文件系统的文档,发现我可以触发从文件系统加载的文件(例如VirtualFileManager.syncRefresh()或者VirtualFileManager.asyncRefresh()).我试过这个希望它会起作用,但事实并非如此.

问题是:如何手动(以编程方式)保存文件?

Dan*_*any 9

在格式化我的问题时,我再次检查了一次,这对我有用.

FileDocumentManager.getInstance().saveAllDocuments();
Run Code Online (Sandbox Code Playgroud)

编辑
最后提出了解决方案

FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
Document document = fileDocumentManager.getDocument(file);
if (document != null) {
    fileDocumentManager.saveDocument(document);
}
Run Code Online (Sandbox Code Playgroud)