void (String packageName, Map<String, Map<String, PsiNameValuePair[]>> annotations, VirtualFile root) { if (annotations.isEmpty()) return; String xmlContent = EntryStream.of(annotations) .mapValues(map -> EntryStream.of(map).mapKeyValue(ModCommandAwareExternalAnnotationsManager::createAnnotationTag).joining()) .mapKeyValue((externalName, content) -> "<item name='" + StringUtil.escapeXmlEntities(externalName) + "'>\n" + content.trim() + "\n</item>\n") .joining("", "<root>\n", "</root>"); WriteCommandAction.runWriteCommandAction(getProject(), () -> { XmlFile xml = ExternalAnnotationsManagerImpl.createAnnotationsXml(root, packageName, getPsiManager()); if (xml == null) throw new IllegalStateException("Unable to get XML for package " + packageName + "; root = " + root); xml.getVirtualFile().refresh(false, false); PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject()); Document document = documentManager.getDocument(xml); document.setText(xmlContent); documentManager.commitDocument(document); CodeStyleManager.getInstance(getProject()).reformat(xml); FileDocumentManager.getInstance().saveDocument(document); }); }