Search in sources :

Example 1 with SafeDeleteReferenceJavaDeleteUsageInfo

use of com.intellij.refactoring.safeDelete.usageInfo.SafeDeleteReferenceJavaDeleteUsageInfo in project intellij-community by JetBrains.

the class JavaSafeDeleteDelegateForGroovy method createUsageInfoForParameter.

@Override
public void createUsageInfoForParameter(PsiReference reference, List<UsageInfo> usages, final PsiParameter parameter, final PsiMethod method) {
    int index = method.getParameterList().getParameterIndex(parameter);
    final PsiElement element = reference.getElement();
    GrCall call = null;
    if (element instanceof GrCall) {
        call = (GrCall) element;
    } else if (element.getParent() instanceof GrCall) {
        call = (GrCall) element.getParent();
    }
    if (call != null) {
        GrClosureSignature signature = GrClosureSignatureUtil.createSignature(call);
        //todo ???
        if (signature == null)
            return;
        GrClosureSignatureUtil.ArgInfo<PsiElement>[] argInfos = GrClosureSignatureUtil.mapParametersToArguments(signature, call);
        //todo???
        if (argInfos == null)
            return;
        for (PsiElement arg : argInfos[index].args) {
            usages.add(new SafeDeleteReferenceJavaDeleteUsageInfo(arg, parameter, true));
        }
    } else if (element instanceof GrDocMethodReference) {
        @NonNls final StringBuilder newText = new StringBuilder();
        newText.append("/** @see ");
        GrDocReferenceElement holder = ((GrDocMethodReference) element).getReferenceHolder();
        if (holder != null) {
            newText.append(holder.getText());
        }
        newText.append('#');
        newText.append(method.getName());
        newText.append('(');
        final List<PsiParameter> parameters = new ArrayList<>(Arrays.asList(method.getParameterList().getParameters()));
        parameters.remove(parameter);
        newText.append(StringUtil.join(parameters, psiParameter -> parameter.getType().getCanonicalText(), ","));
        newText.append(")*/");
        usages.add(new SafeDeleteReferenceJavaDeleteUsageInfo(element, parameter, true) {

            @Override
            public void deleteElement() throws IncorrectOperationException {
                ((GrDocMethodReference) element).bindToText(method.getProject(), newText.toString());
            }
        });
    }
}
Also used : SafeDeleteReferenceJavaDeleteUsageInfo(com.intellij.refactoring.safeDelete.usageInfo.SafeDeleteReferenceJavaDeleteUsageInfo) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrDocReferenceElement(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocReferenceElement) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) ArrayList(java.util.ArrayList) List(java.util.List) GrDocMethodReference(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocMethodReference)

Example 2 with SafeDeleteReferenceJavaDeleteUsageInfo

use of com.intellij.refactoring.safeDelete.usageInfo.SafeDeleteReferenceJavaDeleteUsageInfo in project intellij-community by JetBrains.

the class JavaSafeDeleteDelegateImpl method createUsageInfoForParameter.

@Override
public void createUsageInfoForParameter(final PsiReference reference, final List<UsageInfo> usages, final PsiParameter parameter, final PsiMethod method) {
    int index = method.getParameterList().getParameterIndex(parameter);
    final PsiElement element = reference.getElement();
    PsiCall call = null;
    if (element instanceof PsiCall) {
        call = (PsiCall) element;
    } else {
        final PsiElement parent = element.getParent();
        if (parent instanceof PsiCall) {
            call = (PsiCall) parent;
        } else if (parent instanceof PsiAnonymousClass) {
            call = (PsiNewExpression) parent.getParent();
        }
    }
    if (call != null) {
        final PsiExpressionList argList = call.getArgumentList();
        if (argList != null) {
            final PsiExpression[] args = argList.getExpressions();
            if (index < args.length) {
                if (!parameter.isVarArgs()) {
                    usages.add(new SafeDeleteReferenceJavaDeleteUsageInfo(args[index], parameter));
                } else {
                    for (int i = index; i < args.length; i++) {
                        usages.add(new SafeDeleteReferenceJavaDeleteUsageInfo(args[i], parameter));
                    }
                }
            }
        }
    } else if (element instanceof PsiDocMethodOrFieldRef) {
        if (((PsiDocMethodOrFieldRef) element).getSignature() != null) {
            @NonNls final StringBuffer newText = new StringBuffer();
            newText.append("/** @see #").append(method.getName()).append('(');
            final List<PsiParameter> parameters = new ArrayList<>(Arrays.asList(method.getParameterList().getParameters()));
            parameters.remove(parameter);
            newText.append(StringUtil.join(parameters, psiParameter -> psiParameter.getType().getCanonicalText(), ","));
            newText.append(")*/");
            usages.add(new SafeDeleteReferenceJavaDeleteUsageInfo(element, parameter, true) {

                public void deleteElement() throws IncorrectOperationException {
                    final PsiDocMethodOrFieldRef.MyReference javadocMethodReference = (PsiDocMethodOrFieldRef.MyReference) element.getReference();
                    if (javadocMethodReference != null) {
                        javadocMethodReference.bindToText(method.getContainingClass(), newText);
                    }
                }
            });
        }
    } else if (element instanceof PsiMethodReferenceExpression) {
        usages.add(new SafeDeleteReferenceJavaDeleteUsageInfo(element, parameter, true) {

            public void deleteElement() throws IncorrectOperationException {
                final PsiExpression callExpression = LambdaRefactoringUtil.convertToMethodCallInLambdaBody((PsiMethodReferenceExpression) element);
                if (callExpression instanceof PsiCallExpression) {
                    final PsiExpressionList expressionList = ((PsiCallExpression) callExpression).getArgumentList();
                    if (expressionList != null) {
                        final PsiExpression[] args = expressionList.getExpressions();
                        if (index < args.length) {
                            args[index].delete();
                        }
                    }
                }
            }
        });
    }
}
Also used : SafeDeleteReferenceJavaDeleteUsageInfo(com.intellij.refactoring.safeDelete.usageInfo.SafeDeleteReferenceJavaDeleteUsageInfo) PsiDocMethodOrFieldRef(com.intellij.psi.impl.source.javadoc.PsiDocMethodOrFieldRef) ArrayList(java.util.ArrayList) List(java.util.List) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Aggregations

SafeDeleteReferenceJavaDeleteUsageInfo (com.intellij.refactoring.safeDelete.usageInfo.SafeDeleteReferenceJavaDeleteUsageInfo)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 PsiDocMethodOrFieldRef (com.intellij.psi.impl.source.javadoc.PsiDocMethodOrFieldRef)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 GrDocMethodReference (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocMethodReference)1 GrDocReferenceElement (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocReferenceElement)1 GrClosureSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)1 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)1