VirtualFile (VirtualFile root, String relativePath, byte @Nullable [] data, boolean dir) { try { return WriteAction.computeAndWait(() -> { VirtualFile parent = root; for (String name : StringUtil.tokenize(PathUtil.getParentPath(relativePath), "/")) { VirtualFile child = parent.findChild(name); if (child == null || !child.isValid()) { child = parent.createChildDirectory(VfsTestUtil.class, name); } parent = child; } parent.getChildren(); // to ensure that fileCreated event is fired String name = PathUtil.getFileName(relativePath); VirtualFile file; if (dir) { file = parent.createChildDirectory(VfsTestUtil.class, name); } else { FileDocumentManager manager = FileDocumentManager.getInstance(); file = parent.findChild(name); if (file == null) { file = parent.createChildData(VfsTestUtil.class, name); } else { Document document = manager.getCachedDocument(file); if (document != null) manager.saveDocument(document); // save changes to prevent possible conflicts } if (data != null) { file.setBinaryContent(data); } manager.reloadFiles(file); // update the document now, to prevent MemoryDiskConflictResolver from kicking in later } return file; }); } catch (IOException e) { throw new RuntimeException(e); } }