Search in sources :

Example 11 with GrBinaryExpression

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

the class ComparisonUtils method isComparison.

public static boolean isComparison(PsiElement exp) {
    if (!(exp instanceof GrBinaryExpression)) {
        return false;
    }
    final GrBinaryExpression binaryExpression = (GrBinaryExpression) exp;
    final IElementType sign = binaryExpression.getOperationTokenType();
    return s_comparisonStrings.containsKey(sign);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GrBinaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression)

Example 12 with GrBinaryExpression

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

the class StringPartInfo method replaceLiteralWithConcatenation.

@NotNull
public GrExpression replaceLiteralWithConcatenation(@Nullable String varName) {
    String prefix = preparePrefix();
    String suffix = prepareSuffix();
    StringBuilder buffer = new StringBuilder();
    boolean prefixExists = !GrStringUtil.removeQuotes(prefix).isEmpty();
    if (prefixExists) {
        buffer.append(prefix).append('+');
    }
    buffer.append(varName != null ? varName : prepareSelected());
    boolean suffixExists = !GrStringUtil.removeQuotes(suffix).isEmpty();
    if (suffixExists) {
        buffer.append('+').append(suffix);
    }
    final GrExpression concatenation = GroovyPsiElementFactory.getInstance(myLiteral.getProject()).createExpressionFromText(buffer);
    final GrExpression replaced = getLiteral().replaceWithExpression(concatenation, false);
    try {
        if (prefixExists && suffixExists) {
            return ((GrBinaryExpression) ((GrBinaryExpression) replaced).getLeftOperand()).getRightOperand();
        }
        if (!prefixExists && suffixExists) {
            return ((GrBinaryExpression) replaced).getLeftOperand();
        }
        if (prefixExists && !suffixExists) {
            return ((GrBinaryExpression) replaced).getRightOperand();
        }
        if (!prefixExists && !suffixExists) {
            return replaced;
        }
    } catch (ClassCastException c) {
        throw new IncorrectOperationException(buffer.toString());
    }
    throw new IncorrectOperationException(buffer.toString());
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GrBinaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with GrBinaryExpression

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

the class GroovyPointlessBooleanInspection method createStringForNegatedExpression.

private static String createStringForNegatedExpression(GrExpression exp) {
    if (ComparisonUtils.isComparison(exp)) {
        final GrBinaryExpression binaryExpression = (GrBinaryExpression) exp;
        final IElementType sign = binaryExpression.getOperationTokenType();
        final String negatedComparison = ComparisonUtils.getNegatedComparison(sign);
        final GrExpression lhs = binaryExpression.getLeftOperand();
        final GrExpression rhs = binaryExpression.getRightOperand();
        if (rhs == null) {
            return lhs.getText() + negatedComparison;
        }
        return lhs.getText() + negatedComparison + rhs.getText();
    } else {
        final String baseText = exp.getText();
        if (ParenthesesUtils.getPrecedence(exp) > ParenthesesUtils.PREFIX_PRECEDENCE) {
            return "!(" + baseText + ')';
        } else {
            return '!' + baseText;
        }
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrBinaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression)

Example 14 with GrBinaryExpression

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

the class GroovyAssignmentCanBeOperatorAssignmentInspection method calculateReplacementExpression.

static String calculateReplacementExpression(GrAssignmentExpression expression) {
    final GrExpression rhs = expression.getRValue();
    final GrBinaryExpression binaryExpression = (GrBinaryExpression) PsiUtil.skipParentheses(rhs, false);
    final GrExpression lhs = expression.getLValue();
    assert binaryExpression != null;
    final IElementType sign = binaryExpression.getOperationTokenType();
    final GrExpression rhsRhs = binaryExpression.getRightOperand();
    assert rhsRhs != null;
    String signText = getTextForOperator(sign);
    if ("&&".equals(signText)) {
        signText = "&";
    } else if ("||".equals(signText)) {
        signText = "|";
    }
    return lhs.getText() + ' ' + signText + "= " + rhsRhs.getText();
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrBinaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression)

Example 15 with GrBinaryExpression

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

the class DemorgansLawIntention method convertConjunctionExpression.

private static String convertConjunctionExpression(GrBinaryExpression exp, IElementType tokenType) {
    final GrExpression lhs = exp.getLeftOperand();
    final String lhsText;
    if (isConjunctionExpression(lhs, tokenType)) {
        lhsText = convertConjunctionExpression((GrBinaryExpression) lhs, tokenType);
    } else {
        lhsText = convertLeafExpression(lhs);
    }
    final GrExpression rhs = exp.getRightOperand();
    final String rhsText;
    if (isConjunctionExpression(rhs, tokenType)) {
        rhsText = convertConjunctionExpression((GrBinaryExpression) rhs, tokenType);
    } else {
        rhsText = convertLeafExpression(rhs);
    }
    final String flippedConjunction;
    if (tokenType.equals(GroovyTokenTypes.mLAND)) {
        flippedConjunction = "||";
    } else {
        flippedConjunction = "&&";
    }
    return lhsText + flippedConjunction + rhsText;
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrBinaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression)

Aggregations

GrBinaryExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression)26 IElementType (com.intellij.psi.tree.IElementType)15 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)14 NotNull (org.jetbrains.annotations.NotNull)3 PsiElement (com.intellij.psi.PsiElement)2 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)2 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)2 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)2 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)2 ASTNode (com.intellij.lang.ASTNode)1 PsiType (com.intellij.psi.PsiType)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 Nullable (org.jetbrains.annotations.Nullable)1 GrDocComment (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)1 GrDocTag (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTag)1 GroovyFileBase (org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase)1 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)1 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)1 GrAnnotationArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArgumentList)1