@Override
  public PsiElement add(@NotNull PsiElement element) throws IncorrectOperationException {
    checkAdd(element);

    if (element instanceof PsiFile) {
      PsiFile originalFile = (PsiFile)element;

      try {
        VirtualFile newVFile;
        if (originalFile instanceof PsiFileImpl) {
          newVFile = myFile.createChildData(myManager, originalFile.getName());
          String text = originalFile.getText();
          PsiFile psiFile = getManager().findFile(newVFile);
          Document document = psiFile == null ? null : psiFile.getViewProvider().getDocument();
          FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
          if (document != null) {
            document.setText(text);
            if (psiFile.isPhysical()) {
              fileDocumentManager.saveDocument(document);
            }
            PsiDocumentManager.getInstance(getProject()).commitDocument(document);
          }
          else {
            String lineSeparator = fileDocumentManager.getLineSeparator(newVFile, getProject());
            if (!lineSeparator.equals("\n")) {
              text = StringUtil.convertLineSeparators(text, lineSeparator);
            }

            LoadTextUtil.write(getProject(), newVFile, myManager, text, -1);
          }
        }
        else {
          byte[] storedContents = ((PsiBinaryFileImpl)originalFile).getStoredContents();
          if (storedContents != null) {
            newVFile = myFile.createChildData(myManager, originalFile.getName());
            newVFile.setBinaryContent(storedContents);
          }
          else {
            newVFile = VfsUtilCore.copyFile(null, originalFile.getVirtualFile(), myFile);
          }
        }

        PsiFile newFile = myManager.findFile(newVFile);
        if (newFile == null) throw new IncorrectOperationException("Could not find file " + newVFile);
        UpdateAddedFileProcessor.updateAddedFiles(Collections.singletonList(newFile), Collections.singletonList(originalFile));
        return newFile;
      }
      catch (IOException e) {
        throw new IncorrectOperationException(e);
      }
    }

    throw new IncorrectOperationException(element + " (" + element.getClass() + ")");
  }
@Override
  public PsiElement add(@NotNull PsiElement element)