Search in sources :

Example 41 with GrStatement

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

the class GroovyTrivialIfInspection method isSimplifiableImplicitAssignment.

public static boolean isSimplifiableImplicitAssignment(GrIfStatement ifStatement) {
    if (ifStatement.getElseBranch() != null) {
        return false;
    }
    GrStatement thenBranch = ifStatement.getThenBranch();
    thenBranch = ConditionalUtils.stripBraces(thenBranch);
    final PsiElement nextStatement = PsiTreeUtil.skipSiblingsBackward(ifStatement, PsiWhiteSpace.class);
    if (!(nextStatement instanceof GrStatement)) {
        return false;
    }
    GrStatement elseBranch = (GrStatement) nextStatement;
    elseBranch = ConditionalUtils.stripBraces(elseBranch);
    if (ConditionalUtils.isAssignment(thenBranch, "true") && ConditionalUtils.isAssignment(elseBranch, "false")) {
        final GrAssignmentExpression thenExpression = (GrAssignmentExpression) thenBranch;
        final GrAssignmentExpression elseExpression = (GrAssignmentExpression) elseBranch;
        final IElementType thenSign = thenExpression.getOperationTokenType();
        final IElementType elseSign = elseExpression.getOperationTokenType();
        if (!thenSign.equals(elseSign)) {
            return false;
        }
        final GrExpression thenLhs = thenExpression.getLValue();
        final GrExpression elseLhs = elseExpression.getLValue();
        return EquivalenceChecker.expressionsAreEquivalent(thenLhs, elseLhs);
    } else {
        return false;
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 42 with GrStatement

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

the class GroovyTrivialIfInspection method isSimplifiableImplicitReturnNegated.

public static boolean isSimplifiableImplicitReturnNegated(GrIfStatement ifStatement) {
    if (ifStatement.getElseBranch() != null) {
        return false;
    }
    GrStatement thenBranch = ifStatement.getThenBranch();
    thenBranch = ConditionalUtils.stripBraces(thenBranch);
    final PsiElement nextStatement = PsiTreeUtil.skipSiblingsForward(ifStatement, PsiWhiteSpace.class);
    if (!(nextStatement instanceof GrStatement)) {
        return false;
    }
    final GrStatement elseBranch = (GrStatement) nextStatement;
    return ConditionalUtils.isReturn(thenBranch, "false") && ConditionalUtils.isReturn(elseBranch, "true");
}
Also used : PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 43 with GrStatement

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

the class GroovyTrivialIfInspection method isSimplifiableReturn.

public static boolean isSimplifiableReturn(GrIfStatement ifStatement) {
    GrStatement thenBranch = ifStatement.getThenBranch();
    thenBranch = ConditionalUtils.stripBraces(thenBranch);
    GrStatement elseBranch = ifStatement.getElseBranch();
    elseBranch = ConditionalUtils.stripBraces(elseBranch);
    return ConditionalUtils.isReturn(thenBranch, "true") && ConditionalUtils.isReturn(elseBranch, "false");
}
Also used : GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 44 with GrStatement

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

the class GroovyTrivialIfInspection method isSimplifiableAssignment.

public static boolean isSimplifiableAssignment(GrIfStatement ifStatement) {
    GrStatement thenBranch = ifStatement.getThenBranch();
    thenBranch = ConditionalUtils.stripBraces(thenBranch);
    GrStatement elseBranch = ifStatement.getElseBranch();
    elseBranch = ConditionalUtils.stripBraces(elseBranch);
    if (ConditionalUtils.isAssignment(thenBranch, "true") && ConditionalUtils.isAssignment(elseBranch, "false")) {
        final GrAssignmentExpression thenExpression = (GrAssignmentExpression) thenBranch;
        final GrAssignmentExpression elseExpression = (GrAssignmentExpression) elseBranch;
        final IElementType thenSign = thenExpression.getOperationTokenType();
        final IElementType elseSign = elseExpression.getOperationTokenType();
        if (!thenSign.equals(elseSign)) {
            return false;
        }
        final GrExpression thenLhs = thenExpression.getLValue();
        final GrExpression elseLhs = elseExpression.getLValue();
        return EquivalenceChecker.expressionsAreEquivalent(thenLhs, elseLhs);
    } else {
        return false;
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 45 with GrStatement

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

the class GroovyTrivialIfInspection method isSimplifiableReturnNegated.

public static boolean isSimplifiableReturnNegated(GrIfStatement ifStatement) {
    GrStatement thenBranch = ifStatement.getThenBranch();
    thenBranch = ConditionalUtils.stripBraces(thenBranch);
    GrStatement elseBranch = ifStatement.getElseBranch();
    elseBranch = ConditionalUtils.stripBraces(elseBranch);
    return ConditionalUtils.isReturn(thenBranch, "false") && ConditionalUtils.isReturn(elseBranch, "true");
}
Also used : GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Aggregations

GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)113 PsiElement (com.intellij.psi.PsiElement)36 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)26 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)22 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)21 GrIfStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement)17 TextRange (com.intellij.openapi.util.TextRange)14 Nullable (org.jetbrains.annotations.Nullable)14 GrBlockStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement)13 NotNull (org.jetbrains.annotations.NotNull)12 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)12 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)12 GrStatementOwner (org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)11 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)10 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)10 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)9 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)9 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)8 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)8 Document (com.intellij.openapi.editor.Document)6