use of org.eclipse.jdt.core.dom.InfixExpression in project che by eclipse.
the class AssociativeInfixExpressionFragment method createFragmentForFullSubtree.
public static IExpressionFragment createFragmentForFullSubtree(InfixExpression node) {
Assert.isNotNull(node);
if (!isAssociativeInfix(node))
return null;
InfixExpression groupRoot = findGroupRoot(node);
Assert.isTrue(isAGroupRoot(groupRoot));
List<Expression> groupMembers = AssociativeInfixExpressionFragment.findGroupMembersInOrderFor(node);
return new AssociativeInfixExpressionFragment(groupRoot, groupMembers);
}
use of org.eclipse.jdt.core.dom.InfixExpression in project flux by eclipse.
the class NecessaryParenthesesChecker method needsParenthesesInInfixExpression.
private static boolean needsParenthesesInInfixExpression(Expression expression, InfixExpression parentInfix, StructuralPropertyDescriptor locationInParent, ITypeBinding leftOperandType) {
InfixExpression.Operator parentInfixOperator = parentInfix.getOperator();
ITypeBinding rightOperandType;
ITypeBinding parentInfixExprType;
if (leftOperandType == null) {
// parentInfix has bindings
leftOperandType = parentInfix.getLeftOperand().resolveTypeBinding();
rightOperandType = parentInfix.getRightOperand().resolveTypeBinding();
parentInfixExprType = parentInfix.resolveTypeBinding();
} else {
rightOperandType = expression.resolveTypeBinding();
parentInfixExprType = getInfixExpressionType(parentInfixOperator, leftOperandType, rightOperandType);
}
boolean isAllOperandsHaveSameType = isAllOperandsHaveSameType(parentInfix, leftOperandType, rightOperandType);
if (locationInParent == InfixExpression.LEFT_OPERAND_PROPERTY) {
//infix expressions are evaluated from left to right -> parentheses not needed
return false;
} else if (isAssociative(parentInfixOperator, parentInfixExprType, isAllOperandsHaveSameType)) {
//left op (right) == (right) op left == right op left
if (expression instanceof InfixExpression) {
InfixExpression infixExpression = (InfixExpression) expression;
Operator operator = infixExpression.getOperator();
if (isStringType(parentInfixExprType)) {
if (parentInfixOperator == InfixExpression.Operator.PLUS && operator == InfixExpression.Operator.PLUS && isStringType(infixExpression.resolveTypeBinding())) {
// "" + (2 + "") == "" + 2 + ""
return !isStringType(infixExpression.getLeftOperand().resolveTypeBinding()) && !isStringType(leftOperandType);
}
//"" + (1 + 2), "" + (1 - 2) etc
return true;
}
if (parentInfixOperator != InfixExpression.Operator.TIMES)
return false;
if (operator == InfixExpression.Operator.TIMES)
// x * (y * z) == x * y * z
return false;
if (operator == InfixExpression.Operator.REMAINDER || operator == InfixExpression.Operator.DIVIDE)
// x * (y % z) != x * y % z , x * (y / z) == x * y / z rounding involved
return true;
return false;
}
return false;
} else {
return true;
}
}
use of org.eclipse.jdt.core.dom.InfixExpression in project flux by eclipse.
the class NecessaryParenthesesChecker method isAllOperandsHaveSameType.
/*
* Do all operands in expression have same type
*/
private static boolean isAllOperandsHaveSameType(InfixExpression expression, ITypeBinding leftOperandType, ITypeBinding rightOperandType) {
ITypeBinding binding = leftOperandType;
if (binding == null)
return false;
ITypeBinding current = rightOperandType;
if (binding != current)
return false;
for (Iterator<Expression> iterator = expression.extendedOperands().iterator(); iterator.hasNext(); ) {
Expression operand = iterator.next();
current = operand.resolveTypeBinding();
if (binding != current)
return false;
}
return true;
}
use of org.eclipse.jdt.core.dom.InfixExpression in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getPickOutStringProposals.
private static boolean getPickOutStringProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
// we work with String's
if (!(node instanceof StringLiteral)) {
return false;
}
// user should select part of String
int selectionPos = context.getSelectionOffset();
int selectionLen = context.getSelectionLength();
if (selectionLen == 0) {
return false;
}
int valueStart = node.getStartPosition() + 1;
int valueEnd = node.getStartPosition() + node.getLength() - 1;
// selection must be inside node and the quotes and not contain the full value
if (selectionPos < valueStart || selectionPos + selectionLen > valueEnd || valueEnd - valueStart == selectionLen) {
return false;
}
// prepare string parts positions
StringLiteral stringLiteral = (StringLiteral) node;
String stringValue = stringLiteral.getEscapedValue();
int firstPos = selectionPos - node.getStartPosition();
int secondPos = firstPos + selectionLen;
// prepare new string literals
AST ast = node.getAST();
StringLiteral leftLiteral = ast.newStringLiteral();
StringLiteral centerLiteral = ast.newStringLiteral();
StringLiteral rightLiteral = ast.newStringLiteral();
try {
leftLiteral.setEscapedValue('"' + stringValue.substring(1, firstPos) + '"');
centerLiteral.setEscapedValue('"' + stringValue.substring(firstPos, secondPos) + '"');
rightLiteral.setEscapedValue('"' + stringValue.substring(secondPos, stringValue.length() - 1) + '"');
} catch (IllegalArgumentException e) {
return false;
}
if (resultingCollections == null) {
return true;
}
ASTRewrite rewrite = ASTRewrite.create(ast);
// prepare new expression instead of StringLiteral
InfixExpression expression = ast.newInfixExpression();
expression.setOperator(InfixExpression.Operator.PLUS);
if (firstPos != 1) {
expression.setLeftOperand(leftLiteral);
}
if (firstPos == 1) {
expression.setLeftOperand(centerLiteral);
} else {
expression.setRightOperand(centerLiteral);
}
if (secondPos < stringValue.length() - 1) {
if (firstPos == 1) {
expression.setRightOperand(rightLiteral);
} else {
expression.extendedOperands().add(rightLiteral);
}
}
// use new expression instead of old StirngLiteral
rewrite.replace(stringLiteral, expression, null);
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_pickSelectedString;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.PICK_SELECTED_STRING);
//$NON-NLS-1$
proposal.addLinkedPosition(rewrite.track(centerLiteral), true, "CENTER_STRING");
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.dom.InfixExpression in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getBooleanExpression.
private static Expression getBooleanExpression(ASTNode node) {
if (!(node instanceof Expression)) {
return null;
}
// check if the node is a location where it can be negated
StructuralPropertyDescriptor locationInParent = node.getLocationInParent();
if (locationInParent == QualifiedName.NAME_PROPERTY) {
node = node.getParent();
locationInParent = node.getLocationInParent();
}
while (locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
node = node.getParent();
locationInParent = node.getLocationInParent();
}
Expression expression = (Expression) node;
if (!isBoolean(expression)) {
return null;
}
if (expression.getParent() instanceof InfixExpression) {
return expression;
}
if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY || locationInParent == IfStatement.EXPRESSION_PROPERTY || locationInParent == WhileStatement.EXPRESSION_PROPERTY || locationInParent == DoStatement.EXPRESSION_PROPERTY || locationInParent == ReturnStatement.EXPRESSION_PROPERTY || locationInParent == ForStatement.EXPRESSION_PROPERTY || locationInParent == AssertStatement.EXPRESSION_PROPERTY || locationInParent == MethodInvocation.ARGUMENTS_PROPERTY || locationInParent == ConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == SuperMethodInvocation.ARGUMENTS_PROPERTY || locationInParent == EnumConstantDeclaration.ARGUMENTS_PROPERTY || locationInParent == SuperConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == ClassInstanceCreation.ARGUMENTS_PROPERTY || locationInParent == ConditionalExpression.EXPRESSION_PROPERTY || locationInParent == PrefixExpression.OPERAND_PROPERTY) {
return expression;
}
return null;
}
Aggregations