Search in sources :

Example 6 with GrParenthesizedExpression

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

the class ParenthesisExprSurrounder method surroundExpression.

@Override
protected TextRange surroundExpression(@NotNull GrExpression expression, PsiElement context) {
    GrParenthesizedExpression result = (GrParenthesizedExpression) GroovyPsiElementFactory.getInstance(expression.getProject()).createExpressionFromText("(a)", context);
    replaceToOldExpression(result.getOperand(), expression);
    result = (GrParenthesizedExpression) expression.replaceWithExpression(result, true);
    return new TextRange(result.getTextRange().getEndOffset(), result.getTextRange().getEndOffset());
}
Also used : GrParenthesizedExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrParenthesizedExpression) TextRange(com.intellij.openapi.util.TextRange)

Example 7 with GrParenthesizedExpression

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

the class BoolUtils method getNegatedExpressionText.

public static String getNegatedExpressionText(@NotNull GrExpression condition) {
    if (condition instanceof GrParenthesizedExpression) {
        final GrExpression contentExpression = ((GrParenthesizedExpression) condition).getOperand();
        if (contentExpression == null)
            return "()";
        return '(' + getNegatedExpressionText(contentExpression) + ')';
    } else if (isNegation(condition)) {
        final GrExpression negated = getNegated(condition);
        return negated.getText();
    } else if (ComparisonUtils.isComparison(condition)) {
        final GrBinaryExpression binaryExpression = (GrBinaryExpression) condition;
        final IElementType sign = binaryExpression.getOperationTokenType();
        final String negatedComparison = ComparisonUtils.getNegatedComparison(sign);
        final GrExpression lhs = binaryExpression.getLeftOperand();
        final GrExpression rhs = binaryExpression.getRightOperand();
        assert rhs != null;
        return lhs.getText() + negatedComparison + rhs.getText();
    } else if (ParenthesesUtils.getPrecedence(condition) > ParenthesesUtils.PREFIX_PRECEDENCE) {
        return "!(" + condition.getText() + ')';
    } else {
        return '!' + condition.getText();
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GrParenthesizedExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrParenthesizedExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrBinaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression)

Aggregations

GrParenthesizedExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrParenthesizedExpression)7 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)4 IElementType (com.intellij.psi.tree.IElementType)3 TextRange (com.intellij.openapi.util.TextRange)2 GrUnaryExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrUnaryExpression)2 ASTNode (com.intellij.lang.ASTNode)1 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1 GrBinaryExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression)1 GrTypeCastExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrTypeCastExpression)1 GrTypeElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement)1