Search in sources :

Example 46 with GrExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.

the class GrVariableInliner method inlineReference.

static void inlineReference(UsageInfo usage, PsiElement referenced, GrExpression initializer) {
    if (initializer == null)
        return;
    GrExpression exprToBeReplaced = (GrExpression) usage.getElement();
    if (exprToBeReplaced == null)
        return;
    if ((referenced instanceof GrAccessorMethod || referenced instanceof GrField) && exprToBeReplaced instanceof GrReferenceExpression) {
        final GroovyResolveResult resolveResult = ((GrReferenceExpression) exprToBeReplaced).advancedResolve();
        if (resolveResult.getElement() instanceof GrAccessorMethod && !resolveResult.isInvokedOnProperty()) {
            final PsiElement parent = exprToBeReplaced.getParent();
            if (parent instanceof GrCall && parent instanceof GrExpression) {
                exprToBeReplaced = (GrExpression) parent;
            } else {
                return;
            }
        }
    }
    GrExpression newExpr = exprToBeReplaced.replaceWithExpression((GrExpression) initializer.copy(), true);
    final Project project = usage.getProject();
    JavaCodeStyleManager.getInstance(project).shortenClassReferences(newExpr);
    Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
    GroovyRefactoringUtil.highlightOccurrences(project, editor, new PsiElement[] { newExpr });
    WindowManager.getInstance().getStatusBar(project).setInfo(GroovyRefactoringBundle.message("press.escape.to.remove.the.highlighting"));
}
Also used : GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) Project(com.intellij.openapi.project.Project) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 47 with GrExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.

the class GroovyInlineLocalHandler method createSettings.

/**
   * Returns Settings object for referenced definition in case of local variable
   */
@Nullable
private static InlineLocalVarSettings createSettings(final GrVariable variable, Editor editor, boolean invokedOnReference) {
    final String localName = variable.getName();
    final Project project = variable.getProject();
    GrExpression initializer = null;
    Instruction writeInstr = null;
    Instruction[] flow = null;
    //search for initializer to inline
    if (invokedOnReference) {
        LOG.assertTrue(editor != null, "null editor but invokedOnReference==true");
        final PsiReference ref = TargetElementUtil.findReference(editor);
        LOG.assertTrue(ref != null);
        PsiElement cur = ref.getElement();
        if (cur instanceof GrReferenceExpression) {
            GrControlFlowOwner controlFlowOwner;
            do {
                controlFlowOwner = ControlFlowUtils.findControlFlowOwner(cur);
                if (controlFlowOwner == null)
                    break;
                flow = controlFlowOwner.getControlFlow();
                final List<BitSet> writes = ControlFlowUtils.inferWriteAccessMap(flow, variable);
                final PsiElement finalCur = cur;
                Instruction instruction = ControlFlowUtils.findInstruction(finalCur, flow);
                LOG.assertTrue(instruction != null);
                final BitSet prev = writes.get(instruction.num());
                if (prev.cardinality() == 1) {
                    writeInstr = flow[prev.nextSetBit(0)];
                    final PsiElement element = writeInstr.getElement();
                    if (element instanceof GrVariable) {
                        initializer = ((GrVariable) element).getInitializerGroovy();
                    } else if (element instanceof GrReferenceExpression) {
                        initializer = TypeInferenceHelper.getInitializerFor((GrReferenceExpression) element);
                    }
                }
                PsiElement old_cur = cur;
                if (controlFlowOwner instanceof GrClosableBlock) {
                    cur = controlFlowOwner;
                } else {
                    PsiElement parent = controlFlowOwner.getParent();
                    if (parent instanceof GrMember)
                        cur = ((GrMember) parent).getContainingClass();
                }
                if (cur == old_cur)
                    break;
            } while (initializer == null);
        }
    } else {
        flow = ControlFlowUtils.findControlFlowOwner(variable).getControlFlow();
        initializer = variable.getInitializerGroovy();
        writeInstr = ContainerUtil.find(flow, instruction -> instruction.getElement() == variable);
    }
    if (initializer == null || writeInstr == null) {
        String message = GroovyRefactoringBundle.message("cannot.find.a.single.definition.to.inline.local.var");
        CommonRefactoringUtil.showErrorHint(variable.getProject(), editor, message, INLINE_VARIABLE, HelpID.INLINE_VARIABLE);
        return null;
    }
    int writeInstructionNumber = writeInstr.num();
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        return new InlineLocalVarSettings(initializer, writeInstructionNumber, flow);
    }
    final String question = GroovyRefactoringBundle.message("inline.local.variable.prompt.0.1", localName);
    RefactoringMessageDialog dialog = new RefactoringMessageDialog(INLINE_VARIABLE, question, HelpID.INLINE_VARIABLE, "OptionPane.questionIcon", true, project);
    if (dialog.showAndGet()) {
        return new InlineLocalVarSettings(initializer, writeInstructionNumber, flow);
    }
    return null;
}
Also used : Language(com.intellij.lang.Language) GroovyRefactoringBundle(org.jetbrains.plugins.groovy.refactoring.GroovyRefactoringBundle) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyLanguage(org.jetbrains.plugins.groovy.GroovyLanguage) RefactoringBundle(com.intellij.refactoring.RefactoringBundle) ContainerUtil(com.intellij.util.containers.ContainerUtil) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) ControlFlowUtils(org.jetbrains.plugins.groovy.codeInspection.utils.ControlFlowUtils) TypeInferenceHelper(org.jetbrains.plugins.groovy.lang.psi.dataFlow.types.TypeInferenceHelper) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) Logger(com.intellij.openapi.diagnostic.Logger) HelpID(com.intellij.refactoring.HelpID) InlineActionHandler(com.intellij.lang.refactoring.InlineActionHandler) PsiReference(com.intellij.psi.PsiReference) RefactoringMessageDialog(com.intellij.refactoring.util.RefactoringMessageDialog) Editor(com.intellij.openapi.editor.Editor) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) CommonRefactoringUtil(com.intellij.refactoring.util.CommonRefactoringUtil) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) TargetElementUtil(com.intellij.codeInsight.TargetElementUtil) ApplicationManager(com.intellij.openapi.application.ApplicationManager) BitSet(java.util.BitSet) PsiUtil(org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) RefactoringMessageDialog(com.intellij.refactoring.util.RefactoringMessageDialog) BitSet(java.util.BitSet) PsiReference(com.intellij.psi.PsiReference) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) Project(com.intellij.openapi.project.Project) GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 48 with GrExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.

the class GroovyMethodInliner method prepareNewMethod.

/**
   * Prepare temporary method with non-conflicting local names
   */
@NotNull
private static GrMethod prepareNewMethod(@NotNull GrCallExpression call, @NotNull GrMethod method, @Nullable GrExpression qualifier) throws IncorrectOperationException {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(method.getProject());
    if (method instanceof GrReflectedMethod) {
        method = ((GrReflectedMethod) method).getBaseMethod();
    }
    GrMethod newMethod = factory.createMethodFromText(method.getText(), call);
    if (qualifier != null) {
        Collection<GroovyInlineMethodUtil.ReferenceExpressionInfo> infos = GroovyInlineMethodUtil.collectReferenceInfo(method);
        GroovyInlineMethodUtil.addQualifiersToInnerReferences(newMethod, infos, qualifier);
    }
    ArrayList<PsiNamedElement> innerDefinitions = new ArrayList<>();
    collectInnerDefinitions(newMethod.getBlock(), innerDefinitions);
    // there are only local variables and parameters (possible of inner closures)
    for (PsiNamedElement namedElement : innerDefinitions) {
        String name = namedElement.getName();
        if (name != null) {
            String newName = qualifier instanceof GrReferenceExpression ? InlineMethodConflictSolver.suggestNewName(name, method, call, ((GrReferenceExpression) qualifier).getReferenceName()) : InlineMethodConflictSolver.suggestNewName(name, method, call);
            if (!newName.equals(namedElement.getName())) {
                final Collection<PsiReference> refs = ReferencesSearch.search(namedElement).findAll();
                for (PsiReference ref : refs) {
                    PsiElement element = ref.getElement();
                    if (element instanceof GrReferenceExpression) {
                        GrExpression newExpr = factory.createExpressionFromText(newName);
                        ((GrReferenceExpression) element).replaceWithExpression(newExpr, false);
                    }
                }
                namedElement.setName(newName);
            }
        }
    }
    GroovyInlineMethodUtil.replaceParametersWithArguments(call, newMethod);
    return newMethod;
}
Also used : GrReflectedMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrReflectedMethod) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) ArrayList(java.util.ArrayList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) NotNull(org.jetbrains.annotations.NotNull)

Example 49 with GrExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.

the class GroovyMethodInliner method getAloneResultExpression.

/**
   * Get method result expression (if it is alone in method)
   *
   * @return null if method has more or less than one return statement or has void type
   */
@Nullable
static GrExpression getAloneResultExpression(@NotNull GrMethod method) {
    GrOpenBlock body = method.getBlock();
    assert body != null;
    GrStatement[] statements = body.getStatements();
    if (statements.length == 1) {
        if (statements[0] instanceof GrExpression)
            return (GrExpression) statements[0];
        if (statements[0] instanceof GrReturnStatement) {
            GrExpression value = ((GrReturnStatement) statements[0]).getReturnValue();
            if (value == null && !PsiType.VOID.equals(PsiUtil.getSmartReturnType(method))) {
                return GroovyPsiElementFactory.getInstance(method.getProject()).createExpressionFromText("null");
            }
            return value;
        }
    }
    return null;
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) Nullable(org.jetbrains.annotations.Nullable)

Example 50 with GrExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.

the class GroovyMethodInliner method inlineUsage.

@Override
public void inlineUsage(@NotNull UsageInfo usage, @NotNull PsiElement referenced) {
    PsiElement element = usage.getElement();
    if (!(element instanceof GrExpression && element.getParent() instanceof GrCallExpression))
        return;
    final Editor editor = getCurrentEditorIfApplicable(element);
    GrCallExpression call = (GrCallExpression) element.getParent();
    RangeMarker marker = inlineReferenceImpl(call, myMethod, isOnExpressionOrReturnPlace(call), GroovyInlineMethodUtil.isTailMethodCall(call), editor);
    // highlight replaced result
    if (marker != null) {
        Project project = referenced.getProject();
        TextRange range = TextRange.create(marker);
        GroovyRefactoringUtil.highlightOccurrencesByRanges(project, editor, new TextRange[] { range });
        WindowManager.getInstance().getStatusBar(project).setInfo(GroovyRefactoringBundle.message("press.escape.to.remove.the.highlighting"));
        if (editor != null) {
            editor.getCaretModel().moveToOffset(marker.getEndOffset());
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) GrCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) TextRange(com.intellij.openapi.util.TextRange) RangeMarker(com.intellij.openapi.editor.RangeMarker) Editor(com.intellij.openapi.editor.Editor)

Aggregations

GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)312 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)93 Nullable (org.jetbrains.annotations.Nullable)68 PsiElement (com.intellij.psi.PsiElement)62 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)43 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)43 NotNull (org.jetbrains.annotations.NotNull)37 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)35 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)33 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)29 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)27 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)26 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)26 PsiType (com.intellij.psi.PsiType)24 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)23 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)23 IElementType (com.intellij.psi.tree.IElementType)22 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)18 ArrayList (java.util.ArrayList)16 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)16