Search in sources :

Example 16 with ConflictsDialog

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

the class BaseRefactoringProcessor method showConflicts.

protected boolean showConflicts(@NotNull MultiMap<PsiElement, String> conflicts, @Nullable final UsageInfo[] usages) {
    if (!conflicts.isEmpty() && ApplicationManager.getApplication().isUnitTestMode()) {
        if (!ConflictsInTestsException.isTestIgnore())
            throw new ConflictsInTestsException(conflicts.values());
        return true;
    }
    if (myPrepareSuccessfulSwingThreadCallback != null && !conflicts.isEmpty()) {
        final String refactoringId = getRefactoringId();
        if (refactoringId != null) {
            RefactoringEventData conflictUsages = new RefactoringEventData();
            conflictUsages.putUserData(RefactoringEventData.CONFLICTS_KEY, conflicts.values());
            myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).conflictsDetected(refactoringId, conflictUsages);
        }
        final ConflictsDialog conflictsDialog = prepareConflictsDialog(conflicts, usages);
        if (!conflictsDialog.showAndGet()) {
            if (conflictsDialog.isShowConflicts())
                prepareSuccessful();
            return false;
        }
    }
    prepareSuccessful();
    return true;
}
Also used : RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog)

Example 17 with ConflictsDialog

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

the class ConvertInterfaceToClassIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element) throws IncorrectOperationException {
    final PsiClass anInterface = (PsiClass) element.getParent();
    final SearchScope searchScope = anInterface.getUseScope();
    final Collection<PsiClass> inheritors = ClassInheritorsSearch.search(anInterface, searchScope, false).findAll();
    final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    inheritors.forEach(aClass -> {
        final PsiReferenceList extendsList = aClass.getExtendsList();
        if (extendsList == null) {
            return;
        }
        final PsiJavaCodeReferenceElement[] referenceElements = extendsList.getReferenceElements();
        if (referenceElements.length > 0) {
            final PsiElement target = referenceElements[0].resolve();
            if (target instanceof PsiClass && !CommonClassNames.JAVA_LANG_OBJECT.equals(((PsiClass) target).getQualifiedName())) {
                conflicts.putValue(aClass, IntentionPowerPackBundle.message("0.already.extends.1.and.will.not.compile.after.converting.2.to.a.class", RefactoringUIUtil.getDescription(aClass, true), RefactoringUIUtil.getDescription(target, true), RefactoringUIUtil.getDescription(anInterface, false)));
            }
        }
    });
    final PsiFunctionalExpression functionalExpression = FunctionalExpressionSearch.search(anInterface, searchScope).findFirst();
    if (functionalExpression != null) {
        final String conflictMessage = ClassPresentationUtil.getFunctionalExpressionPresentation(functionalExpression, true) + " will not compile after converting " + RefactoringUIUtil.getDescription(anInterface, false) + " to a class";
        conflicts.putValue(functionalExpression, conflictMessage);
    }
    final boolean conflictsDialogOK;
    if (conflicts.isEmpty()) {
        conflictsDialogOK = true;
    } else {
        final Application application = ApplicationManager.getApplication();
        if (application.isUnitTestMode()) {
            throw new BaseRefactoringProcessor.ConflictsInTestsException(conflicts.values());
        }
        final ConflictsDialog conflictsDialog = new ConflictsDialog(anInterface.getProject(), conflicts, () -> convertInterfaceToClass(anInterface, inheritors));
        conflictsDialogOK = conflictsDialog.showAndGet();
    }
    if (conflictsDialogOK) {
        convertInterfaceToClass(anInterface, inheritors);
    }
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) SearchScope(com.intellij.psi.search.SearchScope) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) Application(com.intellij.openapi.application.Application)

Example 18 with ConflictsDialog

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

the class MakeClassFinalFix method doFix.

@Override
protected void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
    final PsiElement element = descriptor.getPsiElement();
    final PsiClass containingClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
    if (containingClass == null) {
        return;
    }
    final PsiModifierList modifierList = containingClass.getModifierList();
    if (modifierList == null) {
        return;
    }
    if (!isOnTheFly()) {
        if (ClassInheritorsSearch.search(containingClass).findFirst() != null) {
            return;
        }
        doMakeFinal(modifierList);
        return;
    }
    final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    final Query<PsiClass> search = ClassInheritorsSearch.search(containingClass);
    search.forEach(aClass -> {
        conflicts.putValue(containingClass, InspectionGadgetsBundle.message("0.will.no.longer.be.overridable.by.1", RefactoringUIUtil.getDescription(containingClass, false), RefactoringUIUtil.getDescription(aClass, false)));
        return true;
    });
    final boolean conflictsDialogOK;
    if (!conflicts.isEmpty()) {
        ConflictsDialog conflictsDialog = new ConflictsDialog(element.getProject(), conflicts, () -> doMakeFinal(modifierList));
        conflictsDialogOK = conflictsDialog.showAndGet();
    } else {
        conflictsDialogOK = true;
    }
    if (conflictsDialogOK) {
        doMakeFinal(modifierList);
    }
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) PsiClass(com.intellij.psi.PsiClass) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) PsiElement(com.intellij.psi.PsiElement) PsiModifierList(com.intellij.psi.PsiModifierList)

Example 19 with ConflictsDialog

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

the class RenameProcessor method preprocessUsages.

@Override
public boolean preprocessUsages(@NotNull final Ref<UsageInfo[]> refUsages) {
    UsageInfo[] usagesIn = refUsages.get();
    MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    RenameUtil.addConflictDescriptions(usagesIn, conflicts);
    RenamePsiElementProcessor.forElement(myPrimaryElement).findExistingNameConflicts(myPrimaryElement, myNewName, conflicts, myAllRenames);
    if (!conflicts.isEmpty()) {
        final RefactoringEventData conflictData = new RefactoringEventData();
        conflictData.putUserData(RefactoringEventData.CONFLICTS_KEY, conflicts.values());
        myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).conflictsDetected("refactoring.rename", conflictData);
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            throw new ConflictsInTestsException(conflicts.values());
        }
        ConflictsDialog conflictsDialog = prepareConflictsDialog(conflicts, refUsages.get());
        if (!conflictsDialog.showAndGet()) {
            if (conflictsDialog.isShowConflicts())
                prepareSuccessful();
            return false;
        }
    }
    final List<UsageInfo> variableUsages = new ArrayList<>();
    if (!myRenamers.isEmpty()) {
        if (!findRenamedVariables(variableUsages))
            return false;
        final LinkedHashMap<PsiElement, String> renames = new LinkedHashMap<>();
        for (final AutomaticRenamer renamer : myRenamers) {
            final List<? extends PsiNamedElement> variables = renamer.getElements();
            for (final PsiNamedElement variable : variables) {
                final String newName = renamer.getNewName(variable);
                if (newName != null) {
                    addElement(variable, newName);
                    prepareRenaming(variable, newName, renames);
                }
            }
        }
        if (!renames.isEmpty()) {
            for (PsiElement element : renames.keySet()) {
                assertNonCompileElement(element);
            }
            myAllRenames.putAll(renames);
            final Runnable runnable = () -> {
                for (final Map.Entry<PsiElement, String> entry : renames.entrySet()) {
                    final UsageInfo[] usages = ApplicationManager.getApplication().runReadAction(new Computable<UsageInfo[]>() {

                        @Override
                        public UsageInfo[] compute() {
                            return RenameUtil.findUsages(entry.getKey(), entry.getValue(), mySearchInComments, mySearchTextOccurrences, myAllRenames);
                        }
                    });
                    Collections.addAll(variableUsages, usages);
                }
            };
            if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(runnable, RefactoringBundle.message("searching.for.variables"), true, myProject)) {
                return false;
            }
        }
    }
    final int[] choice = myAllRenames.size() > 1 ? new int[] { -1 } : null;
    try {
        for (Iterator<Map.Entry<PsiElement, String>> iterator = myAllRenames.entrySet().iterator(); iterator.hasNext(); ) {
            Map.Entry<PsiElement, String> entry = iterator.next();
            if (entry.getKey() instanceof PsiFile) {
                final PsiFile file = (PsiFile) entry.getKey();
                final PsiDirectory containingDirectory = file.getContainingDirectory();
                if (CopyFilesOrDirectoriesHandler.checkFileExist(containingDirectory, choice, file, entry.getValue(), "Rename")) {
                    iterator.remove();
                    continue;
                }
            }
            RenameUtil.checkRename(entry.getKey(), entry.getValue());
        }
    } catch (IncorrectOperationException e) {
        CommonRefactoringUtil.showErrorMessage(RefactoringBundle.message("rename.title"), e.getMessage(), getHelpID(), myProject);
        return false;
    }
    final Set<UsageInfo> usagesSet = ContainerUtil.newLinkedHashSet(usagesIn);
    usagesSet.addAll(variableUsages);
    final List<UnresolvableCollisionUsageInfo> conflictUsages = RenameUtil.removeConflictUsages(usagesSet);
    if (conflictUsages != null) {
        mySkippedUsages.addAll(conflictUsages);
    }
    refUsages.set(usagesSet.toArray(new UsageInfo[usagesSet.size()]));
    prepareSuccessful();
    return PsiElementRenameHandler.canRename(myProject, null, myPrimaryElement);
}
Also used : AutomaticRenamer(com.intellij.refactoring.rename.naming.AutomaticRenamer) MultiMap(com.intellij.util.containers.MultiMap) RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) UsageInfo(com.intellij.usageView.UsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) RelatedUsageInfo(com.intellij.refactoring.util.RelatedUsageInfo) Computable(com.intellij.openapi.util.Computable) IncorrectOperationException(com.intellij.util.IncorrectOperationException) MultiMap(com.intellij.util.containers.MultiMap)

Example 20 with ConflictsDialog

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

the class ConvertMethodToClosureIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    final GrMethod method;
    if (element.getParent() instanceof GrMethod) {
        method = (GrMethod) element.getParent();
    } else {
        final PsiReference ref = element.getReference();
        LOG.assertTrue(ref != null);
        final PsiElement resolved = ref.resolve();
        LOG.assertTrue(resolved instanceof GrMethod);
        method = (GrMethod) resolved;
    }
    final PsiClass containingClass = method.getContainingClass();
    final String methodName = method.getName();
    final PsiField field = containingClass.findFieldByName(methodName, true);
    if (field != null) {
        conflicts.putValue(field, GroovyIntentionsBundle.message("field.already.exists", methodName));
    }
    final Collection<PsiReference> references = MethodReferencesSearch.search(method).findAll();
    final Collection<GrReferenceExpression> usagesToConvert = new HashSet<>(references.size());
    for (PsiReference ref : references) {
        final PsiElement psiElement = ref.getElement();
        if (!GroovyLanguage.INSTANCE.equals(psiElement.getLanguage())) {
            conflicts.putValue(psiElement, GroovyIntentionsBundle.message("method.is.used.outside.of.groovy"));
        } else if (!PsiUtil.isMethodUsage(psiElement)) {
            if (psiElement instanceof GrReferenceExpression) {
                if (((GrReferenceExpression) psiElement).hasMemberPointer()) {
                    usagesToConvert.add((GrReferenceExpression) psiElement);
                }
            }
        }
    }
    if (!conflicts.isEmpty()) {
        ConflictsDialog conflictsDialog = new ConflictsDialog(project, conflicts, () -> execute(method, usagesToConvert));
        conflictsDialog.show();
        if (conflictsDialog.getExitCode() != DialogWrapper.OK_EXIT_CODE)
            return;
    }
    execute(method, usagesToConvert);
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) PsiClass(com.intellij.psi.PsiClass) PsiReference(com.intellij.psi.PsiReference) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) MultiMap(com.intellij.util.containers.MultiMap) PsiField(com.intellij.psi.PsiField) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) PsiElement(com.intellij.psi.PsiElement) HashSet(com.intellij.util.containers.HashSet)

Aggregations

ConflictsDialog (com.intellij.refactoring.ui.ConflictsDialog)21 MultiMap (com.intellij.util.containers.MultiMap)15 UsageInfo (com.intellij.usageView.UsageInfo)7 HashSet (com.intellij.util.containers.HashSet)5 PsiElement (com.intellij.psi.PsiElement)4 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)3 Project (com.intellij.openapi.project.Project)2 PsiClass (com.intellij.psi.PsiClass)2 PsiReference (com.intellij.psi.PsiReference)2 RefactoringEventData (com.intellij.refactoring.listeners.RefactoringEventData)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 NotNull (org.jetbrains.annotations.NotNull)2 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)2 LocalHistoryAction (com.intellij.history.LocalHistoryAction)1 DirectoryChooser (com.intellij.ide.util.DirectoryChooser)1 Language (com.intellij.lang.Language)1 InlineHandler (com.intellij.lang.refactoring.InlineHandler)1 Application (com.intellij.openapi.application.Application)1 DialogWrapper (com.intellij.openapi.ui.DialogWrapper)1 Computable (com.intellij.openapi.util.Computable)1