Search in sources :

Example 1 with ConflictsDialog

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

the class GrPullUpHandler method checkConflicts.

@Override
public boolean checkConflicts(final GrPullUpDialog dialog) {
    /*                         todo */
    List<GrMemberInfo> _infos = dialog.getSelectedMemberInfos();
    final GrMemberInfo[] infos = _infos.toArray(new GrMemberInfo[_infos.size()]);
    final PsiClass superClass = dialog.getSuperClass();
    if (!checkWritable(superClass, infos))
        return false;
    final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> ApplicationManager.getApplication().runReadAction(() -> {
        final PsiDirectory targetDirectory = superClass.getContainingFile().getContainingDirectory();
        final PsiPackage targetPackage = targetDirectory != null ? JavaDirectoryService.getInstance().getPackage(targetDirectory) : null;
        conflicts.putAllValues(GrPullUpConflictsUtil.checkConflicts(infos, mySubclass, superClass, targetPackage, targetDirectory, dialog.getContainmentVerifier()));
    }), RefactoringBundle.message("detecting.possible.conflicts"), true, myProject)) {
        return false;
    }
    if (!conflicts.isEmpty()) {
        ConflictsDialog conflictsDialog = new ConflictsDialog(myProject, conflicts);
        conflictsDialog.show();
        final boolean ok = conflictsDialog.isOK();
        if (!ok && conflictsDialog.isShowConflicts())
            dialog.close(DialogWrapper.CANCEL_EXIT_CODE);
        return ok;
    }
    return true;
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) GrMemberInfo(org.jetbrains.plugins.groovy.refactoring.classMembers.GrMemberInfo)

Example 2 with ConflictsDialog

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

the class ConvertClosureToMethodIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrField field;
    if (element.getParent() instanceof GrField) {
        field = (GrField) element.getParent();
    } else {
        final PsiReference ref = element.getReference();
        LOG.assertTrue(ref != null);
        PsiElement resolved = ref.resolve();
        if (resolved instanceof GrAccessorMethod) {
            resolved = ((GrAccessorMethod) resolved).getProperty();
        }
        LOG.assertTrue(resolved instanceof GrField);
        field = (GrField) resolved;
    }
    final HashSet<PsiReference> usages = new HashSet<>();
    usages.addAll(ReferencesSearch.search(field).findAll());
    final GrAccessorMethod[] getters = field.getGetters();
    for (GrAccessorMethod getter : getters) {
        usages.addAll(MethodReferencesSearch.search(getter).findAll());
    }
    final GrAccessorMethod setter = field.getSetter();
    if (setter != null) {
        usages.addAll(MethodReferencesSearch.search(setter).findAll());
    }
    final String fieldName = field.getName();
    LOG.assertTrue(fieldName != null);
    final Collection<PsiElement> fieldUsages = new HashSet<>();
    MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    for (PsiReference usage : usages) {
        final PsiElement psiElement = usage.getElement();
        if (PsiUtil.isMethodUsage(psiElement))
            continue;
        if (!GroovyLanguage.INSTANCE.equals(psiElement.getLanguage())) {
            conflicts.putValue(psiElement, GroovyIntentionsBundle.message("closure.is.accessed.outside.of.groovy", fieldName));
        } else {
            if (psiElement instanceof GrReferenceExpression) {
                fieldUsages.add(psiElement);
                if (PsiUtil.isAccessedForWriting((GrExpression) psiElement)) {
                    conflicts.putValue(psiElement, GroovyIntentionsBundle.message("write.access.to.closure.variable", fieldName));
                }
            } else if (psiElement instanceof GrArgumentLabel) {
                conflicts.putValue(psiElement, GroovyIntentionsBundle.message("field.is.used.in.argument.label", fieldName));
            }
        }
    }
    final PsiClass containingClass = field.getContainingClass();
    final GrExpression initializer = field.getInitializerGroovy();
    LOG.assertTrue(initializer != null);
    final PsiType type = initializer.getType();
    LOG.assertTrue(type instanceof GrClosureType);
    final GrSignature signature = ((GrClosureType) type).getSignature();
    final List<MethodSignature> signatures = GrClosureSignatureUtil.generateAllMethodSignaturesBySignature(fieldName, signature);
    for (MethodSignature s : signatures) {
        final PsiMethod method = MethodSignatureUtil.findMethodBySignature(containingClass, s, true);
        if (method != null) {
            conflicts.putValue(method, GroovyIntentionsBundle.message("method.with.signature.already.exists", GroovyPresentationUtil.getSignaturePresentation(s)));
        }
    }
    if (!conflicts.isEmpty()) {
        final ConflictsDialog conflictsDialog = new ConflictsDialog(project, conflicts, () -> execute(field, fieldUsages));
        conflictsDialog.show();
        if (conflictsDialog.getExitCode() != DialogWrapper.OK_EXIT_CODE)
            return;
    }
    execute(field, fieldUsages);
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) MethodSignature(com.intellij.psi.util.MethodSignature) GrArgumentLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) MultiMap(com.intellij.util.containers.MultiMap) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) GrSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature) HashSet(com.intellij.util.containers.HashSet) GrClosureType(org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType)

Example 3 with ConflictsDialog

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

the class ExtractClosureFromMethodProcessor method preprocessUsages.

@Override
protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) {
    UsageInfo[] usagesIn = refUsages.get();
    MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    final GrStatement[] statements = myHelper.getStatements();
    for (GrStatement statement : statements) {
        GroovyIntroduceParameterUtil.detectAccessibilityConflicts(statement, usagesIn, conflicts, false, myProject);
    }
    for (UsageInfo info : usagesIn) {
        if (info instanceof OtherLanguageUsageInfo) {
            final String lang = CommonRefactoringUtil.htmlEmphasize(info.getElement().getLanguage().getDisplayName());
            conflicts.putValue(info.getElement(), GroovyRefactoringBundle.message("cannot.process.usage.in.language.{0}", lang));
        }
    }
    if (!myMethod.hasModifierProperty(PsiModifier.PRIVATE)) {
        final AnySupers anySupers = new AnySupers();
        for (GrStatement statement : statements) {
            statement.accept(anySupers);
        }
        if (anySupers.containsSupers()) {
            for (UsageInfo usageInfo : usagesIn) {
                if (!(usageInfo.getElement() instanceof PsiMethod) && !(usageInfo instanceof InternalUsageInfo)) {
                    if (!PsiTreeUtil.isAncestor(myMethod.getContainingClass(), usageInfo.getElement(), false)) {
                        conflicts.putValue(statements[0], RefactoringBundle.message("parameter.initializer.contains.0.but.not.all.calls.to.method.are.in.its.class", CommonRefactoringUtil.htmlEmphasize(PsiKeyword.SUPER)));
                        break;
                    }
                }
            }
        }
    }
    if (!conflicts.isEmpty() && ApplicationManager.getApplication().isUnitTestMode()) {
        throw new ConflictsInTestsException(conflicts.values());
    }
    if (!conflicts.isEmpty()) {
        final ConflictsDialog conflictsDialog = prepareConflictsDialog(conflicts, usagesIn);
        if (!conflictsDialog.showAndGet()) {
            if (conflictsDialog.isShowConflicts())
                prepareSuccessful();
            return false;
        }
    }
    prepareSuccessful();
    return true;
}
Also used : GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) MultiMap(com.intellij.util.containers.MultiMap) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) UsageInfo(com.intellij.usageView.UsageInfo) NoConstructorClassUsageInfo(com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo) DefaultConstructorImplicitUsageInfo(com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo) AnySupers(org.jetbrains.plugins.groovy.refactoring.util.AnySupers)

Example 4 with ConflictsDialog

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

the class GrChangeSignatureProcessor method preprocessUsages.

@Override
protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) {
    MultiMap<PsiElement, String> conflictDescriptions = new MultiMap<>();
    collectConflictsFromExtensions(refUsages, conflictDescriptions, myChangeInfo);
    final UsageInfo[] usagesIn = refUsages.get();
    RenameUtil.addConflictDescriptions(usagesIn, conflictDescriptions);
    Set<UsageInfo> usagesSet = new HashSet<>(Arrays.asList(usagesIn));
    RenameUtil.removeConflictUsages(usagesSet);
    if (!conflictDescriptions.isEmpty()) {
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            throw new ConflictsInTestsException(conflictDescriptions.values());
        }
        ConflictsDialog dialog = prepareConflictsDialog(conflictDescriptions, usagesIn);
        if (!dialog.showAndGet()) {
            if (dialog.isShowConflicts())
                prepareSuccessful();
            return false;
        }
    }
    refUsages.set(usagesSet.toArray(new UsageInfo[usagesSet.size()]));
    prepareSuccessful();
    return true;
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) PsiElement(com.intellij.psi.PsiElement) UsageInfo(com.intellij.usageView.UsageInfo) HashSet(com.intellij.util.containers.HashSet)

Example 5 with ConflictsDialog

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

the class GenericInlineHandler method invoke.

public static boolean invoke(final PsiElement element, @Nullable Editor editor, final InlineHandler languageSpecific) {
    final PsiReference invocationReference = editor != null ? TargetElementUtil.findReference(editor) : null;
    final InlineHandler.Settings settings = languageSpecific.prepareInlineElement(element, editor, invocationReference != null);
    if (settings == null || settings == InlineHandler.Settings.CANNOT_INLINE_SETTINGS) {
        return settings != null;
    }
    final Collection<? extends PsiReference> allReferences;
    if (settings.isOnlyOneReferenceToInline()) {
        allReferences = Collections.singleton(invocationReference);
    } else {
        final Ref<Collection<? extends PsiReference>> usagesRef = new Ref<>();
        ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> usagesRef.set(ReferencesSearch.search(element).findAll()), "Find Usages", false, element.getProject());
        allReferences = usagesRef.get();
    }
    final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    final Map<Language, InlineHandler.Inliner> inliners = initializeInliners(element, settings, allReferences);
    for (PsiReference reference : allReferences) {
        collectConflicts(reference, element, inliners, conflicts);
    }
    final Project project = element.getProject();
    if (!conflicts.isEmpty()) {
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            throw new BaseRefactoringProcessor.ConflictsInTestsException(conflicts.values());
        } else {
            final ConflictsDialog conflictsDialog = new ConflictsDialog(project, conflicts);
            if (!conflictsDialog.showAndGet()) {
                return true;
            }
        }
    }
    HashSet<PsiElement> elements = new HashSet<>();
    for (PsiReference reference : allReferences) {
        PsiElement refElement = reference.getElement();
        if (refElement != null) {
            elements.add(refElement);
        }
    }
    if (!settings.isOnlyOneReferenceToInline()) {
        elements.add(element);
    }
    if (!CommonRefactoringUtil.checkReadOnlyStatusRecursively(project, elements, true)) {
        return true;
    }
    ApplicationManager.getApplication().runWriteAction(() -> {
        final String subj = element instanceof PsiNamedElement ? ((PsiNamedElement) element).getName() : "element";
        CommandProcessor.getInstance().executeCommand(project, () -> {
            final PsiReference[] references = sortDepthFirstRightLeftOrder(allReferences);
            final UsageInfo[] usages = new UsageInfo[references.length];
            for (int i = 0; i < references.length; i++) {
                usages[i] = new UsageInfo(references[i]);
            }
            for (UsageInfo usage : usages) {
                inlineReference(usage, element, inliners);
            }
            if (!settings.isOnlyOneReferenceToInline()) {
                languageSpecific.removeDefinition(element, settings);
            }
        }, RefactoringBundle.message("inline.command", StringUtil.notNullize(subj, "<nameless>")), null);
    });
    return true;
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) PsiReference(com.intellij.psi.PsiReference) MultiMap(com.intellij.util.containers.MultiMap) Project(com.intellij.openapi.project.Project) Ref(com.intellij.openapi.util.Ref) Language(com.intellij.lang.Language) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) InlineHandler(com.intellij.lang.refactoring.InlineHandler) PsiElement(com.intellij.psi.PsiElement) UsageInfo(com.intellij.usageView.UsageInfo) 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