private static void copyToDirectory(List<? extends PsiFileSystemItem> elementsToCopy,
                                      @Nullable String newName,
                                      @NotNull PsiDirectory targetDirectory,
                                      int @Nullable [] choice,
                                      @Nullable @NlsContexts.Command String title,
                                      @NotNull List<? super PsiFile> added,
                                      @NotNull List<? super PsiFile> originals) throws IncorrectOperationException, IOException {
    MultiMap<PsiDirectory, PsiFile> existingFiles = new MultiMap<>();
    ApplicationEx app = ApplicationManagerEx.getApplicationEx();
    if (Registry.is("run.refactorings.under.progress")) {
      AtomicReference<Throwable> thrown = new AtomicReference<>();
      Consumer<ProgressIndicator> copyAction = pi -> {
        Project project = targetDirectory.getProject();
        int fileCount = ActionUtil.underModalProgress(
          project,
          IdeBundle.message("progress.counting.files"),
          () -> countFiles(elementsToCopy)
        );
        pi.setIndeterminate(fileCount <= 1); // don't show progression when copy-pasting a single file
        try {
          for (PsiFileSystemItem elementToCopy : elementsToCopy) {
            copyToDirectoryUnderProgress(elementToCopy, newName, targetDirectory, added, originals, existingFiles,
                                         Ref.create(0), fileCount, pi);
          }
        }
        catch (Throwable e) {
          thrown.set(e);
        }
      };
      CommandProcessor.getInstance().executeCommand(targetDirectory.getProject(),
                                                    () -> app.runWriteActionWithCancellableProgressInDispatchThread(
                                                      ObjectUtils.notNull(title, RefactoringBundle.message("command.name.copy")),
                                                      targetDirectory.getProject(), null, copyAction), title, null);
      Throwable throwable = thrown.get();
      if (throwable instanceof ProcessCanceledException) {
        //process was canceled, don't proceed with existing files
        return;
      }
      rethrow(throwable);
    }
    else {
      WriteCommandAction.writeCommandAction(targetDirectory.getProject())
        .withName(title)
        .run(() -> {
          for (PsiFileSystemItem elementToCopy : elementsToCopy) {
            copyToDirectoryUnderProgress(elementToCopy, newName, targetDirectory, added, originals, existingFiles, Ref.create(0), -1, null);
          }
        });
    }

    handleExistingFiles(newName, targetDirectory, choice, title, existingFiles, added);
  }
private static void copyToDirectory(List<? extends PsiFileSystemItem> elementsToCopy,
                                      @Nullable String newName,
                                      @NotNull PsiDirectory targetDirectory,
                                      int @Nullable [] choice,
                                      @Nullable @NlsContexts.Command String title,
                                      @NotNull List<? super PsiFile> added,
                                      @NotNull List<? super PsiFile> originals)