Search in sources :

Example 81 with GrStatement

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

the class GroovyTrivialIfInspection method isSimplifiableAssignmentNegated.

public static boolean isSimplifiableAssignmentNegated(GrIfStatement ifStatement) {
    GrStatement thenBranch = ifStatement.getThenBranch();
    thenBranch = ConditionalUtils.stripBraces(thenBranch);
    GrStatement elseBranch = ifStatement.getElseBranch();
    elseBranch = ConditionalUtils.stripBraces(elseBranch);
    if (ConditionalUtils.isAssignment(thenBranch, "false") && ConditionalUtils.isAssignment(elseBranch, "true")) {
        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 82 with GrStatement

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

the class GroovyTrivialIfInspection method isSimplifiableImplicitReturn.

public static boolean isSimplifiableImplicitReturn(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, "true") && ConditionalUtils.isReturn(elseBranch, "false");
}
Also used : PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 83 with GrStatement

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

the class GroovyTrivialIfInspection method isSimplifiableImplicitAssignmentNegated.

public static boolean isSimplifiableImplicitAssignmentNegated(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, "false") && ConditionalUtils.isAssignment(elseBranch, "true")) {
        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 84 with GrStatement

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

the class GroovyFix method replaceStatement.

protected static void replaceStatement(GrStatement statement, String newStatement) {
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(statement.getProject());
    final GrStatement newCall = (GrStatement) factory.createTopElementFromText(newStatement);
    statement.replaceWithStatement(newCall);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 85 with GrStatement

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

the class GrBlockImpl method deleteChildInternal.

@Override
public void deleteChildInternal(@NotNull ASTNode child) {
    final PsiElement element = child.getPsi();
    if (element instanceof GrStatement) {
        PsiImplUtil.deleteStatementTail(this, element);
    }
    super.deleteChildInternal(child);
}
Also used : PsiElement(com.intellij.psi.PsiElement) LazyParseablePsiElement(com.intellij.psi.impl.source.tree.LazyParseablePsiElement) ASTDelegatePsiElement(com.intellij.extapi.psi.ASTDelegatePsiElement) 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