Search in sources :

Example 31 with GrExpression

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

the class ConvertSimpleGetterToPropertyIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    GrMethod method = (GrMethod) element.getParent();
    GrOpenBlock block = method.getBlock();
    if (block == null)
        return;
    GrStatement statement = block.getStatements()[0];
    GrExpression value;
    if (statement instanceof GrReturnStatement) {
        value = ((GrReturnStatement) statement).getReturnValue();
    } else {
        value = (GrExpression) statement;
    }
    String fieldName = GroovyPropertyUtils.getPropertyNameByGetter(method);
    if (fieldName == null)
        return;
    PsiClass aClass = method.getContainingClass();
    if (aClass == null)
        return;
    List<String> modifiers = ContainerUtil.newArrayList();
    for (String modifier : MODIFIERS_TO_CHECK) {
        if (method.hasModifierProperty(modifier))
            modifiers.add(modifier);
    }
    modifiers.add(PsiModifier.FINAL);
    GrTypeElement returnTypeElement = method.getReturnTypeElementGroovy();
    PsiType returnType = returnTypeElement == null ? null : returnTypeElement.getType();
    GrVariableDeclaration declaration = GroovyPsiElementFactory.getInstance(project).createFieldDeclaration(ArrayUtil.toStringArray(modifiers), fieldName, value, returnType);
    PsiElement replaced = method.replace(declaration);
    JavaCodeStyleManager.getInstance(project).shortenClassReferences(replaced);
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) PsiClass(com.intellij.psi.PsiClass) 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) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) PsiType(com.intellij.psi.PsiType)

Example 32 with GrExpression

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

the class MergeIfAndIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrIfStatement parentStatement = (GrIfStatement) element;
    final GrStatement parentThenBranch = parentStatement.getThenBranch();
    final GrIfStatement childStatement = (GrIfStatement) ConditionalUtils.stripBraces(parentThenBranch);
    final GrExpression childCondition = childStatement.getCondition();
    final String childConditionText;
    if (ParenthesesUtils.getPrecedence(childCondition) > ParenthesesUtils.AND_PRECEDENCE) {
        childConditionText = '(' + childCondition.getText() + ')';
    } else {
        childConditionText = childCondition.getText();
    }
    final GrExpression parentCondition = parentStatement.getCondition();
    final String parentConditionText;
    if (ParenthesesUtils.getPrecedence(parentCondition) > ParenthesesUtils.AND_PRECEDENCE) {
        parentConditionText = '(' + parentCondition.getText() + ')';
    } else {
        parentConditionText = parentCondition.getText();
    }
    final GrStatement childThenBranch = childStatement.getThenBranch();
    @NonNls final String statement = "if(" + parentConditionText + "&&" + childConditionText + ')' + childThenBranch.getText();
    PsiImplUtil.replaceStatement(statement, parentStatement);
}
Also used : NonNls(org.jetbrains.annotations.NonNls) GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 33 with GrExpression

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

the class ReplaceIfWithTernaryIntention method getElementPredicate.

@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
    return new PsiElementPredicate() {

        @Override
        public boolean satisfiedBy(PsiElement e) {
            if (!e.getNode().getElementType().equals(GroovyTokenTypes.kIF))
                return false;
            if (!(e.getParent() instanceof GrIfStatement))
                return false;
            final GrIfStatement ifStatement = (GrIfStatement) e.getParent();
            final PsiElement thenBranch = skipBlock(ifStatement.getThenBranch());
            final PsiElement elseBranch = skipBlock(ifStatement.getElseBranch());
            if (thenBranch instanceof GrAssignmentExpression && elseBranch instanceof GrAssignmentExpression && ((GrAssignmentExpression) thenBranch).getRValue() != null && ((GrAssignmentExpression) elseBranch).getRValue() != null) {
                final GrExpression lvalue1 = ((GrAssignmentExpression) thenBranch).getLValue();
                final GrExpression lvalue2 = ((GrAssignmentExpression) elseBranch).getLValue();
                return EquivalenceChecker.expressionsAreEquivalent(lvalue1, lvalue2);
            }
            if (thenBranch instanceof GrReturnStatement && elseBranch instanceof GrReturnStatement && ((GrReturnStatement) thenBranch).getReturnValue() != null && ((GrReturnStatement) elseBranch).getReturnValue() != null) {
                return true;
            }
            return false;
        }
    };
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 34 with GrExpression

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

the class SimplifyTernaryOperatorIntention method getElementPredicate.

@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
    return new PsiElementPredicate() {

        @Override
        public boolean satisfiedBy(PsiElement element) {
            if (!(element instanceof GrConditionalExpression)) {
                return false;
            }
            GrConditionalExpression condExp = (GrConditionalExpression) element;
            PsiType condType = condExp.getType();
            if (condType == null || !PsiType.BOOLEAN.isConvertibleFrom(condType)) {
                return false;
            }
            GrExpression thenBranch = condExp.getThenBranch();
            GrExpression elseBranch = condExp.getElseBranch();
            Object thenVal = GroovyConstantExpressionEvaluator.evaluate(thenBranch);
            if (Boolean.TRUE.equals(thenVal) && elseBranch != null) {
                return true;
            }
            Object elseVal = GroovyConstantExpressionEvaluator.evaluate(elseBranch);
            if (thenBranch != null && Boolean.FALSE.equals(elseVal)) {
                return true;
            }
            return false;
        }
    };
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) GrConditionalExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrConditionalExpression) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType) NotNull(org.jetbrains.annotations.NotNull)

Example 35 with GrExpression

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

the class SplitIfIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement andElement, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    GrBinaryExpression binaryExpression = (GrBinaryExpression) andElement.getParent();
    GrIfStatement ifStatement = (GrIfStatement) binaryExpression.getParent();
    GrExpression leftOperand = binaryExpression.getLeftOperand();
    GrExpression rightOperand = binaryExpression.getRightOperand();
    GrStatement thenBranch = ifStatement.getThenBranch();
    assert thenBranch != null;
    assert rightOperand != null;
    GrStatement newSplittedIfs = GroovyPsiElementFactory.getInstance(project).createStatementFromText("if(" + leftOperand.getText() + ") { \n" + "  if(" + rightOperand.getText() + ")" + thenBranch.getText() + "\n" + "}");
    ifStatement.replaceWithStatement(newSplittedIfs);
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrBinaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

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