Search in sources :

Example 16 with GrAssignmentExpression

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

the class GrConcatenationInjector method findLanguageParams.

private static BaseInjection findLanguageParams(PsiElement place) {
    PsiElement parent = place.getParent();
    if (parent instanceof GrAssignmentExpression && ((GrAssignmentExpression) parent).getRValue() == place) {
        final GrExpression lvalue = ((GrAssignmentExpression) parent).getLValue();
        if (lvalue instanceof GrReferenceExpression) {
            final PsiElement resolved = ((GrReferenceExpression) lvalue).resolve();
            if (resolved instanceof PsiModifierListOwner) {
                return getLanguageParams((PsiModifierListOwner) resolved);
            }
        }
    } else if (parent instanceof GrVariable) {
        return getLanguageParams((PsiModifierListOwner) parent);
    } else if (parent instanceof GrArgumentList) {
        final PsiElement pparent = parent.getParent();
        if (pparent instanceof GrCall) {
            final GrCall call = (GrCall) pparent;
            final GroovyResolveResult result = call.advancedResolve();
            if (result.getElement() != null) {
                final Map<GrExpression, Pair<PsiParameter, PsiType>> map = GrClosureSignatureUtil.mapArgumentsToParameters(result, place, false, false, call.getNamedArguments(), call.getExpressionArguments(), call.getClosureArguments());
                if (map != null) {
                    final Pair<PsiParameter, PsiType> pair = map.get(place);
                    return getLanguageParams(pair.first);
                }
            }
        }
    }
    return null;
}
Also used : GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) Pair(com.intellij.openapi.util.Pair)

Example 17 with GrAssignmentExpression

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

the class GrCastFix method findExpressionToCast.

private static GrExpression findExpressionToCast(ProblemDescriptor descriptor) {
    final PsiElement element = descriptor.getPsiElement();
    final PsiElement parent = element.getParent();
    if (parent instanceof GrVariable) {
        return ((GrVariable) parent).getInitializerGroovy();
    } else if (parent instanceof GrAssignmentExpression) {
        return ((GrAssignmentExpression) parent).getRValue();
    } else if (parent instanceof GrThrowStatement) {
        return ((GrThrowStatement) parent).getException();
    } else if (parent instanceof GrReturnStatement) {
        return ((GrReturnStatement) parent).getReturnValue();
    } else if (element instanceof GrExpression) {
        return (GrExpression) element;
    }
    PsiFile file = element.getContainingFile();
    VirtualFile virtualFile = file.getVirtualFile();
    String url = virtualFile == null ? "" : virtualFile.getPresentableUrl();
    LOG.error("can't find expression to cast at position " + element.getTextRange(), new Attachment(url, file.getText()));
    return null;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) VirtualFile(com.intellij.openapi.vfs.VirtualFile) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrThrowStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrThrowStatement) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiFile(com.intellij.psi.PsiFile) Attachment(com.intellij.openapi.diagnostic.Attachment) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) PsiElement(com.intellij.psi.PsiElement)

Example 18 with GrAssignmentExpression

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

the class ExpandBooleanIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrStatement containingStatement = (GrStatement) element;
    if (ExpandBooleanPredicate.isBooleanAssignment(containingStatement)) {
        final GrAssignmentExpression assignmentExpression = (GrAssignmentExpression) containingStatement;
        final GrExpression rhs = assignmentExpression.getRValue();
        assert rhs != null;
        final String rhsText = rhs.getText();
        final GrExpression lhs = assignmentExpression.getLValue();
        final String lhsText = lhs.getText();
        @NonNls final String statement = "if(" + rhsText + "){\n" + lhsText + " = true\n}else{\n" + lhsText + " = false\n}";
        PsiImplUtil.replaceStatement(statement, containingStatement);
    } else if (ExpandBooleanPredicate.isBooleanReturn(containingStatement)) {
        final GrReturnStatement returnStatement = (GrReturnStatement) containingStatement;
        final GrExpression returnValue = returnStatement.getReturnValue();
        final String valueText = returnValue.getText();
        @NonNls final String statement = "if(" + valueText + "){\nreturn true\n}else{\nreturn false\n}";
        PsiImplUtil.replaceStatement(statement, containingStatement);
    }
}
Also used : NonNls(org.jetbrains.annotations.NonNls) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 19 with GrAssignmentExpression

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

the class ReplaceIfWithTernaryIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrIfStatement ifStatement = (GrIfStatement) element.getParent();
    final PsiElement thenBranch = skipBlock(ifStatement.getThenBranch());
    final PsiElement elseBranch = skipBlock(ifStatement.getElseBranch());
    if (thenBranch instanceof GrAssignmentExpression && elseBranch instanceof GrAssignmentExpression) {
        final GrAssignmentExpression assignment = (GrAssignmentExpression) GroovyPsiElementFactory.getInstance(project).createStatementFromText("a = b ? c : d");
        assignment.getLValue().replaceWithExpression(((GrAssignmentExpression) thenBranch).getLValue(), true);
        final GrConditionalExpression conditional = (GrConditionalExpression) assignment.getRValue();
        replaceConditional(conditional, ifStatement.getCondition(), ((GrAssignmentExpression) thenBranch).getRValue(), ((GrAssignmentExpression) elseBranch).getRValue());
        ifStatement.replaceWithStatement(assignment);
    }
    if (thenBranch instanceof GrReturnStatement && elseBranch instanceof GrReturnStatement) {
        final GrReturnStatement returnSt = (GrReturnStatement) GroovyPsiElementFactory.getInstance(project).createStatementFromText("return a ? b : c");
        final GrConditionalExpression conditional = (GrConditionalExpression) returnSt.getReturnValue();
        replaceConditional(conditional, ifStatement.getCondition(), ((GrReturnStatement) thenBranch).getReturnValue(), ((GrReturnStatement) elseBranch).getReturnValue());
        ifStatement.replaceWithStatement(returnSt);
    }
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrConditionalExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrConditionalExpression) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) PsiElement(com.intellij.psi.PsiElement)

Example 20 with GrAssignmentExpression

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

the class GroovyShellLanguageConsoleView method processCode.

protected void processCode() {
    GroovyShellCodeFragment groovyFile = getGroovyFile();
    for (GrTopStatement statement : groovyFile.getTopStatements()) {
        if (statement instanceof GrImportStatement) {
            groovyFile.addImportsFromString(importToString((GrImportStatement) statement));
        } else if (statement instanceof GrMethod) {
            groovyFile.addVariable(((GrMethod) statement).getName(), generateClosure((GrMethod) statement));
        } else if (statement instanceof GrAssignmentExpression) {
            GrAssignmentExpression assignment = (GrAssignmentExpression) statement;
            GrExpression left = assignment.getLValue();
            if (left instanceof GrReferenceExpression && !((GrReferenceExpression) left).isQualified()) {
                groovyFile.addVariable(((GrReferenceExpression) left).getReferenceName(), assignment.getRValue());
            }
        } else if (statement instanceof GrTypeDefinition) {
            groovyFile.addTypeDefinition(prepareTypeDefinition((GrTypeDefinition) statement));
        }
    }
    PsiType scriptType = groovyFile.getInferredScriptReturnType();
    if (scriptType != null) {
        groovyFile.addVariable("_", scriptType);
    }
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrTopStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) PsiType(com.intellij.psi.PsiType)

Aggregations

GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)34 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)26 PsiElement (com.intellij.psi.PsiElement)16 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)12 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)8 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)7 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)6 IElementType (com.intellij.psi.tree.IElementType)5 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)5 NotNull (org.jetbrains.annotations.NotNull)4 Nullable (org.jetbrains.annotations.Nullable)4 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)4 PsiFile (com.intellij.psi.PsiFile)3 PsiType (com.intellij.psi.PsiType)3 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)3 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)3 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)3 GrApplicationStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement)3 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)3 ASTNode (com.intellij.lang.ASTNode)2