Search in sources :

Example 6 with GrBinaryExpression

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

the class NegateComparisonIntention method getTextForElement.

@Override
protected String getTextForElement(PsiElement element) {
    final GrBinaryExpression binaryExpression = (GrBinaryExpression) element;
    final IElementType tokenType = binaryExpression.getOperationTokenType();
    final String comparison = ComparisonUtils.getStringForComparison(tokenType);
    final String negatedComparison = ComparisonUtils.getNegatedComparison(tokenType);
    return GroovyIntentionsBundle.message("negate.comparison.intention.name", comparison, negatedComparison);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GrBinaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression)

Example 7 with GrBinaryExpression

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

the class SplitIfIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement andElement, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    GrBinaryExpression binaryExpression = (GrBinaryExpression) andElement.getParent();
    GrIfStatement ifStatement = (GrIfStatement) binaryExpression.getParent();
    GrExpression leftOperand = binaryExpression.getLeftOperand();
    GrExpression rightOperand = binaryExpression.getRightOperand();
    GrStatement thenBranch = ifStatement.getThenBranch();
    assert thenBranch != null;
    assert rightOperand != null;
    GrStatement newSplittedIfs = GroovyPsiElementFactory.getInstance(project).createStatementFromText("if(" + leftOperand.getText() + ") { \n" + "  if(" + rightOperand.getText() + ")" + thenBranch.getText() + "\n" + "}");
    ifStatement.replaceWithStatement(newSplittedIfs);
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrBinaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 8 with GrBinaryExpression

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

the class GroovyBlock method getChildAttributes.

@Override
@NotNull
public ChildAttributes getChildAttributes(final int newChildIndex) {
    ASTNode astNode = getNode();
    final PsiElement psiParent = astNode.getPsi();
    if (psiParent instanceof GroovyFileBase) {
        return new ChildAttributes(Indent.getNoneIndent(), null);
    }
    if (psiParent instanceof GrSwitchStatement) {
        List<Block> subBlocks = getSubBlocks();
        if (newChildIndex > 0) {
            Block block = subBlocks.get(newChildIndex - 1);
            if (block instanceof GroovyBlock) {
                PsiElement anchorPsi = ((GroovyBlock) block).getNode().getPsi();
                if (anchorPsi instanceof GrCaseSection) {
                    for (GrStatement statement : ((GrCaseSection) anchorPsi).getStatements()) {
                        if (statement instanceof GrBreakStatement || statement instanceof GrContinueStatement || statement instanceof GrReturnStatement || statement instanceof GrThrowStatement) {
                            final Indent indent = GroovyIndentProcessor.getSwitchCaseIndent(myContext.getSettings());
                            return new ChildAttributes(indent, null);
                        }
                    }
                    int indentSize = myContext.getSettings().getIndentOptions().INDENT_SIZE;
                    final int spaces = myContext.getSettings().INDENT_CASE_FROM_SWITCH ? 2 * indentSize : indentSize;
                    return new ChildAttributes(Indent.getSpaceIndent(spaces), null);
                }
            }
        }
    }
    if (psiParent instanceof GrCaseLabel) {
        return new ChildAttributes(GroovyIndentProcessor.getSwitchCaseIndent(getContext().getSettings()), null);
    }
    if (psiParent instanceof GrCaseSection) {
        return getSwitchIndent((GrCaseSection) psiParent, newChildIndex);
    }
    if (TokenSets.BLOCK_SET.contains(astNode.getElementType()) || GroovyElementTypes.SWITCH_STATEMENT.equals(astNode.getElementType())) {
        return new ChildAttributes(Indent.getNormalIndent(), null);
    }
    if (GroovyElementTypes.CASE_SECTION.equals(astNode.getElementType())) {
        return new ChildAttributes(Indent.getNormalIndent(), null);
    }
    if (psiParent instanceof GrBinaryExpression || psiParent instanceof GrConditionalExpression || psiParent instanceof GrCommandArgumentList || psiParent instanceof GrArgumentList || psiParent instanceof GrParameterList || psiParent instanceof GrListOrMap || psiParent instanceof GrAnnotationArgumentList || psiParent instanceof GrVariable || psiParent instanceof GrAssignmentExpression) {
        return new ChildAttributes(Indent.getContinuationWithoutFirstIndent(), null);
    }
    if (psiParent instanceof GrDocComment || psiParent instanceof GrDocTag) {
        return new ChildAttributes(Indent.getSpaceIndent(GroovyIndentProcessor.GDOC_COMMENT_INDENT), null);
    }
    if (psiParent instanceof GrIfStatement || psiParent instanceof GrLoopStatement) {
        return new ChildAttributes(Indent.getNormalIndent(), null);
    }
    if (psiParent instanceof GrLabeledStatement && newChildIndex == 2) {
        final Indent indent = getContext().getGroovySettings().INDENT_LABEL_BLOCKS ? Indent.getLabelIndent() : Indent.getNoneIndent();
        return new ChildAttributes(indent, null);
    }
    return new ChildAttributes(Indent.getNoneIndent(), null);
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrCommandArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrBinaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment) GrBreakStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrBreakStatement) GrContinueStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrContinueStatement) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) ASTNode(com.intellij.lang.ASTNode) GrDocTag(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTag) GrCaseLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel) GrThrowStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrThrowStatement) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) GrAnnotationArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArgumentList) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrConditionalExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrConditionalExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with GrBinaryExpression

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

the class GroovyPointlessArithmeticInspection method calculateReplacementExpression.

private static String calculateReplacementExpression(GrExpression expression) {
    final GrBinaryExpression exp = (GrBinaryExpression) expression;
    final IElementType sign = exp.getOperationTokenType();
    final GrExpression lhs = exp.getLeftOperand();
    final GrExpression rhs = exp.getRightOperand();
    assert rhs != null;
    if (GroovyTokenTypes.mPLUS == sign) {
        if (isZero(lhs)) {
            return rhs.getText();
        } else {
            return lhs.getText();
        }
    }
    if (GroovyTokenTypes.mMINUS == sign) {
        return lhs.getText();
    }
    if (GroovyTokenTypes.mSTAR == sign) {
        if (isOne(lhs)) {
            return rhs.getText();
        } else if (isOne(rhs)) {
            return lhs.getText();
        } else {
            return "0";
        }
    }
    if (GroovyTokenTypes.mDIV == sign) {
        return lhs.getText();
    }
    return "";
}
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 10 with GrBinaryExpression

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

the class SpockUtils method createVariableMap.

// See org.spockframework.compiler.WhereBlockRewriter
public static Map<String, SpockVariableDescriptor> createVariableMap(GrMethod method) {
    GrOpenBlock block = method.getBlock();
    if (block == null)
        return Collections.emptyMap();
    PsiElement elementUnderLabel = null;
    PsiElement elementAfterLabel = null;
    main: for (PsiElement e = block.getFirstChild(); e != null; e = e.getNextSibling()) {
        if (e instanceof GrLabeledStatement) {
            GrLabeledStatement l = (GrLabeledStatement) e;
            elementAfterLabel = l.getNextSibling();
            while (true) {
                GrStatement statement = l.getStatement();
                if ("where".equals(l.getName())) {
                    elementUnderLabel = statement;
                    break main;
                }
                if (statement instanceof GrLabeledStatement) {
                    l = (GrLabeledStatement) statement;
                    continue;
                }
                break;
            }
        }
    }
    if (elementUnderLabel == null)
        return Collections.emptyMap();
    Map<String, SpockVariableDescriptor> res = new HashMap<>();
    PsiElement e = elementUnderLabel;
    while (e != null) {
        if (e instanceof GrBinaryExpression && ((GrBinaryExpression) e).getOperationTokenType() == GroovyElementTypes.COMPOSITE_LSHIFT_SIGN) {
            GrBinaryExpression shift = (GrBinaryExpression) e;
            GrExpression leftOperand = shift.getLeftOperand();
            GrExpression rightOperand = shift.getRightOperand();
            if (leftOperand instanceof GrReferenceExpression) {
                String name = getNameByReference(leftOperand);
                if (name != null) {
                    SpockVariableDescriptor descriptor = new SpockVariableDescriptor(leftOperand, name);
                    descriptor.addExpressionOfCollection(rightOperand);
                    res.put(name, descriptor);
                }
            } else if (leftOperand instanceof GrListOrMap) {
                GrExpression[] variableDefinitions = ((GrListOrMap) leftOperand).getInitializers();
                SpockVariableDescriptor[] variables = createVariables(res, Arrays.asList(variableDefinitions));
                if (rightOperand instanceof GrListOrMap) {
                    for (GrExpression expression : ((GrListOrMap) rightOperand).getInitializers()) {
                        if (expression instanceof GrListOrMap) {
                            add(variables, Arrays.asList(((GrListOrMap) expression).getInitializers()));
                        } else {
                            for (SpockVariableDescriptor variable : variables) {
                                if (variable != null) {
                                    variable.addExpressionOfCollection(expression);
                                }
                            }
                        }
                    }
                }
            }
        } else if (e instanceof GrAssignmentExpression) {
            GrAssignmentExpression assExpr = (GrAssignmentExpression) e;
            GrExpression lValue = assExpr.getLValue();
            String name = getNameByReference(lValue);
            if (name != null) {
                res.put(name, new SpockVariableDescriptor(lValue, name).addExpression(assExpr.getRValue()));
            }
        } else if (isOrStatement(e)) {
            // See org.spockframework.compiler.WhereBlockRewriter#rewriteTableLikeParameterization()
            List<GrExpression> variableDefinitions = new ArrayList<>();
            splitOr(variableDefinitions, (GrExpression) e);
            SpockVariableDescriptor[] variables = createVariables(res, variableDefinitions);
            List<GrExpression> row = new ArrayList<>();
            PsiElement rowElement = getNext(e, elementUnderLabel, elementAfterLabel);
            while (isOrStatement(rowElement)) {
                row.clear();
                splitOr(row, (GrExpression) rowElement);
                add(variables, row);
                rowElement = getNext(rowElement, elementUnderLabel, elementAfterLabel);
            }
            e = rowElement;
            continue;
        }
        e = getNext(e, elementUnderLabel, elementAfterLabel);
    }
    return res;
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrBinaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression) GrLabeledStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrLabeledStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) PsiElement(com.intellij.psi.PsiElement)

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