@Override
  protected void doRename(PsiElement element, Project project, PsiElement nameSuggestionContext, Editor editor) {
    final PsiDirectory selected = (PsiDirectory)element;
    final T aPackage = getPackage(selected);
    final String qualifiedName = aPackage != null ? getQualifiedName(aPackage) : "";
    if (aPackage == null || qualifiedName.isEmpty()/*default package*/ || !isIdentifier(selected.getName(), project)) {
      PsiElementRenameHandler.rename(element, project, nameSuggestionContext, editor);
    }
    else {
      PsiDirectory[] directories = aPackage.getDirectories();
      final VirtualFile[] virtualFiles = occursInPackagePrefixes(aPackage);
      if (virtualFiles.length == 0 && directories.length == 1) {
        PsiElementRenameHandler.rename(aPackage, project, nameSuggestionContext, editor);
      }
      else { // the directory corresponds to a package that has multiple associated directories
        PsiDirectory[] projectDirectories = aPackage.getDirectories(GlobalSearchScope.projectScope(project));

        if (virtualFiles.length == 0 && projectDirectories.length == 1) { //ignore library packages
          renameDirs(project, nameSuggestionContext, editor, selected, aPackage, selected);
          return;
        }

        final @Nls StringBuffer message = new StringBuffer();
        RenameUtil.buildPackagePrefixChangedMessage(virtualFiles, message, qualifiedName);
        Module module = Objects.requireNonNull(ModuleUtilCore.findModuleForFile(selected.getVirtualFile(), project));
        buildMultipleDirectoriesInPackageMessage(message, getQualifiedName(aPackage), projectDirectories, selected);
        PsiDirectory[] moduleDirectories = aPackage.getDirectories(GlobalSearchScope.moduleScope(module));
        message.append(RefactoringBundle.message("directories.and.all.references.to.package.will.be.renamed"));
        List<String> options = new ArrayList<>();
        options.add(RefactoringBundle.message("rename.package.button.text"));
        if (projectDirectories.length > moduleDirectories.length) {
          if (moduleDirectories.length > 1) {
            options.add(RefactoringBundle.message("rename.source.root.button.text"));
          }
          options.add(RefactoringBundle.message("rename.directory.button.text"));
        }
        else {
          options.add(RefactoringBundle.message("rename.directory.button.text"));
        }
        options.add(CommonBundle.getCancelButtonText());

        int ret = Messages.showDialog(project, message.toString(), RefactoringBundle.message("dialog.title.rename.package.directories"),
                                      ArrayUtil.toStringArray(options), 0, Messages.getQuestionIcon());
        if (ret == 0) {
          if (directories.length > projectDirectories.length) {
            renameDirs(project, nameSuggestionContext, editor, selected, aPackage, projectDirectories);
          }
          else {
            PsiElementRenameHandler.rename(aPackage, project, nameSuggestionContext, editor);
          }
        }
        else if (ret == 1) {
          PsiDirectory[] dirsToRename = projectDirectories.length > moduleDirectories.length ? moduleDirectories : new PsiDirectory[]{selected};
          renameDirs(project, nameSuggestionContext, editor, selected, aPackage, dirsToRename);
        }
        else if (ret == 2 && options.size() > 3) {
          renameDirs(project, nameSuggestionContext, editor, selected, aPackage, selected);
        }
      }
    }
  }
@Override
  protected void doRename(PsiElement element, Project project, PsiElement nameSuggestionContext, Editor editor)