Search in sources :

Example 6 with MoveDestination

use of com.intellij.refactoring.MoveDestination in project intellij-community by JetBrains.

the class JavaIntroduceParameterObjectDelegate method collectConflicts.

@Override
public void collectConflicts(MultiMap<PsiElement, String> conflicts, UsageInfo[] infos, PsiMethod method, JavaIntroduceParameterObjectClassDescriptor classDescriptor) {
    final MoveDestination moveDestination = classDescriptor.getMoveDestination();
    if (moveDestination != null) {
        if (!moveDestination.isTargetAccessible(method.getProject(), method.getContainingFile().getVirtualFile())) {
            conflicts.putValue(method, "Created class won't be accessible");
        }
        if (!classDescriptor.isCreateInnerClass() && !classDescriptor.isUseExistingClass()) {
            final PsiFile containingFile = method.getContainingFile();
            final PsiDirectory containingDirectory = containingFile.getContainingDirectory();
            PsiDirectory directory = moveDestination.getTargetDirectory(containingDirectory);
            if (directory != null) {
                PsiFile file = directory.findFile(classDescriptor.getClassName() + ".java");
                if (file != null) {
                    VirtualFile virtualFile = PsiUtilCore.getVirtualFile(file);
                    if (virtualFile != null) {
                        conflicts.putValue(method, "File already exits: " + virtualFile.getPresentableUrl());
                    }
                }
            }
        }
    }
    if (moveDestination != null) {
        boolean constructorMiss = false;
        for (UsageInfo info : infos) {
            if (info instanceof IntroduceParameterObjectProcessor.ChangeSignatureUsageWrapper) {
                final UsageInfo usageInfo = ((IntroduceParameterObjectProcessor.ChangeSignatureUsageWrapper) info).getInfo();
                if (usageInfo instanceof OverriderMethodUsageInfo) {
                    final PsiElement overridingMethod = ((OverriderMethodUsageInfo) usageInfo).getOverridingMethod();
                    if (!moveDestination.isTargetAccessible(overridingMethod.getProject(), overridingMethod.getContainingFile().getVirtualFile())) {
                        conflicts.putValue(overridingMethod, "Created class won't be accessible");
                    }
                }
                if (!constructorMiss && classDescriptor.isUseExistingClass() && usageInfo instanceof MethodCallUsageInfo && classDescriptor.getExistingClassCompatibleConstructor() == null) {
                    conflicts.putValue(classDescriptor.getExistingClass(), "Existing class misses compatible constructor");
                    constructorMiss = true;
                }
            }
        }
    }
    if (classDescriptor.isUseExistingClass()) {
        for (ParameterInfoImpl info : classDescriptor.getParamsToMerge()) {
            Object existingClassBean = classDescriptor.getBean(info);
            if (existingClassBean == null) {
                conflicts.putValue(classDescriptor.getExistingClass(), "No field associated with " + info.getName() + " found");
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MoveDestination(com.intellij.refactoring.MoveDestination) UsageInfo(com.intellij.usageView.UsageInfo) FixableUsageInfo(com.intellij.refactoring.util.FixableUsageInfo)

Example 7 with MoveDestination

use of com.intellij.refactoring.MoveDestination in project kotlin by JetBrains.

the class MoveKotlinTopLevelDeclarationsDialog method selectMoveTarget.

@Nullable
private KotlinMoveTarget selectMoveTarget() {
    String message = verifyBeforeRun();
    if (message != null) {
        setErrorText(message);
        return null;
    }
    setErrorText(null);
    List<KtFile> sourceFiles = getSourceFiles(getSelectedElementsToMove());
    PsiDirectory sourceDirectory = getSourceDirectory(sourceFiles);
    if (isMoveToPackage()) {
        Pair<VirtualFile, ? extends MoveDestination> targetDirWithMoveDestination = selectPackageBasedTargetDirAndDestination(true);
        if (targetDirWithMoveDestination == null)
            return null;
        VirtualFile targetDir = targetDirWithMoveDestination.getFirst();
        final MoveDestination moveDestination = targetDirWithMoveDestination.getSecond();
        final String targetFileName = sourceFiles.size() > 1 ? null : tfFileNameInPackage.getText();
        if (targetFileName != null && !checkTargetFileName(targetFileName))
            return null;
        PsiDirectory targetDirectory = moveDestination.getTargetIfExists(sourceDirectory);
        List<PsiFile> filesExistingInTargetDir = getFilesExistingInTargetDir(sourceFiles, targetFileName, targetDirectory);
        if (!filesExistingInTargetDir.isEmpty()) {
            if (!CollectionsKt.intersect(sourceFiles, filesExistingInTargetDir).isEmpty()) {
                setErrorText("Can't move to the original file(s)");
                return null;
            }
            if (filesExistingInTargetDir.size() > 1) {
                String filePathsToReport = StringUtil.join(filesExistingInTargetDir, new Function<PsiFile, String>() {

                    @Override
                    public String fun(PsiFile file) {
                        return file.getVirtualFile().getPath();
                    }
                }, "\n");
                Messages.showErrorDialog(myProject, "Cannot perform refactoring since the following files already exist:\n\n" + filePathsToReport, RefactoringBundle.message("move.title"));
                return null;
            }
            String question = String.format("File '%s' already exists. Do you want to move selected declarations to this file?", filesExistingInTargetDir.get(0).getVirtualFile().getPath());
            int ret = Messages.showYesNoDialog(myProject, question, RefactoringBundle.message("move.title"), Messages.getQuestionIcon());
            if (ret != Messages.YES)
                return null;
        }
        // All source files must be in the same directory
        return new KotlinMoveTargetForDeferredFile(new FqName(getTargetPackage()), moveDestination.getTargetIfExists(sourceFiles.get(0)), targetDir, new Function1<KtFile, KtFile>() {

            @Override
            public KtFile invoke(@NotNull KtFile originalFile) {
                return KotlinRefactoringUtilKt.getOrCreateKotlinFile(targetFileName != null ? targetFileName : originalFile.getName(), moveDestination.getTargetDirectory(originalFile));
            }
        });
    }
    final File targetFile = new File(getTargetFilePath());
    if (!checkTargetFileName(targetFile.getName()))
        return null;
    KtFile jetFile = (KtFile) KotlinRefactoringUtilKt.toPsiFile(targetFile, myProject);
    if (jetFile != null) {
        if (sourceFiles.size() == 1 && sourceFiles.contains(jetFile)) {
            setErrorText("Can't move to the original file");
            return null;
        }
        return new KotlinMoveTargetForExistingElement(jetFile);
    }
    File targetDir = targetFile.getParentFile();
    final PsiDirectory psiDirectory = targetDir != null ? KotlinRefactoringUtilKt.toPsiDirectory(targetDir, myProject) : null;
    if (psiDirectory == null) {
        setErrorText("No directory found for file: " + targetFile.getPath());
        return null;
    }
    Set<FqName> sourcePackageFqNames = CollectionsKt.mapTo(sourceFiles, new LinkedHashSet<FqName>(), new Function1<KtFile, FqName>() {

        @Override
        public FqName invoke(KtFile file) {
            return file.getPackageFqName();
        }
    });
    FqName targetPackageFqName = CollectionsKt.singleOrNull(sourcePackageFqNames);
    if (targetPackageFqName == null) {
        PsiPackage psiPackage = JavaDirectoryService.getInstance().getPackage(psiDirectory);
        if (psiPackage == null) {
            setErrorText("Could not find package corresponding to " + targetDir.getPath());
            return null;
        }
        targetPackageFqName = new FqName(psiPackage.getQualifiedName());
    }
    final String finalTargetPackageFqName = targetPackageFqName.asString();
    return new KotlinMoveTargetForDeferredFile(targetPackageFqName, psiDirectory, null, new Function1<KtFile, KtFile>() {

        @Override
        public KtFile invoke(@NotNull KtFile originalFile) {
            return KotlinRefactoringUtilKt.getOrCreateKotlinFile(targetFile.getName(), psiDirectory, finalTargetPackageFqName);
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FqName(org.jetbrains.kotlin.name.FqName) MoveDestination(com.intellij.refactoring.MoveDestination) AutocreatingSingleSourceRootMoveDestination(com.intellij.refactoring.move.moveClassesOrPackages.AutocreatingSingleSourceRootMoveDestination) MultipleRootsMoveDestination(com.intellij.refactoring.move.moveClassesOrPackages.MultipleRootsMoveDestination) KtFile(org.jetbrains.kotlin.psi.KtFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) KtFile(org.jetbrains.kotlin.psi.KtFile) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with MoveDestination

use of com.intellij.refactoring.MoveDestination in project intellij-plugins by JetBrains.

the class ActivatorRenameTest method renamePackage.

private void renamePackage() {
    VirtualFile sourceRoot = ModuleRootManager.getInstance(myModule).getSourceRoots()[0];
    JavaRefactoringFactory factory = JavaRefactoringFactory.getInstance(getProject());
    MoveDestination moveDestination = factory.createSourceRootMoveDestination("tx", sourceRoot);
    factory.createMoveClassesOrPackages(new PsiElement[] { myActivator }, moveDestination).run();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) JavaRefactoringFactory(com.intellij.refactoring.JavaRefactoringFactory) MoveDestination(com.intellij.refactoring.MoveDestination) PsiElement(com.intellij.psi.PsiElement)

Aggregations

MoveDestination (com.intellij.refactoring.MoveDestination)8 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4 MultipleRootsMoveDestination (com.intellij.refactoring.move.moveClassesOrPackages.MultipleRootsMoveDestination)3 PackageWrapper (com.intellij.refactoring.PackageWrapper)2 AutocreatingSingleSourceRootMoveDestination (com.intellij.refactoring.move.moveClassesOrPackages.AutocreatingSingleSourceRootMoveDestination)2 KtFile (org.jetbrains.kotlin.psi.KtFile)2 Module (com.intellij.openapi.module.Module)1 Project (com.intellij.openapi.project.Project)1 Computable (com.intellij.openapi.util.Computable)1 PsiDirectory (com.intellij.psi.PsiDirectory)1 PsiElement (com.intellij.psi.PsiElement)1 CodeStyleManager (com.intellij.psi.codeStyle.CodeStyleManager)1 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)1 JavaRefactoringFactory (com.intellij.refactoring.JavaRefactoringFactory)1 JavaRefactoringSettings (com.intellij.refactoring.JavaRefactoringSettings)1 ParameterInfoImpl (com.intellij.refactoring.changeSignature.ParameterInfoImpl)1 FixableUsageInfo (com.intellij.refactoring.util.FixableUsageInfo)1 UsageInfo (com.intellij.usageView.UsageInfo)1 File (java.io.File)1