Search in sources :

Example 1 with JavaChangeSignatureDialog

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

the class CreateParameterFromUsageFix method showDialog.

private static void showDialog(final PsiMethod method, final GrReferenceExpression ref, final Project project) {
    final String name = ref.getReferenceName();
    final List<PsiType> types = GroovyExpectedTypesProvider.getDefaultExpectedTypes(ref);
    PsiType unboxed = types.isEmpty() ? null : TypesUtil.unboxPrimitiveTypeWrapper(types.get(0));
    @NotNull final PsiType type = unboxed != null ? unboxed : PsiType.getJavaLangObject(ref.getManager(), ref.getResolveScope());
    if (method instanceof GrMethod) {
        GrMethodDescriptor descriptor = new GrMethodDescriptor((GrMethod) method);
        GrChangeSignatureDialog dialog = new GrChangeSignatureDialog(project, descriptor, true, ref);
        List<GrParameterInfo> parameters = dialog.getParameters();
        parameters.add(createParameterInfo(name, type));
        dialog.setParameterInfos(parameters);
        dialog.show();
    } else if (method != null) {
        JavaChangeSignatureDialog dialog = new JavaChangeSignatureDialog(project, method, false, ref);
        final List<ParameterInfoImpl> parameterInfos = new ArrayList<>(Arrays.asList(ParameterInfoImpl.fromMethod(method)));
        ParameterInfoImpl parameterInfo = new ParameterInfoImpl(-1, name, type, PsiTypesUtil.getDefaultValueOfType(type), false);
        if (!method.isVarArgs()) {
            parameterInfos.add(parameterInfo);
        } else {
            parameterInfos.add(parameterInfos.size() - 1, parameterInfo);
        }
        dialog.setParameterInfos(parameterInfos);
        dialog.show();
    }
}
Also used : GrMethodDescriptor(org.jetbrains.plugins.groovy.refactoring.changeSignature.GrMethodDescriptor) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl) NotNull(org.jetbrains.annotations.NotNull) GrChangeSignatureDialog(org.jetbrains.plugins.groovy.refactoring.changeSignature.GrChangeSignatureDialog) GrParameterInfo(org.jetbrains.plugins.groovy.refactoring.changeSignature.GrParameterInfo) JavaChangeSignatureDialog(com.intellij.refactoring.changeSignature.JavaChangeSignatureDialog) ArrayList(java.util.ArrayList) List(java.util.List) PsiType(com.intellij.psi.PsiType)

Example 2 with JavaChangeSignatureDialog

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

the class CreateParameterFromUsageFix method invokeImpl.

@Override
protected void invokeImpl(PsiClass targetClass) {
    TransactionGuard.getInstance().submitTransactionLater(targetClass.getProject(), () -> {
        if (!myReferenceExpression.isValid())
            return;
        if (CreateFromUsageUtils.isValidReference(myReferenceExpression, false))
            return;
        final Project project = myReferenceExpression.getProject();
        PsiType[] expectedTypes = CreateFromUsageUtils.guessType(myReferenceExpression, false);
        PsiType type = expectedTypes[0];
        final String varName = myReferenceExpression.getReferenceName();
        PsiMethod method = PsiTreeUtil.getParentOfType(myReferenceExpression, PsiMethod.class);
        LOG.assertTrue(method != null);
        method = IntroduceParameterHandler.chooseEnclosingMethod(method);
        if (method == null)
            return;
        method = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringBundle.message("to.refactor"));
        if (method == null)
            return;
        final List<ParameterInfoImpl> parameterInfos = new ArrayList<>(Arrays.asList(ParameterInfoImpl.fromMethod(method)));
        ParameterInfoImpl parameterInfo = new ParameterInfoImpl(-1, varName, type, varName, false);
        if (!method.isVarArgs()) {
            parameterInfos.add(parameterInfo);
        } else {
            parameterInfos.add(parameterInfos.size() - 1, parameterInfo);
        }
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            ParameterInfoImpl[] array = parameterInfos.toArray(new ParameterInfoImpl[parameterInfos.size()]);
            String modifier = PsiUtil.getAccessModifier(PsiUtil.getAccessLevel(method.getModifierList()));
            ChangeSignatureProcessor processor = new ChangeSignatureProcessor(project, method, false, modifier, method.getName(), method.getReturnType(), array);
            processor.run();
        } else {
            try {
                JavaChangeSignatureDialog dialog = JavaChangeSignatureDialog.createAndPreselectNew(project, method, parameterInfos, true, myReferenceExpression);
                dialog.setParameterInfos(parameterInfos);
                if (dialog.showAndGet()) {
                    for (ParameterInfoImpl info : parameterInfos) {
                        if (info.getOldIndex() == -1) {
                            final String newParamName = info.getName();
                            if (!Comparing.strEqual(varName, newParamName)) {
                                final PsiExpression newExpr = JavaPsiFacade.getElementFactory(project).createExpressionFromText(newParamName, method);
                                new WriteCommandAction(project) {

                                    @Override
                                    protected void run(@NotNull Result result) throws Throwable {
                                        final PsiReferenceExpression[] refs = CreateFromUsageUtils.collectExpressions(myReferenceExpression, PsiMember.class, PsiFile.class);
                                        for (PsiReferenceExpression ref : refs) {
                                            ref.replace(newExpr.copy());
                                        }
                                    }
                                }.execute();
                            }
                            break;
                        }
                    }
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    });
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) ArrayList(java.util.ArrayList) ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl) Result(com.intellij.openapi.application.Result) Project(com.intellij.openapi.project.Project) JavaChangeSignatureDialog(com.intellij.refactoring.changeSignature.JavaChangeSignatureDialog) ChangeSignatureProcessor(com.intellij.refactoring.changeSignature.ChangeSignatureProcessor)

Example 3 with JavaChangeSignatureDialog

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

the class VariableTypeFix method changeMethodSignatureIfNeeded.

private boolean changeMethodSignatureIfNeeded(PsiVariable myVariable) {
    if (myVariable instanceof PsiParameter) {
        final PsiElement scope = ((PsiParameter) myVariable).getDeclarationScope();
        if (scope instanceof PsiMethod) {
            final PsiMethod method = (PsiMethod) scope;
            final PsiMethod psiMethod = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringBundle.message("to.refactor"));
            if (psiMethod == null)
                return true;
            final int parameterIndex = method.getParameterList().getParameterIndex((PsiParameter) myVariable);
            if (!FileModificationService.getInstance().prepareFileForWrite(psiMethod.getContainingFile()))
                return true;
            final ArrayList<ParameterInfoImpl> infos = new ArrayList<>();
            int i = 0;
            for (PsiParameter parameter : psiMethod.getParameterList().getParameters()) {
                final boolean changeType = i == parameterIndex;
                infos.add(new ParameterInfoImpl(i++, parameter.getName(), changeType ? getReturnType() : parameter.getType()));
            }
            if (!ApplicationManager.getApplication().isUnitTestMode()) {
                final JavaChangeSignatureDialog dialog = new JavaChangeSignatureDialog(psiMethod.getProject(), psiMethod, false, myVariable);
                dialog.setParameterInfos(infos);
                dialog.show();
            } else {
                ChangeSignatureProcessor processor = new ChangeSignatureProcessor(psiMethod.getProject(), psiMethod, false, null, psiMethod.getName(), psiMethod.getReturnType(), infos.toArray(new ParameterInfoImpl[infos.size()]));
                processor.run();
            }
            return true;
        }
    }
    return false;
}
Also used : ArrayList(java.util.ArrayList) JavaChangeSignatureDialog(com.intellij.refactoring.changeSignature.JavaChangeSignatureDialog) ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl) LocalQuickFixAndIntentionActionOnPsiElement(com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement) ChangeSignatureProcessor(com.intellij.refactoring.changeSignature.ChangeSignatureProcessor)

Example 4 with JavaChangeSignatureDialog

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

the class ChangeMethodSignatureFromUsageFix method performChange.

public static List<ParameterInfoImpl> performChange(final Project project, final Editor editor, final PsiFile file, final PsiMethod method, final int minUsagesNumber, final ParameterInfoImpl[] newParametersInfo, final boolean changeAllUsages, final boolean allowDelegation) {
    if (!FileModificationService.getInstance().prepareFileForWrite(method.getContainingFile()))
        return null;
    final FindUsagesManager findUsagesManager = ((FindManagerImpl) FindManager.getInstance(project)).getFindUsagesManager();
    final FindUsagesHandler handler = findUsagesManager.getFindUsagesHandler(method, false);
    //on failure or cancel (e.g. cancel of super methods dialog)
    if (handler == null)
        return null;
    final JavaMethodFindUsagesOptions options = new JavaMethodFindUsagesOptions(project);
    options.isImplementingMethods = true;
    options.isOverridingMethods = true;
    options.isUsages = true;
    options.isSearchForTextOccurrences = false;
    final int[] usagesFound = new int[1];
    Runnable runnable = () -> {
        Processor<UsageInfo> processor = t -> ++usagesFound[0] < minUsagesNumber;
        handler.processElementUsages(method, processor, options);
    };
    String progressTitle = QuickFixBundle.message("searching.for.usages.progress.title");
    if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(runnable, progressTitle, true, project))
        return null;
    if (ApplicationManager.getApplication().isUnitTestMode() || usagesFound[0] < minUsagesNumber) {
        ChangeSignatureProcessor processor = new ChangeSignatureProcessor(project, method, false, null, method.getName(), method.getReturnType(), newParametersInfo) {

            @Override
            @NotNull
            protected UsageInfo[] findUsages() {
                return changeAllUsages ? super.findUsages() : UsageInfo.EMPTY_ARRAY;
            }

            @Override
            protected void performRefactoring(@NotNull UsageInfo[] usages) {
                CommandProcessor.getInstance().setCurrentCommandName(getCommandName());
                super.performRefactoring(usages);
            }
        };
        processor.run();
        ApplicationManager.getApplication().runWriteAction(() -> UndoUtil.markPsiFileForUndo(file));
        return Arrays.asList(newParametersInfo);
    } else {
        final List<ParameterInfoImpl> parameterInfos = newParametersInfo != null ? new ArrayList<>(Arrays.asList(newParametersInfo)) : new ArrayList<>();
        final PsiReferenceExpression refExpr = JavaTargetElementEvaluator.findReferenceExpression(editor);
        JavaChangeSignatureDialog dialog = JavaChangeSignatureDialog.createAndPreselectNew(project, method, parameterInfos, allowDelegation, refExpr);
        dialog.setParameterInfos(parameterInfos);
        dialog.show();
        return dialog.isOK() ? dialog.getParameters() : null;
    }
}
Also used : FindUsagesHandler(com.intellij.find.findUsages.FindUsagesHandler) ChangeSignatureProcessor(com.intellij.refactoring.changeSignature.ChangeSignatureProcessor) CommandProcessor(com.intellij.openapi.command.CommandProcessor) Processor(com.intellij.util.Processor) JavaMethodFindUsagesOptions(com.intellij.find.findUsages.JavaMethodFindUsagesOptions) ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl) NotNull(org.jetbrains.annotations.NotNull) FindManagerImpl(com.intellij.find.impl.FindManagerImpl) JavaChangeSignatureDialog(com.intellij.refactoring.changeSignature.JavaChangeSignatureDialog) FindUsagesManager(com.intellij.find.findUsages.FindUsagesManager) UsageInfo(com.intellij.usageView.UsageInfo) ChangeSignatureProcessor(com.intellij.refactoring.changeSignature.ChangeSignatureProcessor)

Aggregations

JavaChangeSignatureDialog (com.intellij.refactoring.changeSignature.JavaChangeSignatureDialog)4 ParameterInfoImpl (com.intellij.refactoring.changeSignature.ParameterInfoImpl)4 ChangeSignatureProcessor (com.intellij.refactoring.changeSignature.ChangeSignatureProcessor)3 ArrayList (java.util.ArrayList)3 NotNull (org.jetbrains.annotations.NotNull)2 LocalQuickFixAndIntentionActionOnPsiElement (com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement)1 FindUsagesHandler (com.intellij.find.findUsages.FindUsagesHandler)1 FindUsagesManager (com.intellij.find.findUsages.FindUsagesManager)1 JavaMethodFindUsagesOptions (com.intellij.find.findUsages.JavaMethodFindUsagesOptions)1 FindManagerImpl (com.intellij.find.impl.FindManagerImpl)1 Result (com.intellij.openapi.application.Result)1 CommandProcessor (com.intellij.openapi.command.CommandProcessor)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 Project (com.intellij.openapi.project.Project)1 PsiType (com.intellij.psi.PsiType)1 UsageInfo (com.intellij.usageView.UsageInfo)1 Processor (com.intellij.util.Processor)1 List (java.util.List)1 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)1 GrChangeSignatureDialog (org.jetbrains.plugins.groovy.refactoring.changeSignature.GrChangeSignatureDialog)1