use of org.eclipse.jdt.core.dom.InfixExpression in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getExchangeOperandsProposals.
private static boolean getExchangeOperandsProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
// check that user invokes quick assist on infix expression
if (!(node instanceof InfixExpression)) {
return false;
}
InfixExpression infixExpression = (InfixExpression) node;
Operator operator = infixExpression.getOperator();
if (operator != InfixExpression.Operator.CONDITIONAL_AND && operator != InfixExpression.Operator.AND && operator != InfixExpression.Operator.CONDITIONAL_OR && operator != InfixExpression.Operator.OR && operator != InfixExpression.Operator.EQUALS && operator != InfixExpression.Operator.NOT_EQUALS && operator != InfixExpression.Operator.LESS && operator != InfixExpression.Operator.LESS_EQUALS && operator != InfixExpression.Operator.GREATER && operator != InfixExpression.Operator.GREATER_EQUALS && operator != InfixExpression.Operator.PLUS && operator != InfixExpression.Operator.TIMES && operator != InfixExpression.Operator.XOR) {
return false;
}
int offset = isOperatorSelected(infixExpression, context.getSelectionOffset(), context.getSelectionLength());
if (offset == -1) {
return false;
}
// we could produce quick assist
if (resultingCollections == null) {
return true;
}
AST ast = infixExpression.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
// prepare left and right expressions
Expression leftExpression = null;
Expression rightExpression = null;
InfixExpression currentExpression = infixExpression;
leftExpression = combineOperands(rewrite, leftExpression, infixExpression.getLeftOperand(), false, operator);
if (infixExpression.getRightOperand().getStartPosition() <= context.getSelectionOffset()) {
leftExpression = combineOperands(rewrite, leftExpression, infixExpression.getRightOperand(), false, operator);
} else {
rightExpression = combineOperands(rewrite, rightExpression, infixExpression.getRightOperand(), false, operator);
}
for (Iterator<Expression> iter = currentExpression.extendedOperands().iterator(); iter.hasNext(); ) {
Expression extendedOperand = iter.next();
if (extendedOperand.getStartPosition() <= context.getSelectionOffset()) {
leftExpression = combineOperands(rewrite, leftExpression, extendedOperand, false, operator);
} else {
rightExpression = combineOperands(rewrite, rightExpression, extendedOperand, false, operator);
}
}
if (NecessaryParenthesesChecker.needsParentheses(leftExpression, infixExpression, InfixExpression.RIGHT_OPERAND_PROPERTY)) {
leftExpression = getParenthesizedExpression(ast, leftExpression);
}
if (NecessaryParenthesesChecker.needsParentheses(rightExpression, infixExpression, InfixExpression.LEFT_OPERAND_PROPERTY)) {
rightExpression = getParenthesizedExpression(ast, rightExpression);
}
if (operator == InfixExpression.Operator.LESS) {
operator = InfixExpression.Operator.GREATER;
} else if (operator == InfixExpression.Operator.LESS_EQUALS) {
operator = InfixExpression.Operator.GREATER_EQUALS;
} else if (operator == InfixExpression.Operator.GREATER) {
operator = InfixExpression.Operator.LESS;
} else if (operator == InfixExpression.Operator.GREATER_EQUALS) {
operator = InfixExpression.Operator.LESS_EQUALS;
}
// create new infix expression
InfixExpression newInfix = ast.newInfixExpression();
newInfix.setOperator(operator);
newInfix.setLeftOperand(rightExpression);
newInfix.setRightOperand(leftExpression);
rewrite.replace(infixExpression, newInfix, null);
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_exchangeOperands_description;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.EXCHANGE_OPERANDS);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.dom.InfixExpression in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getPushNegationDownProposals.
private static boolean getPushNegationDownProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
PrefixExpression negationExpression = null;
ParenthesizedExpression parenthesizedExpression = null;
// check for case when cursor is on '!' before parentheses
if (covering instanceof PrefixExpression) {
PrefixExpression prefixExpression = (PrefixExpression) covering;
if (prefixExpression.getOperator() == PrefixExpression.Operator.NOT && prefixExpression.getOperand() instanceof ParenthesizedExpression) {
negationExpression = prefixExpression;
parenthesizedExpression = (ParenthesizedExpression) prefixExpression.getOperand();
}
}
// check for case when cursor is on parenthesized expression that is negated
if (covering instanceof ParenthesizedExpression && covering.getParent() instanceof PrefixExpression && ((PrefixExpression) covering.getParent()).getOperator() == PrefixExpression.Operator.NOT) {
negationExpression = (PrefixExpression) covering.getParent();
parenthesizedExpression = (ParenthesizedExpression) covering;
}
if (negationExpression == null || (!(parenthesizedExpression.getExpression() instanceof InfixExpression) && !(parenthesizedExpression.getExpression() instanceof ConditionalExpression))) {
return false;
}
// we could produce quick assist
if (resultingCollections == null) {
return true;
}
//
final AST ast = covering.getAST();
final ASTRewrite rewrite = ASTRewrite.create(ast);
// prepared inverted expression
Expression inversedExpression = getInversedExpression(rewrite, parenthesizedExpression.getExpression());
// check, may be we should keep parentheses
boolean keepParentheses = false;
if (negationExpression.getParent() instanceof Expression) {
int parentPrecedence = OperatorPrecedence.getExpressionPrecedence(((Expression) negationExpression.getParent()));
int inversedExpressionPrecedence = OperatorPrecedence.getExpressionPrecedence(inversedExpression);
keepParentheses = parentPrecedence > inversedExpressionPrecedence;
}
// replace negated expression with inverted one
if (keepParentheses) {
ParenthesizedExpression pe = ast.newParenthesizedExpression();
pe.setExpression(inversedExpression);
rewrite.replace(negationExpression, pe, null);
} else {
rewrite.replace(negationExpression, inversedExpression, null);
}
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_pushNegationDown;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.PULL_NEGATION_DOWN);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.dom.InfixExpression in project flux by eclipse.
the class AdvancedQuickAssistProcessor method combineOperands.
private static Expression combineOperands(ASTRewrite rewrite, Expression existing, Expression originalNode, boolean removeParentheses, Operator operator) {
if (existing == null && removeParentheses) {
while (originalNode instanceof ParenthesizedExpression) {
originalNode = ((ParenthesizedExpression) originalNode).getExpression();
}
}
Expression newRight = (Expression) rewrite.createMoveTarget(originalNode);
if (originalNode instanceof InfixExpression) {
((InfixExpression) newRight).setOperator(((InfixExpression) originalNode).getOperator());
}
if (existing == null) {
return newRight;
}
AST ast = rewrite.getAST();
InfixExpression infix = ast.newInfixExpression();
infix.setOperator(operator);
infix.setLeftOperand(existing);
infix.setRightOperand(newRight);
return infix;
}
use of org.eclipse.jdt.core.dom.InfixExpression in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getInversedAndOrExpression.
private static Expression getInversedAndOrExpression(ASTRewrite rewrite, InfixExpression infixExpression, Operator newOperator, SimpleNameRenameProvider provider) {
InfixExpression newExpression = rewrite.getAST().newInfixExpression();
newExpression.setOperator(newOperator);
int newOperatorPrecedence = OperatorPrecedence.getOperatorPrecedence(newOperator);
//
Expression leftOperand = getInversedExpression(rewrite, infixExpression.getLeftOperand(), provider);
newExpression.setLeftOperand(parenthesizeIfRequired(leftOperand, newOperatorPrecedence));
Expression rightOperand = getInversedExpression(rewrite, infixExpression.getRightOperand(), provider);
newExpression.setRightOperand(parenthesizeIfRequired(rightOperand, newOperatorPrecedence));
List<Expression> extraOperands = infixExpression.extendedOperands();
List<Expression> newExtraOperands = newExpression.extendedOperands();
for (int i = 0; i < extraOperands.size(); i++) {
Expression extraOperand = getInversedExpression(rewrite, extraOperands.get(i), provider);
newExtraOperands.add(parenthesizeIfRequired(extraOperand, newOperatorPrecedence));
}
return newExpression;
}
use of org.eclipse.jdt.core.dom.InfixExpression in project flux by eclipse.
the class AdvancedQuickAssistProcessor method isOperatorSelected.
private static int isOperatorSelected(InfixExpression infixExpression, int offset, int length) {
ASTNode left = infixExpression.getLeftOperand();
ASTNode right = infixExpression.getRightOperand();
if (isSelectingOperator(left, right, offset, length)) {
return ASTNodes.getExclusiveEnd(left);
}
List<Expression> extended = infixExpression.extendedOperands();
for (int i = 0; i < extended.size(); i++) {
left = right;
right = extended.get(i);
if (isSelectingOperator(left, right, offset, length)) {
return ASTNodes.getExclusiveEnd(left);
}
}
return -1;
}
Aggregations