@Nullable
  private XmlFile createAnnotationsXml(
      @NotNull VirtualFile root, @NonNls @NotNull String packageName) {
    final String[] dirs = packageName.split("[\\.]");
    for (String dir : dirs) {
      if (dir.isEmpty()) break;
      VirtualFile subdir = root.findChild(dir);
      if (subdir == null) {
        try {
          subdir = root.createChildDirectory(null, dir);
        } catch (IOException e) {
          LOG.error(e);
        }
      }
      root = subdir;
    }
    final PsiDirectory directory = myPsiManager.findDirectory(root);
    if (directory == null) return null;

    final PsiFile psiFile = directory.findFile(ANNOTATIONS_XML);
    if (psiFile instanceof XmlFile) {
      return (XmlFile) psiFile;
    }

    try {
      final PsiFileFactory factory = PsiFileFactory.getInstance(myPsiManager.getProject());
      return (XmlFile)
          directory.add(
              factory.createFileFromText(ANNOTATIONS_XML, XmlFileType.INSTANCE, "<root></root>"));
    } catch (IncorrectOperationException e) {
      LOG.error(e);
    }
    return null;
  }
 public final <T extends DomElement> T createMockElement(
     final Class<T> aClass, final Module module, final boolean physical) {
   final XmlFile file =
       (XmlFile)
           PsiFileFactory.getInstance(myProject)
               .createFileFromText("a.xml", StdFileTypes.XML, "", (long) 0, physical);
   file.putUserData(MOCK_ELEMENT_MODULE, module);
   file.putUserData(MOCK, new Object());
   return getFileElement(
           file, aClass, "I_sincerely_hope_that_nobody_will_have_such_a_root_tag_name")
       .getRootElement();
 }
 @Nullable
 private PsiFile createFileCopyWithNewName(VirtualFile vFile, String name) {
   // TODO[ik] remove this. Event handling and generation must be in view providers mechanism since
   // we
   // need to track changes in _all_ psi views (e.g. namespace changes in XML)
   final FileTypeManager instance = FileTypeManager.getInstance();
   if (instance.isFileIgnored(name)) return null;
   final FileType fileTypeByFileName = instance.getFileTypeByFileName(name);
   final Document document = FileDocumentManager.getInstance().getDocument(vFile);
   return PsiFileFactory.getInstance(myManager.getProject())
       .createFileFromText(
           name,
           fileTypeByFileName,
           document != null ? document.getCharsSequence() : "",
           vFile.getModificationStamp(),
           true,
           false);
 }
  private static List<PsiElement> getTopLevelRegExpChars(String regExpText, Project project) {
    @SuppressWarnings("deprecation")
    PsiFile file = PsiFileFactory.getInstance(project).createFileFromText("A.regexp", regExpText);
    List<PsiElement> result = null;
    final PsiElement[] children = file.getChildren();

    for (PsiElement child : children) {
      PsiElement[] grandChildren = child.getChildren();
      if (grandChildren.length != 1)
        return Collections
            .emptyList(); // a | b, more than one branch, can not predict in current way

      for (PsiElement grandGrandChild : grandChildren[0].getChildren()) {
        if (result == null) result = new ArrayList<>();
        result.add(grandGrandChild);
      }
    }
    return result != null ? result : Collections.<PsiElement>emptyList();
  }