Search in sources :

Example 21 with GrBinaryExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression 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)

Example 22 with GrBinaryExpression

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

the class SpockUtils method splitOr.

// See org.spockframework.compiler.WhereBlockRewriter#splitRow()
private static void splitOr(List<GrExpression> res, GrExpression element) {
    if (isOrStatement(element)) {
        GrBinaryExpression be = (GrBinaryExpression) element;
        splitOr(res, be.getLeftOperand());
        splitOr(res, be.getRightOperand());
    } else {
        res.add(element);
    }
}
Also used : GrBinaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression)

Example 23 with GrBinaryExpression

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

the class ConvertConcatenationToGstringIntention method getOperandText.

private static void getOperandText(@Nullable GrExpression operand, StringBuilder builder, boolean multiline) {
    if (operand instanceof GrRegex) {
        for (GroovyPsiElement element : ((GrRegex) operand).getAllContentParts()) {
            if (element instanceof GrStringInjection) {
                builder.append(element.getText());
            } else if (element instanceof GrStringContent) {
                if (GrStringUtil.isDollarSlashyString((GrLiteral) operand)) {
                    processDollarSlashyContent(builder, multiline, element.getText());
                } else {
                    processSlashyContent(builder, multiline, element.getText());
                }
            }
        }
    } else if (operand instanceof GrString) {
        boolean isMultiline = GrStringUtil.isMultilineStringLiteral((GrLiteral) operand);
        for (GroovyPsiElement element : ((GrString) operand).getAllContentParts()) {
            if (element instanceof GrStringInjection) {
                builder.append(element.getText());
            } else if (element instanceof GrStringContent) {
                if (isMultiline) {
                    processMultilineGString(builder, element.getText());
                } else {
                    processSinglelineGString(builder, element.getText());
                }
            }
        }
    } else if (operand instanceof GrLiteral) {
        String text = GrStringUtil.removeQuotes(operand.getText());
        GrLiteral literal = (GrLiteral) operand;
        if (GrStringUtil.isSingleQuoteString(literal)) {
            processSinglelineString(builder, text);
        } else if (GrStringUtil.isTripleQuoteString(literal)) {
            processMultilineString(builder, text);
        } else if (GrStringUtil.isDoubleQuoteString(literal)) {
            processSinglelineGString(builder, text);
        } else if (GrStringUtil.isTripleDoubleQuoteString(literal)) {
            processMultilineGString(builder, text);
        } else if (GrStringUtil.isSlashyString(literal)) {
            processSlashyContent(builder, multiline, text);
        } else if (GrStringUtil.isDollarSlashyString(literal)) {
            processDollarSlashyContent(builder, multiline, text);
        }
    } else if (MyPredicate.satisfied(operand)) {
        performIntention((GrBinaryExpression) operand, builder, multiline);
    } else if (isToStringMethod(operand, builder)) {
    //nothing to do
    } else {
        builder.append(START_BRACE).append(operand == null ? "" : operand.getText()).append(END_BRACE);
    }
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrBinaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression)

Example 24 with GrBinaryExpression

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

the class ConvertConcatenationToGstringIntention method invokeImpl.

private static void invokeImpl(final PsiElement element, Document document) {
    boolean isMultiline = containsMultilineStrings((GrExpression) element);
    StringBuilder builder = new StringBuilder(element.getTextLength());
    if (element instanceof GrBinaryExpression) {
        performIntention((GrBinaryExpression) element, builder, isMultiline);
    } else if (element instanceof GrLiteral) {
        getOperandText((GrExpression) element, builder, isMultiline);
    } else {
        return;
    }
    String text = builder.toString();
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(element.getProject());
    final GrExpression newExpr = factory.createExpressionFromText(GrStringUtil.addQuotes(text, true));
    CommandProcessor.getInstance().executeCommand(element.getProject(), () -> WriteAction.run(() -> {
        final GrExpression expression = ((GrExpression) element).replaceWithExpression(newExpr, true);
        if (expression instanceof GrString) {
            GrStringUtil.removeUnnecessaryBracesInGString((GrString) expression);
        }
    }), null, null, document);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrBinaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression)

Example 25 with GrBinaryExpression

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

the class ConvertGStringToStringIntention method prepareExpression.

private static String prepareExpression(GrExpression expr) {
    if (PsiUtil.isThisOrSuperRef(expr))
        return expr.getText();
    String text = expr.getText();
    final PsiType type = expr.getType();
    if (type != null && CommonClassNames.JAVA_LANG_STRING.equals(type.getCanonicalText())) {
        if (expr instanceof GrBinaryExpression && GroovyTokenTypes.mPLUS.equals(((GrBinaryExpression) expr).getOperationTokenType())) {
            return '(' + text + ')';
        } else {
            return text;
        }
    } else {
        return "String.valueOf(" + text + ")";
    }
}
Also used : GrBinaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression) PsiType(com.intellij.psi.PsiType)

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