Search in sources :

Example 1 with IntroduceVariableHandler

use of com.intellij.refactoring.introduceVariable.IntroduceVariableHandler in project intellij-community by JetBrains.

the class LambdaRefactoringUtil method removeSideEffectsFromLambdaBody.

/**
   * Works for expression lambdas/one statement code block lambdas to ensures equivalent method ref -> lambda transformation. 
   */
public static void removeSideEffectsFromLambdaBody(Editor editor, PsiLambdaExpression lambdaExpression) {
    if (lambdaExpression != null && lambdaExpression.isValid()) {
        final PsiElement body = lambdaExpression.getBody();
        PsiExpression methodCall = LambdaUtil.extractSingleExpressionFromBody(body);
        PsiExpression qualifierExpression = null;
        if (methodCall instanceof PsiMethodCallExpression) {
            qualifierExpression = ((PsiMethodCallExpression) methodCall).getMethodExpression().getQualifierExpression();
        } else if (methodCall instanceof PsiNewExpression) {
            qualifierExpression = ((PsiNewExpression) methodCall).getQualifier();
        }
        if (qualifierExpression != null) {
            final List<PsiElement> sideEffects = new ArrayList<>();
            SideEffectChecker.checkSideEffects(qualifierExpression, sideEffects);
            if (!sideEffects.isEmpty()) {
                if (ApplicationManager.getApplication().isUnitTestMode() || Messages.showYesNoDialog(lambdaExpression.getProject(), "There are possible side effects found in method reference qualifier." + "\nIntroduce local variable?", "Side Effects Detected", Messages.getQuestionIcon()) == Messages.YES) {
                    //ensure introduced before lambda
                    qualifierExpression.putUserData(ElementToWorkOn.PARENT, lambdaExpression);
                    new IntroduceVariableHandler().invoke(qualifierExpression.getProject(), editor, qualifierExpression);
                }
            }
        }
    }
}
Also used : IntroduceVariableHandler(com.intellij.refactoring.introduceVariable.IntroduceVariableHandler) ArrayList(java.util.ArrayList)

Example 2 with IntroduceVariableHandler

use of com.intellij.refactoring.introduceVariable.IntroduceVariableHandler in project intellij-community by JetBrains.

the class IntroduceVariablePostfixTemplate method expandForChooseExpression.

@Override
protected void expandForChooseExpression(@NotNull PsiElement expression, @NotNull Editor editor) {
    // for advanced stuff use ((PsiJavaCodeReferenceElement)expression).advancedResolve(true).getElement();
    IntroduceVariableHandler handler = ApplicationManager.getApplication().isUnitTestMode() ? getMockHandler() : new IntroduceVariableHandler();
    handler.invoke(expression.getProject(), editor, (PsiExpression) expression);
}
Also used : IntroduceVariableHandler(com.intellij.refactoring.introduceVariable.IntroduceVariableHandler)

Example 3 with IntroduceVariableHandler

use of com.intellij.refactoring.introduceVariable.IntroduceVariableHandler in project intellij-community by JetBrains.

the class IntroduceVariableIntentionAction method invoke.

@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
    final PsiExpressionStatement statement = detectExpressionStatement(element);
    if (statement == null) {
        return;
    }
    new IntroduceVariableHandler().invoke(project, editor, statement.getExpression());
}
Also used : IntroduceVariableHandler(com.intellij.refactoring.introduceVariable.IntroduceVariableHandler)

Aggregations

IntroduceVariableHandler (com.intellij.refactoring.introduceVariable.IntroduceVariableHandler)3 ArrayList (java.util.ArrayList)1