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;
}
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);
}
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);
}
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);
}
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);
}
}
Aggregations