Search in sources :

Example 1 with InternalUsageInfo

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

the class GrIntroduceClosureParameterProcessor method processInternalUsages.

private static void processInternalUsages(UsageInfo[] usages, GrIntroduceParameterSettings settings) {
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(settings.getProject());
    // Replacing expression occurrences
    for (UsageInfo usage : usages) {
        if (usage instanceof ChangedMethodCallInfo) {
            PsiElement element = usage.getElement();
            processChangedMethodCall(element, settings);
        } else if (usage instanceof InternalUsageInfo) {
            PsiElement element = usage.getElement();
            if (element == null)
                continue;
            GrExpression newExpr = factory.createExpressionFromText(settings.getName());
            if (element instanceof GrExpression) {
                ((GrExpression) element).replaceWithExpression(newExpr, true);
            } else {
                element.replace(newExpr);
            }
        }
    }
    final StringPartInfo info = settings.getStringPartInfo();
    if (info != null) {
        final GrExpression expr = info.replaceLiteralWithConcatenation(settings.getName());
        final Editor editor = PsiUtilBase.findEditor(expr);
        if (editor != null) {
            editor.getSelectionModel().removeSelection();
            editor.getCaretModel().moveToOffset(expr.getTextRange().getEndOffset());
        }
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) ChangedMethodCallInfo(com.intellij.refactoring.introduceParameter.ChangedMethodCallInfo) Editor(com.intellij.openapi.editor.Editor) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo) ExternalUsageInfo(com.intellij.refactoring.introduceParameter.ExternalUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) InternalUsageInfo(com.intellij.refactoring.introduceParameter.InternalUsageInfo) InternalUsageInfo(com.intellij.refactoring.introduceParameter.InternalUsageInfo)

Example 2 with InternalUsageInfo

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

the class GrIntroduceClosureParameterProcessor method preprocessUsages.

@Override
protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) {
    UsageInfo[] usagesIn = refUsages.get();
    MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    if (!mySettings.generateDelegate()) {
        detectAccessibilityConflicts(usagesIn, conflicts);
    }
    final GrExpression expression = mySettings.getExpression();
    if (expression != null && toSearchFor instanceof PsiMember) {
        final AnySupers anySupers = new AnySupers();
        expression.accept(anySupers);
        if (anySupers.containsSupers()) {
            final PsiElement containingClass = PsiUtil.getFileOrClassContext(toReplaceIn);
            for (UsageInfo usageInfo : usagesIn) {
                if (!(usageInfo.getElement() instanceof PsiMethod) && !(usageInfo instanceof InternalUsageInfo)) {
                    if (!PsiTreeUtil.isAncestor(containingClass, usageInfo.getElement(), false)) {
                        conflicts.putValue(expression, RefactoringBundle.message("parameter.initializer.contains.0.but.not.all.calls.to.method.are.in.its.class", CommonRefactoringUtil.htmlEmphasize(PsiKeyword.SUPER)));
                        break;
                    }
                }
            }
        }
    }
    return showConflicts(conflicts, usagesIn);
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) ExternalUsageInfo(com.intellij.refactoring.introduceParameter.ExternalUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) InternalUsageInfo(com.intellij.refactoring.introduceParameter.InternalUsageInfo) AnySupers(org.jetbrains.plugins.groovy.refactoring.util.AnySupers) InternalUsageInfo(com.intellij.refactoring.introduceParameter.InternalUsageInfo)

Example 3 with InternalUsageInfo

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

the class GrIntroduceClosureParameterProcessor method findUsages.

@NotNull
@Override
protected UsageInfo[] findUsages() {
    ArrayList<UsageInfo> result = new ArrayList<>();
    if (!mySettings.generateDelegate() && toSearchFor != null) {
        Collection<PsiReference> refs;
        if (toSearchFor instanceof GrField) {
            refs = ReferencesSearch.search(toSearchFor).findAll();
            final GrAccessorMethod[] getters = ((GrField) toSearchFor).getGetters();
            for (GrAccessorMethod getter : getters) {
                refs.addAll(MethodReferencesSearch.search(getter, getter.getResolveScope(), true).findAll());
            }
        } else if (toSearchFor instanceof GrVariable) {
            refs = findUsagesForLocal(toReplaceIn, ((GrVariable) toSearchFor));
        } else {
            refs = ReferencesSearch.search(toSearchFor).findAll();
        }
        for (PsiReference ref1 : refs) {
            PsiElement ref = ref1.getElement();
            if (!PsiTreeUtil.isAncestor(toReplaceIn, ref, false)) {
                result.add(new ExternalUsageInfo(ref));
            } else {
                result.add(new ChangedMethodCallInfo(ref));
            }
        }
        if (toSearchFor instanceof GrVariable && !((GrVariable) toSearchFor).hasModifierProperty(PsiModifier.FINAL)) {
            setPreviewUsages(true);
        }
    }
    if (mySettings.replaceAllOccurrences()) {
        PsiElement[] exprs = GroovyIntroduceParameterUtil.getOccurrences(mySettings);
        for (PsiElement expr : exprs) {
            result.add(new InternalUsageInfo(expr));
        }
    } else {
        if (mySettings.getExpression() != null) {
            result.add(new InternalUsageInfo(mySettings.getExpression()));
        }
    }
    final UsageInfo[] usageInfos = result.toArray(new UsageInfo[result.size()]);
    return UsageViewUtil.removeDuplicatedUsages(usageInfos);
}
Also used : ArrayList(java.util.ArrayList) ChangedMethodCallInfo(com.intellij.refactoring.introduceParameter.ChangedMethodCallInfo) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) ExternalUsageInfo(com.intellij.refactoring.introduceParameter.ExternalUsageInfo) ExternalUsageInfo(com.intellij.refactoring.introduceParameter.ExternalUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) InternalUsageInfo(com.intellij.refactoring.introduceParameter.InternalUsageInfo) InternalUsageInfo(com.intellij.refactoring.introduceParameter.InternalUsageInfo) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ExternalUsageInfo (com.intellij.refactoring.introduceParameter.ExternalUsageInfo)3 InternalUsageInfo (com.intellij.refactoring.introduceParameter.InternalUsageInfo)3 UsageInfo (com.intellij.usageView.UsageInfo)3 ChangedMethodCallInfo (com.intellij.refactoring.introduceParameter.ChangedMethodCallInfo)2 Editor (com.intellij.openapi.editor.Editor)1 MultiMap (com.intellij.util.containers.MultiMap)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1 GrAccessorMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod)1 StringPartInfo (org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo)1 AnySupers (org.jetbrains.plugins.groovy.refactoring.util.AnySupers)1