use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.
the class AdvancedQuickAssistProcessor method getInverseConditionProposals.
private static boolean getInverseConditionProposals(IInvocationContext context, ASTNode covering, ArrayList<ASTNode> coveredNodes, Collection<ICommandAccess> resultingCollections) {
if (coveredNodes.isEmpty()) {
return false;
}
//
final AST ast = covering.getAST();
final ASTRewrite rewrite = ASTRewrite.create(ast);
// check sub-expressions in fully covered nodes
boolean hasChanges = false;
for (Iterator<ASTNode> iter = coveredNodes.iterator(); iter.hasNext(); ) {
ASTNode covered = iter.next();
Expression coveredExpression = getBooleanExpression(covered);
if (coveredExpression != null) {
Expression inversedExpression = getInversedExpression(rewrite, coveredExpression);
rewrite.replace(coveredExpression, inversedExpression, null);
hasChanges = true;
}
}
//
if (!hasChanges) {
return false;
}
if (resultingCollections == null) {
return true;
}
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_inverseConditions_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INVERSE_CONDITIONS, image);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che 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, image);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.
the class AdvancedQuickAssistProcessor method getInverseIfContinueIntoIfThenInLoopsProposals.
private static boolean getInverseIfContinueIntoIfThenInLoopsProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
if (!(covering instanceof IfStatement)) {
return false;
}
IfStatement ifStatement = (IfStatement) covering;
if (ifStatement.getElseStatement() != null) {
return false;
}
// check that 'then' is 'continue'
if (!(ifStatement.getThenStatement() instanceof ContinueStatement)) {
return false;
}
// check that 'if' statement is statement in block that is body of loop
Block loopBlock = null;
if (ifStatement.getParent() instanceof Block && ifStatement.getParent().getParent() instanceof ForStatement) {
loopBlock = (Block) ifStatement.getParent();
} else if (ifStatement.getParent() instanceof Block && ifStatement.getParent().getParent() instanceof WhileStatement) {
loopBlock = (Block) ifStatement.getParent();
} else {
return false;
}
if (resultingCollections == null) {
return true;
}
//
AST ast = covering.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
// create inverted 'if' statement
Expression inversedExpression = getInversedExpression(rewrite, ifStatement.getExpression());
IfStatement newIf = ast.newIfStatement();
newIf.setExpression(inversedExpression);
// prepare 'then' for new 'if'
Block thenBlock = ast.newBlock();
int ifIndex = loopBlock.statements().indexOf(ifStatement);
for (int i = ifIndex + 1; i < loopBlock.statements().size(); i++) {
Statement statement = (Statement) loopBlock.statements().get(i);
thenBlock.statements().add(rewrite.createMoveTarget(statement));
}
newIf.setThenStatement(thenBlock);
// replace 'if' statement in loop
rewrite.replace(ifStatement, newIf, null);
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_inverseIfContinue_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INVERSE_IF_CONTINUE, image);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.
the class AdvancedQuickAssistProcessor method getJoinOrIfStatementsProposals.
private static boolean getJoinOrIfStatementsProposals(IInvocationContext context, ASTNode covering, ArrayList<ASTNode> coveredNodes, Collection<ICommandAccess> resultingCollections) {
Operator orOperator = InfixExpression.Operator.CONDITIONAL_OR;
if (coveredNodes.size() < 2)
return false;
// check that all covered nodes are IfStatement's with same 'then' statement and without 'else'
String commonThenSource = null;
for (Iterator<ASTNode> iter = coveredNodes.iterator(); iter.hasNext(); ) {
ASTNode node = iter.next();
if (!(node instanceof IfStatement))
return false;
//
IfStatement ifStatement = (IfStatement) node;
if (ifStatement.getElseStatement() != null)
return false;
//
Statement thenStatement = ifStatement.getThenStatement();
try {
String thenSource = context.getCompilationUnit().getBuffer().getText(thenStatement.getStartPosition(), thenStatement.getLength());
if (commonThenSource == null) {
commonThenSource = thenSource;
} else {
if (!commonThenSource.equals(thenSource))
return false;
}
} catch (Throwable e) {
return false;
}
}
if (resultingCollections == null) {
return true;
}
//
final AST ast = covering.getAST();
final ASTRewrite rewrite = ASTRewrite.create(ast);
// prepare OR'ed condition
InfixExpression condition = null;
boolean hasRightOperand = false;
Statement thenStatement = null;
for (Iterator<ASTNode> iter = coveredNodes.iterator(); iter.hasNext(); ) {
IfStatement ifStatement = (IfStatement) iter.next();
if (thenStatement == null)
thenStatement = (Statement) rewrite.createCopyTarget(ifStatement.getThenStatement());
if (condition == null) {
condition = ast.newInfixExpression();
condition.setOperator(orOperator);
condition.setLeftOperand(getParenthesizedExpressionIfNeeded(ast, rewrite, ifStatement.getExpression(), condition, InfixExpression.LEFT_OPERAND_PROPERTY));
} else if (!hasRightOperand) {
condition.setRightOperand(getParenthesizedExpressionIfNeeded(ast, rewrite, ifStatement.getExpression(), condition, InfixExpression.RIGHT_OPERAND_PROPERTY));
hasRightOperand = true;
} else {
InfixExpression newCondition = ast.newInfixExpression();
newCondition.setOperator(orOperator);
newCondition.setLeftOperand(condition);
newCondition.setRightOperand(getParenthesizedExpressionIfNeeded(ast, rewrite, ifStatement.getExpression(), condition, InfixExpression.RIGHT_OPERAND_PROPERTY));
condition = newCondition;
}
}
// prepare new IfStatement with OR'ed condition
IfStatement newIf = ast.newIfStatement();
newIf.setExpression(condition);
newIf.setThenStatement(thenStatement);
//
ListRewrite listRewriter = null;
for (Iterator<ASTNode> iter = coveredNodes.iterator(); iter.hasNext(); ) {
IfStatement ifStatement = (IfStatement) iter.next();
if (listRewriter == null) {
Block sourceBlock = (Block) ifStatement.getParent();
//int insertIndex = sourceBlock.statements().indexOf(ifStatement);
listRewriter = rewrite.getListRewrite(sourceBlock, (ChildListPropertyDescriptor) ifStatement.getLocationInParent());
}
if (newIf != null) {
listRewriter.replace(ifStatement, newIf, null);
newIf = null;
} else {
listRewriter.remove(ifStatement, null);
}
}
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_joinWithOr_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.JOIN_IF_STATEMENTS_WITH_OR, image);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.
the class AdvancedQuickAssistProcessor method getIfReturnIntoIfElseAtEndOfVoidMethodProposals.
private static boolean getIfReturnIntoIfElseAtEndOfVoidMethodProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
if (!(covering instanceof IfStatement)) {
return false;
}
IfStatement ifStatement = (IfStatement) covering;
if (ifStatement.getElseStatement() != null) {
return false;
}
// 'then' block should have 'return' as last statement
Statement thenStatement = ifStatement.getThenStatement();
if (!(thenStatement instanceof Block)) {
return false;
}
Block thenBlock = (Block) thenStatement;
List<Statement> thenStatements = thenBlock.statements();
if (thenStatements.isEmpty() || !(thenStatements.get(thenStatements.size() - 1) instanceof ReturnStatement)) {
return false;
}
// method should return 'void'
MethodDeclaration coveringMetod = ASTResolving.findParentMethodDeclaration(covering);
if (coveringMetod == null) {
return false;
}
Type returnType = coveringMetod.getReturnType2();
if (!isVoid(returnType)) {
return false;
}
//
List<Statement> statements = coveringMetod.getBody().statements();
int ifIndex = statements.indexOf(ifStatement);
if (ifIndex == -1) {
return false;
}
// we could produce quick assist
if (resultingCollections == null) {
return true;
}
//
AST ast = covering.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
// remove last 'return' in 'then' block
ListRewrite listRewriter = rewrite.getListRewrite(thenBlock, (ChildListPropertyDescriptor) ifStatement.getLocationInParent());
listRewriter.remove(thenStatements.get(thenStatements.size() - 1), null);
// prepare original nodes
Expression conditionPlaceholder = (Expression) rewrite.createMoveTarget(ifStatement.getExpression());
Statement thenPlaceholder = (Statement) rewrite.createMoveTarget(ifStatement.getThenStatement());
// prepare 'else' block
Block elseBlock = ast.newBlock();
for (int i = ifIndex + 1; i < statements.size(); i++) {
Statement statement = statements.get(i);
elseBlock.statements().add(rewrite.createMoveTarget(statement));
}
// prepare new 'if' statement
IfStatement newIf = ast.newIfStatement();
newIf.setExpression(conditionPlaceholder);
newIf.setThenStatement(thenPlaceholder);
newIf.setElseStatement(elseBlock);
rewrite.replace(ifStatement, newIf, null);
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_convertToIfElse_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.CONVERT_TO_IF_ELSE, image);
resultingCollections.add(proposal);
return true;
}
Aggregations