Search in sources :

Example 6 with PsiPolyadicExpression

use of com.intellij.psi.PsiPolyadicExpression in project intellij-community by JetBrains.

the class ReplaceExpressionUtil method isNeedParenthesis.

public static boolean isNeedParenthesis(ASTNode oldExpr, ASTNode newExpr) {
    final ASTNode oldParent = oldExpr.getTreeParent();
    if (!ElementType.EXPRESSION_BIT_SET.contains(oldParent.getElementType()))
        return false;
    int priority = getExpressionPriority(newExpr);
    int parentPriority = getExpressionPriority(oldParent);
    if (priority > parentPriority)
        return false;
    IElementType i = oldParent.getElementType();
    if (i == JavaElementType.ASSIGNMENT_EXPRESSION) {
        return priority < parentPriority || ((CompositeElement) oldParent).getChildRole(oldExpr) == ChildRole.LOPERAND;
    } else if (i == JavaElementType.CONDITIONAL_EXPRESSION) {
        int role = ((CompositeElement) oldParent).getChildRole(oldExpr);
        if (role == ChildRole.THEN_EXPRESSION)
            return false;
        return priority < parentPriority || role != ChildRole.ELSE_EXPRESSION;
    } else if (i == JavaElementType.BINARY_EXPRESSION || i == JavaElementType.POLYADIC_EXPRESSION) {
        if (priority < parentPriority)
            return true;
        PsiElement element = SourceTreeToPsiMap.treeElementToPsi(oldParent);
        IElementType opType = ((PsiPolyadicExpression) element).getOperationTokenType();
        return ((CompositeElement) oldParent).getChildRole(oldExpr) != ChildRole.LOPERAND && opType != JavaTokenType.PLUS && opType != JavaTokenType.ASTERISK && opType != JavaTokenType.ANDAND && opType != JavaTokenType.OROR;
    } else if (i == JavaElementType.INSTANCE_OF_EXPRESSION) {
        return priority < parentPriority;
    } else if (i == JavaElementType.PREFIX_EXPRESSION || i == JavaElementType.TYPE_CAST_EXPRESSION) {
        return priority < parentPriority;
    } else if (i == JavaElementType.POSTFIX_EXPRESSION) {
        return priority <= parentPriority;
    } else if (i == JavaElementType.REFERENCE_EXPRESSION || i == JavaElementType.METHOD_REF_EXPRESSION) {
        return priority < parentPriority;
    } else if (i == JavaElementType.METHOD_CALL_EXPRESSION) {
        return false;
    } else if (i == JavaElementType.NEW_EXPRESSION) {
        return false;
    } else if (i == JavaElementType.ARRAY_ACCESS_EXPRESSION) {
        int role = ((CompositeElement) oldParent).getChildRole(oldExpr);
        return role != ChildRole.ARRAY_DIMENSION && role != ChildRole.INDEX && priority < parentPriority;
    } else if (i == JavaElementType.ARRAY_INITIALIZER_EXPRESSION) {
        return false;
    } else if (i == JavaElementType.PARENTH_EXPRESSION) {
        return false;
    } else if (i == JavaElementType.LITERAL_EXPRESSION || i == JavaElementType.THIS_EXPRESSION || i == JavaElementType.SUPER_EXPRESSION || i == JavaElementType.CLASS_OBJECT_ACCESS_EXPRESSION || i == JavaElementType.LAMBDA_EXPRESSION) {
        return false;
    }
    LOG.assertTrue(false);
    return false;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiPolyadicExpression(com.intellij.psi.PsiPolyadicExpression) ASTNode(com.intellij.lang.ASTNode) CompositeElement(com.intellij.psi.impl.source.tree.CompositeElement) PsiElement(com.intellij.psi.PsiElement)

Example 7 with PsiPolyadicExpression

use of com.intellij.psi.PsiPolyadicExpression in project intellij-community by JetBrains.

the class ConjunctionPredicate method satisfiedBy.

public boolean satisfiedBy(PsiElement element) {
    if (!(element instanceof PsiPolyadicExpression)) {
        return false;
    }
    final PsiPolyadicExpression expression = (PsiPolyadicExpression) element;
    final IElementType tokenType = expression.getOperationTokenType();
    if (!tokenType.equals(JavaTokenType.ANDAND) && !tokenType.equals(JavaTokenType.OROR)) {
        return false;
    }
    return !ErrorUtil.containsError(element);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiPolyadicExpression(com.intellij.psi.PsiPolyadicExpression)

Example 8 with PsiPolyadicExpression

use of com.intellij.psi.PsiPolyadicExpression in project intellij-community by JetBrains.

the class ConstantSubexpressionIntention method getTextForElement.

@Override
protected String getTextForElement(PsiElement element) {
    final PsiJavaToken token;
    if (element instanceof PsiJavaToken) {
        token = (PsiJavaToken) element;
    } else {
        final PsiElement prevSibling = element.getPrevSibling();
        if (prevSibling instanceof PsiJavaToken) {
            token = (PsiJavaToken) prevSibling;
        } else {
            throw new AssertionError();
        }
    }
    final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression) element.getParent();
    final PsiPolyadicExpression subexpression = ConstantSubexpressionPredicate.getSubexpression(polyadicExpression, token);
    final String text = HighlightUtil.getPresentableText(subexpression);
    return IntentionPowerPackBundle.message("constant.expression.intention.name", text);
}
Also used : PsiJavaToken(com.intellij.psi.PsiJavaToken) PsiPolyadicExpression(com.intellij.psi.PsiPolyadicExpression) PsiElement(com.intellij.psi.PsiElement)

Example 9 with PsiPolyadicExpression

use of com.intellij.psi.PsiPolyadicExpression in project intellij-community by JetBrains.

the class ExpressionPredicate method satisfiedBy.

public boolean satisfiedBy(PsiElement element) {
    if (!(element instanceof PsiJavaToken)) {
        return false;
    }
    final PsiElement parent = element.getParent();
    if (!(parent instanceof PsiPolyadicExpression)) {
        return false;
    }
    final PsiPolyadicExpression expression = (PsiPolyadicExpression) parent;
    final PsiExpression[] operands = expression.getOperands();
    if (operands.length < 2) {
        return false;
    }
    PsiExpression prevOperand = null;
    for (PsiExpression operand : operands) {
        final PsiJavaToken token = expression.getTokenBeforeOperand(operand);
        if (element == token) {
            if (prevOperand == null || operand.getText().equals(prevOperand.getText())) {
                return false;
            }
            break;
        }
        prevOperand = operand;
    }
    return !ComparisonUtils.isComparison(expression);
}
Also used : PsiJavaToken(com.intellij.psi.PsiJavaToken) PsiExpression(com.intellij.psi.PsiExpression) PsiPolyadicExpression(com.intellij.psi.PsiPolyadicExpression) PsiElement(com.intellij.psi.PsiElement)

Example 10 with PsiPolyadicExpression

use of com.intellij.psi.PsiPolyadicExpression in project intellij-community by JetBrains.

the class FlipExpressionIntention method getTextForElement.

@Override
public String getTextForElement(PsiElement element) {
    final PsiPolyadicExpression expression = (PsiPolyadicExpression) element.getParent();
    final PsiExpression[] operands = expression.getOperands();
    final PsiJavaToken sign = expression.getTokenBeforeOperand(operands[1]);
    final String operatorText = sign == null ? "" : sign.getText();
    final IElementType tokenType = expression.getOperationTokenType();
    final boolean commutative = ParenthesesUtils.isCommutativeOperator(tokenType);
    if (commutative && !ExpressionUtils.isConcatenation(expression)) {
        return IntentionPowerPackBundle.message("flip.smth.intention.name", operatorText);
    } else {
        return IntentionPowerPackBundle.message("flip.smth.intention.name1", operatorText);
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiJavaToken(com.intellij.psi.PsiJavaToken) PsiExpression(com.intellij.psi.PsiExpression) PsiPolyadicExpression(com.intellij.psi.PsiPolyadicExpression)

Aggregations

PsiPolyadicExpression (com.intellij.psi.PsiPolyadicExpression)13 PsiElement (com.intellij.psi.PsiElement)6 PsiExpression (com.intellij.psi.PsiExpression)6 PsiJavaToken (com.intellij.psi.PsiJavaToken)5 IElementType (com.intellij.psi.tree.IElementType)5 ASTNode (com.intellij.lang.ASTNode)2 Wrap (com.intellij.formatting.Wrap)1 PsiMethod (com.intellij.psi.PsiMethod)1 CompositeElement (com.intellij.psi.impl.source.tree.CompositeElement)1 InspectionGadgetsFix (com.siyeh.ig.InspectionGadgetsFix)1 SuppressForTestsScopeFix (com.siyeh.ig.fixes.SuppressForTestsScopeFix)1 NonNls (org.jetbrains.annotations.NonNls)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1