void (@NotNull String root, @NotNull Disposable parent) { Map<VirtualFile, byte[]> oldContent = new HashMap<>(); VirtualFileManager.getInstance().addVirtualFileListener(new VirtualFileListener() { @Override public void beforeContentsChange(@NotNull VirtualFileEvent event) { VirtualFile file = event.getFile(); if (file.getPath().startsWith(root)) { try { byte[] old = file.contentsToByteArray(); oldContent.put(file, old); } catch (IOException e) { throw new RuntimeException(e); } } } }, parent); Disposer.register(parent, () -> { for (Map.Entry<VirtualFile, byte[]> entry : oldContent.entrySet()) { VirtualFile file = entry.getKey(); byte[] content = entry.getValue(); try { LOG.warn("Restoring "+file+" ..."); WriteAction.run(() -> file.setBinaryContent(content)); } catch (IOException e) { throw new RuntimeException(e); } } }); }