Search in sources :

Example 6 with GrUnaryExpression

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

the class BoolUtils method getNegated.

public static GrExpression getNegated(GrExpression exp) {
    final GrUnaryExpression prefixExp = (GrUnaryExpression) exp;
    final GrExpression operand = prefixExp.getOperand();
    return (GrExpression) PsiUtil.skipParentheses(operand, false);
}
Also used : GrUnaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrUnaryExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)

Example 7 with GrUnaryExpression

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

the class BoolUtils method isNegation.

public static boolean isNegation(@Nullable PsiElement exp) {
    if (!(exp instanceof GrUnaryExpression)) {
        return false;
    }
    final GrUnaryExpression prefixExp = (GrUnaryExpression) exp;
    final IElementType sign = prefixExp.getOperationTokenType();
    return GroovyTokenTypes.mLNOT.equals(sign);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GrUnaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrUnaryExpression)

Example 8 with GrUnaryExpression

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

the class BoolUtils method isNegated.

public static boolean isNegated(GrExpression exp) {
    GrExpression ancestor = exp;
    while (ancestor.getParent() instanceof GrParenthesizedExpression) {
        ancestor = (GrExpression) ancestor.getParent();
    }
    if (ancestor.getParent() instanceof GrUnaryExpression) {
        final GrUnaryExpression prefixAncestor = (GrUnaryExpression) ancestor.getParent();
        final IElementType sign = prefixAncestor.getOperationTokenType();
        if (GroovyTokenTypes.mLNOT.equals(sign)) {
            return true;
        }
    }
    return false;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GrParenthesizedExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrParenthesizedExpression) GrUnaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrUnaryExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)

Example 9 with GrUnaryExpression

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

the class BoolUtils method findNegation.

@Nullable
public static GrExpression findNegation(GrExpression exp) {
    GrExpression ancestor = exp;
    while (ancestor.getParent() instanceof GrParenthesizedExpression) {
        ancestor = (GrExpression) ancestor.getParent();
    }
    if (ancestor.getParent() instanceof GrUnaryExpression) {
        final GrUnaryExpression prefixAncestor = (GrUnaryExpression) ancestor.getParent();
        final IElementType sign = prefixAncestor.getOperationTokenType();
        if (GroovyTokenTypes.mLNOT.equals(sign)) {
            return prefixAncestor;
        }
    }
    return null;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GrParenthesizedExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrParenthesizedExpression) GrUnaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrUnaryExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with GrUnaryExpression

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

the class InvertIfIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    PsiElement parent = element.getParent();
    if (!"if".equals(element.getText()) || !(parent instanceof GrIfStatement)) {
        throw new IncorrectOperationException("Not invoked on an if");
    }
    GrIfStatement parentIf = (GrIfStatement) parent;
    GroovyPsiElementFactory groovyPsiElementFactory = GroovyPsiElementFactory.getInstance(project);
    GrExpression condition = parentIf.getCondition();
    if (condition == null) {
        throw new IncorrectOperationException("Invoked on an if with empty condition");
    }
    GrExpression negatedCondition = null;
    if (condition instanceof GrUnaryExpression) {
        GrUnaryExpression unaryCondition = (GrUnaryExpression) condition;
        if ("!".equals(unaryCondition.getOperationToken().getText())) {
            negatedCondition = stripParenthesis(unaryCondition.getOperand());
        }
    }
    if (negatedCondition == null) {
        // Now check whether this is a simple expression
        condition = stripParenthesis(condition);
        String negatedExpressionText;
        if (condition instanceof GrCallExpression || condition instanceof GrReferenceExpression) {
            negatedExpressionText = "!" + condition.getText();
        } else {
            negatedExpressionText = "!(" + condition.getText() + ")";
        }
        negatedCondition = groovyPsiElementFactory.createExpressionFromText(negatedExpressionText, parentIf);
    }
    GrStatement thenBranch = parentIf.getThenBranch();
    final boolean thenIsNotEmpty = isNotEmpty(thenBranch);
    String newIfText = "if (" + negatedCondition.getText() + ") {}";
    if (thenIsNotEmpty) {
        newIfText += " else {}";
    }
    GrIfStatement newIf = (GrIfStatement) groovyPsiElementFactory.createStatementFromText(newIfText, parentIf.getContext());
    generateElseBranchTextAndRemoveTailStatements(parentIf, newIf);
    if (thenIsNotEmpty) {
        final GrStatement elseBranch = newIf.getElseBranch();
        assert elseBranch != null;
        elseBranch.replaceWithStatement(thenBranch);
    }
    parentIf.replace(newIf);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) GrUnaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrUnaryExpression) GrCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Aggregations

GrUnaryExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrUnaryExpression)11 IElementType (com.intellij.psi.tree.IElementType)7 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)6 PsiElement (com.intellij.psi.PsiElement)4 Nullable (org.jetbrains.annotations.Nullable)2 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)2 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)2 GrParenthesizedExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrParenthesizedExpression)2 ASTNode (com.intellij.lang.ASTNode)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiErrorElement (com.intellij.psi.PsiErrorElement)1 CommonCodeStyleSettings (com.intellij.psi.codeStyle.CommonCodeStyleSettings)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 GroovyCodeStyleSettings (org.jetbrains.plugins.groovy.codeStyle.GroovyCodeStyleSettings)1 ClosureBodyBlock (org.jetbrains.plugins.groovy.formatter.blocks.ClosureBodyBlock)1 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)1 GrIfStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement)1 GrConditionalExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrConditionalExpression)1 GrNewExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression)1 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)1