Search in sources :

Example 1 with PsiPackageBase

use of com.intellij.psi.impl.file.PsiPackageBase in project intellij-community by JetBrains.

the class DirectoryAsPackageRenameHandlerBase method doRename.

private void doRename(PsiElement element, final Project project, PsiElement nameSuggestionContext, Editor editor) {
    final PsiDirectory psiDirectory = (PsiDirectory) element;
    final T aPackage = getPackage(psiDirectory);
    final String qualifiedName = aPackage != null ? getQualifiedName(aPackage) : "";
    if (aPackage == null || qualifiedName.length() == 0 || /*default package*/
    !isIdentifier(psiDirectory.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
            final ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
            boolean inLib = false;
            for (PsiDirectory directory : directories) {
                inLib |= !projectFileIndex.isInContent(directory.getVirtualFile());
            }
            final PsiDirectory[] projectDirectories = aPackage.getDirectories(GlobalSearchScope.projectScope(project));
            if (inLib) {
                final Module module = ModuleUtilCore.findModuleForPsiElement(psiDirectory);
                LOG.assertTrue(module != null);
                PsiDirectory[] moduleDirs = null;
                if (nameSuggestionContext instanceof PsiPackageBase) {
                    moduleDirs = aPackage.getDirectories(GlobalSearchScope.moduleScope(module));
                    if (moduleDirs.length <= 1) {
                        moduleDirs = null;
                    }
                }
                final String promptMessage = "Package \'" + aPackage.getName() + "\' contains directories in libraries which cannot be renamed. Do you want to rename " + (moduleDirs == null ? "current directory" : "current module directories");
                if (projectDirectories.length > 0) {
                    int ret = Messages.showYesNoCancelDialog(project, promptMessage + " or all directories in project?", RefactoringBundle.message("warning.title"), RefactoringBundle.message("rename.current.directory"), RefactoringBundle.message("rename.directories"), CommonBundle.getCancelButtonText(), Messages.getWarningIcon());
                    if (ret == Messages.CANCEL)
                        return;
                    renameDirs(project, nameSuggestionContext, editor, psiDirectory, aPackage, ret == Messages.YES ? (moduleDirs == null ? new PsiDirectory[] { psiDirectory } : moduleDirs) : projectDirectories);
                } else {
                    if (Messages.showOkCancelDialog(project, promptMessage + "?", RefactoringBundle.message("warning.title"), Messages.getWarningIcon()) == Messages.OK) {
                        renameDirs(project, nameSuggestionContext, editor, psiDirectory, aPackage, psiDirectory);
                    }
                }
            } else {
                final StringBuffer message = new StringBuffer();
                RenameUtil.buildPackagePrefixChangedMessage(virtualFiles, message, qualifiedName);
                buildMultipleDirectoriesInPackageMessage(message, getQualifiedName(aPackage), directories);
                message.append(RefactoringBundle.message("directories.and.all.references.to.package.will.be.renamed", psiDirectory.getVirtualFile().getPresentableUrl()));
                int ret = Messages.showYesNoCancelDialog(project, message.toString(), RefactoringBundle.message("warning.title"), RefactoringBundle.message("rename.package.button.text"), RefactoringBundle.message("rename.directory.button.text"), CommonBundle.getCancelButtonText(), Messages.getWarningIcon());
                if (ret == Messages.YES) {
                    PsiElementRenameHandler.rename(aPackage, project, nameSuggestionContext, editor);
                } else if (ret == Messages.NO) {
                    renameDirs(project, nameSuggestionContext, editor, psiDirectory, aPackage, psiDirectory);
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) PsiDirectory(com.intellij.psi.PsiDirectory) PsiPackageBase(com.intellij.psi.impl.file.PsiPackageBase) Module(com.intellij.openapi.module.Module)

Aggregations

Module (com.intellij.openapi.module.Module)1 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiDirectory (com.intellij.psi.PsiDirectory)1 PsiPackageBase (com.intellij.psi.impl.file.PsiPackageBase)1