use of com.intellij.psi.PsiPolyadicExpression in project intellij-community by JetBrains.
the class ConstantSubexpressionIntention method processIntention.
@Override
public void processIntention(@NotNull PsiElement element) throws IncorrectOperationException {
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);
if (subexpression == null) {
return;
}
final Object value = ExpressionUtils.computeConstantExpression(subexpression);
@NonNls final StringBuilder newExpressionText = new StringBuilder();
final PsiExpression[] operands = polyadicExpression.getOperands();
PsiExpression prevOperand = null;
PsiJavaToken prevToken = null;
for (PsiExpression operand : operands) {
final PsiJavaToken currentToken = polyadicExpression.getTokenBeforeOperand(operand);
if (token == currentToken) {
if (prevToken != null) {
newExpressionText.append(prevToken.getText());
}
if (newExpressionText.length() > 0) {
newExpressionText.append(' ');
}
if (value instanceof Long) {
newExpressionText.append(value).append('L');
} else if (value instanceof Double) {
final double v = ((Double) value).doubleValue();
if (Double.isNaN(v)) {
newExpressionText.append("java.lang.Double.NaN");
} else if (Double.isInfinite(v)) {
if (v > 0.0) {
newExpressionText.append("java.lang.Double.POSITIVE_INFINITY");
} else {
newExpressionText.append("java.lang.Double.NEGATIVE_INFINITY");
}
} else {
newExpressionText.append(Double.toString(v));
}
} else if (value instanceof Float) {
final float v = ((Float) value).floatValue();
if (Float.isNaN(v)) {
newExpressionText.append("java.lang.Float.NaN");
} else if (Float.isInfinite(v)) {
if (v > 0.0F) {
newExpressionText.append("java.lang.Float.POSITIVE_INFINITY");
} else {
newExpressionText.append("java.lang.Float.NEGATIVE_INFINITY");
}
} else {
newExpressionText.append(Float.toString(v)).append('f');
}
} else {
newExpressionText.append(value);
}
prevOperand = null;
prevToken = null;
} else {
if (prevToken != null) {
newExpressionText.append(prevToken.getText());
}
if (prevOperand != null) {
newExpressionText.append(prevOperand.getText());
}
prevOperand = operand;
prevToken = currentToken;
}
}
if (prevToken != null) {
newExpressionText.append(prevToken.getText());
}
if (prevOperand != null) {
newExpressionText.append(prevOperand.getText());
}
PsiReplacementUtil.replaceExpression(polyadicExpression, newExpressionText.toString());
}
use of com.intellij.psi.PsiPolyadicExpression in project intellij-community by JetBrains.
the class FlipExpressionIntention method processIntention.
@Override
public void processIntention(@NotNull PsiElement element) {
final PsiJavaToken token = (PsiJavaToken) element;
final PsiElement parent = token.getParent();
if (!(parent instanceof PsiPolyadicExpression)) {
return;
}
final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression) parent;
final PsiExpression[] operands = polyadicExpression.getOperands();
final StringBuilder newExpression = new StringBuilder();
String prevOperand = null;
// 2- -1 without the space is not legal
final String tokenText = token.getText() + ' ';
for (PsiExpression operand : operands) {
final PsiJavaToken token1 = polyadicExpression.getTokenBeforeOperand(operand);
if (token == token1) {
newExpression.append(operand.getText()).append(tokenText);
continue;
}
if (prevOperand != null) {
newExpression.append(prevOperand).append(tokenText);
}
prevOperand = operand.getText();
}
newExpression.append(prevOperand);
PsiReplacementUtil.replaceExpression(polyadicExpression, newExpression.toString());
}
use of com.intellij.psi.PsiPolyadicExpression in project intellij-community by JetBrains.
the class JavaPolyadicExpressionUnwrapper method isApplicableTo.
@Override
public boolean isApplicableTo(PsiElement e) {
if (!(e.getParent() instanceof PsiPolyadicExpression)) {
return false;
}
final PsiPolyadicExpression expression = (PsiPolyadicExpression) e.getParent();
final PsiExpression operand = findOperand(e, expression);
return operand != null;
}
use of com.intellij.psi.PsiPolyadicExpression in project intellij-community by JetBrains.
the class JavaChildBlockWrapFactory method create.
/**
* Creates {@link Wrap wrap} to be used with the children blocks of the the given block.
*
* @param block target block which sub-blocks should use wrap created by the current method
* @param settings code formatting settings to consider during wrap construction
* @param reservedWrapsProvider reserved {@code 'element type -> wrap instance'} mappings provider. <b>Note:</b> this
* argument is considered to be a part of legacy heritage and is intended to be removed as
* soon as formatting code refactoring is done
* @return wrap to use for the sub-blocks of the given block
*/
@Nullable
public Wrap create(ASTBlock block, CommonCodeStyleSettings settings, ReservedWrapsProvider reservedWrapsProvider) {
ASTNode node = block.getNode();
Wrap wrap = block.getWrap();
final IElementType nodeType = node.getElementType();
if (nodeType == JavaElementType.EXTENDS_LIST || nodeType == JavaElementType.IMPLEMENTS_LIST) {
return Wrap.createWrap(settings.EXTENDS_LIST_WRAP, false);
} else if (node instanceof PsiPolyadicExpression) {
Wrap actualWrap = wrap != null ? wrap : reservedWrapsProvider.getReservedWrap(JavaElementType.BINARY_EXPRESSION);
if (actualWrap == null) {
return Wrap.createWrap(settings.BINARY_OPERATION_WRAP, false);
} else {
if (JavaFormatterUtil.areSamePriorityBinaryExpressions(node, node.getTreeParent())) {
return actualWrap;
} else {
return Wrap.createChildWrap(actualWrap, WrapType.byLegacyRepresentation(settings.BINARY_OPERATION_WRAP), false);
}
}
} else if (nodeType == JavaElementType.CONDITIONAL_EXPRESSION) {
return Wrap.createWrap(settings.TERNARY_OPERATION_WRAP, false);
} else if (nodeType == JavaElementType.ASSERT_STATEMENT) {
return Wrap.createWrap(settings.ASSERT_STATEMENT_WRAP, false);
} else if (nodeType == JavaElementType.FOR_STATEMENT) {
return Wrap.createWrap(settings.FOR_STATEMENT_WRAP, false);
} else if (nodeType == JavaElementType.THROWS_LIST) {
return Wrap.createWrap(settings.THROWS_LIST_WRAP, true);
} else if (nodeType == JavaElementType.CODE_BLOCK) {
if (settings.KEEP_SIMPLE_METHODS_IN_ONE_LINE && node.getPsi().getParent() instanceof PsiMethod && !node.textContains('\n')) {
return null;
}
return Wrap.createWrap(WrapType.NORMAL, false);
} else if (JavaFormatterUtil.isAssignment(node)) {
return Wrap.createWrap(settings.ASSIGNMENT_WRAP, true);
} else {
return null;
}
}
use of com.intellij.psi.PsiPolyadicExpression in project intellij-community by JetBrains.
the class ReplaceExpressionUtil method getExpressionPriority.
private static int getExpressionPriority(ASTNode expr) {
IElementType i = expr.getElementType();
if (i == JavaElementType.ASSIGNMENT_EXPRESSION) {
return 0;
} else if (i == JavaElementType.CONDITIONAL_EXPRESSION) {
return 1;
} else if (i == JavaElementType.BINARY_EXPRESSION || i == JavaElementType.POLYADIC_EXPRESSION) {
PsiElement element = SourceTreeToPsiMap.treeElementToPsi(expr);
IElementType opType = ((PsiPolyadicExpression) element).getOperationTokenType();
if (opType == JavaTokenType.OROR) {
return 2;
} else if (opType == JavaTokenType.ANDAND) {
return 3;
} else if (opType == JavaTokenType.OR) {
return 4;
} else if (opType == JavaTokenType.XOR) {
return 5;
} else if (opType == JavaTokenType.AND) {
return 6;
} else if (opType == JavaTokenType.EQEQ || opType == JavaTokenType.NE) {
return 7;
} else if (opType == JavaTokenType.LT || opType == JavaTokenType.GT || opType == JavaTokenType.LE || opType == JavaTokenType.GE) {
return 8;
} else if (opType == JavaTokenType.LTLT || opType == JavaTokenType.GTGT || opType == JavaTokenType.GTGTGT) {
return 9;
} else if (opType == JavaTokenType.PLUS || opType == JavaTokenType.MINUS) {
return 10;
} else if (opType == JavaTokenType.ASTERISK || opType == JavaTokenType.DIV || opType == JavaTokenType.PERC) {
return 11;
}
return 8;
} else if (i == JavaElementType.INSTANCE_OF_EXPRESSION) {
return 8;
} else if (i == JavaElementType.PREFIX_EXPRESSION || i == JavaElementType.TYPE_CAST_EXPRESSION) {
return 12;
} else if (i == JavaElementType.POSTFIX_EXPRESSION) {
return 13;
} else if (i == JavaElementType.LITERAL_EXPRESSION || i == JavaElementType.REFERENCE_EXPRESSION || i == JavaElementType.THIS_EXPRESSION || i == JavaElementType.SUPER_EXPRESSION || i == JavaElementType.PARENTH_EXPRESSION || i == JavaElementType.METHOD_CALL_EXPRESSION || i == JavaElementType.CLASS_OBJECT_ACCESS_EXPRESSION || i == JavaElementType.NEW_EXPRESSION || i == JavaElementType.ARRAY_ACCESS_EXPRESSION || i == JavaElementType.ARRAY_INITIALIZER_EXPRESSION || i == JavaElementType.JAVA_CODE_REFERENCE || i == JavaElementType.METHOD_REF_EXPRESSION || i == JavaElementType.LAMBDA_EXPRESSION || i == JavaElementType.EMPTY_EXPRESSION) {
return 14;
} else {
LOG.error("Unknown element type:" + i);
return -1;
}
}
Aggregations