XmlTag (@NotNull XmlTag rootTag, @Nullable XmlTag anchor, @NotNull String ownerName, @NotNull ExternalAnnotation annotation) { XmlElementFactory elementFactory = XmlElementFactory.getInstance(myPsiManager.getProject()); XmlTag newItemTag = elementFactory.createTagFromText(createItemTag(ownerName, annotation)); PsiElement addedElement; if (anchor != null) { addedElement = rootTag.addAfter(newItemTag, anchor); } else { addedElement = rootTag.addSubTag(newItemTag, true); } if (!(addedElement instanceof XmlTag)) { throw new IncorrectOperationException("Failed to add annotation " + annotation + " after " + anchor); } return (XmlTag)addedElement; }