Search in sources :

Example 6 with GrIfStatement

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

the class MergeElseIfIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrIfStatement parentStatement = (GrIfStatement) element;
    GrBlockStatement elseBlockStatement = (GrBlockStatement) parentStatement.getElseBranch();
    assert elseBlockStatement != null;
    final GrOpenBlock elseBranch = elseBlockStatement.getBlock();
    final GrStatement elseBranchContents = elseBranch.getStatements()[0];
    PsiImplUtil.replaceStatement("if(" + parentStatement.getCondition().getText() + ")" + parentStatement.getThenBranch().getText() + "else " + elseBranchContents.getText(), parentStatement);
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrBlockStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 7 with GrIfStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement 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 8 with GrIfStatement

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

the class MergeIfAndPredicate method satisfiedBy.

@Override
public boolean satisfiedBy(PsiElement element) {
    if (!(element instanceof GrIfStatement)) {
        return false;
    }
    final GrIfStatement ifStatement = (GrIfStatement) element;
    if (ErrorUtil.containsError(ifStatement)) {
        return false;
    }
    GrStatement thenBranch = ifStatement.getThenBranch();
    if (thenBranch == null) {
        return false;
    }
    thenBranch = ConditionalUtils.stripBraces(thenBranch);
    if (!(thenBranch instanceof GrIfStatement)) {
        return false;
    }
    GrStatement elseBranch = ifStatement.getElseBranch();
    if (elseBranch != null) {
        elseBranch = ConditionalUtils.stripBraces(elseBranch);
        if (elseBranch != null) {
            return false;
        }
    }
    final GrIfStatement childIfStatement = (GrIfStatement) thenBranch;
    return childIfStatement.getElseBranch() == null;
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 9 with GrIfStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement 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 10 with GrIfStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement 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

GrIfStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement)27 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)17 PsiElement (com.intellij.psi.PsiElement)11 GrBlockStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement)6 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)6 TextRange (com.intellij.openapi.util.TextRange)4 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)4 NotNull (org.jetbrains.annotations.NotNull)3 PsiElementPredicate (org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate)3 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)3 Document (com.intellij.openapi.editor.Document)2 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)2 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)2 GrConditionalExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrConditionalExpression)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 NonNls (org.jetbrains.annotations.NonNls)1 Nullable (org.jetbrains.annotations.Nullable)1 GrControlFlowOwner (org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner)1 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)1 GrCondition (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrCondition)1