use of org.eclipse.jdt.core.dom.IfStatement in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getSplitAndConditionProposals.
public static boolean getSplitAndConditionProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
Operator andOperator = InfixExpression.Operator.CONDITIONAL_AND;
// check that user invokes quick assist on infix expression
if (!(node instanceof InfixExpression)) {
return false;
}
InfixExpression infixExpression = (InfixExpression) node;
if (infixExpression.getOperator() != andOperator) {
return false;
}
int offset = isOperatorSelected(infixExpression, context.getSelectionOffset(), context.getSelectionLength());
if (offset == -1) {
return false;
}
// check that infix expression belongs to IfStatement
Statement statement = ASTResolving.findParentStatement(node);
if (!(statement instanceof IfStatement)) {
return false;
}
IfStatement ifStatement = (IfStatement) statement;
// check that infix expression is part of first level && condition of IfStatement
InfixExpression topInfixExpression = infixExpression;
while (topInfixExpression.getParent() instanceof InfixExpression && ((InfixExpression) topInfixExpression.getParent()).getOperator() == andOperator) {
topInfixExpression = (InfixExpression) topInfixExpression.getParent();
}
if (ifStatement.getExpression() != topInfixExpression) {
return false;
}
//
if (resultingCollections == null) {
return true;
}
AST ast = ifStatement.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
// prepare left and right conditions
Expression[] newOperands = { null, null };
breakInfixOperationAtOperation(rewrite, topInfixExpression, andOperator, offset, true, newOperands);
Expression leftCondition = newOperands[0];
Expression rightCondition = newOperands[1];
// replace conditions in outer IfStatement
rewrite.set(ifStatement, IfStatement.EXPRESSION_PROPERTY, leftCondition, null);
// prepare inner IfStatement
IfStatement innerIf = ast.newIfStatement();
innerIf.setExpression(rightCondition);
innerIf.setThenStatement((Statement) rewrite.createMoveTarget(ifStatement.getThenStatement()));
Block innerBlock = ast.newBlock();
innerBlock.statements().add(innerIf);
Statement elseStatement = ifStatement.getElseStatement();
if (elseStatement != null) {
innerIf.setElseStatement((Statement) rewrite.createCopyTarget(elseStatement));
}
// replace outer thenStatement
rewrite.replace(ifStatement.getThenStatement(), innerBlock, null);
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_splitAndCondition_description;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.SPLIT_AND_CONDITION);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.dom.IfStatement in project flux 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);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.dom.IfStatement in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getCastAndAssignIfStatementProposals.
private static boolean getCastAndAssignIfStatementProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
if (node instanceof IfStatement) {
node = ((IfStatement) node).getExpression();
} else if (node instanceof WhileStatement) {
node = ((WhileStatement) node).getExpression();
} else if (node instanceof Block) {
List<Statement> statements = ((Block) node).statements();
if (statements.size() > 0) {
if (context.getSelectionOffset() > statements.get(0).getStartPosition()) {
return false;
}
}
ASTNode parent = node.getParent();
Expression expression = null;
if (parent instanceof IfStatement) {
expression = ((IfStatement) parent).getExpression();
} else if (parent instanceof WhileStatement) {
expression = ((WhileStatement) parent).getExpression();
} else {
return false;
}
if (expression instanceof InstanceofExpression) {
node = expression;
} else {
final ArrayList<InstanceofExpression> nodes = new ArrayList<InstanceofExpression>();
expression.accept(new ASTVisitor() {
@Override
public boolean visit(InstanceofExpression instanceofExpression) {
nodes.add(instanceofExpression);
return false;
}
});
if (nodes.size() != 1) {
return false;
}
node = nodes.get(0);
}
} else {
while (node != null && !(node instanceof InstanceofExpression) && !(node instanceof Statement)) {
node = node.getParent();
}
}
if (!(node instanceof InstanceofExpression)) {
return false;
}
InstanceofExpression expression = (InstanceofExpression) node;
// test that we are the expression of a 'while' or 'if'
while (node.getParent() instanceof Expression) {
node = node.getParent();
}
StructuralPropertyDescriptor locationInParent = node.getLocationInParent();
boolean negated = isNegated(expression);
Statement body = null;
ASTNode insertionPosition = null;
if (negated) {
insertionPosition = node.getParent();
if (locationInParent == IfStatement.EXPRESSION_PROPERTY) {
body = ((IfStatement) node.getParent()).getElseStatement();
if (body != null) {
negated = false;
}
}
if (body == null && insertionPosition.getParent() instanceof Block) {
body = (Statement) insertionPosition.getParent();
}
} else {
if (locationInParent == IfStatement.EXPRESSION_PROPERTY) {
body = ((IfStatement) node.getParent()).getThenStatement();
} else if (locationInParent == WhileStatement.EXPRESSION_PROPERTY) {
body = ((WhileStatement) node.getParent()).getBody();
}
}
if (body == null) {
return false;
}
Type originalType = expression.getRightOperand();
if (originalType.resolveBinding() == null) {
return false;
}
// we could produce quick assist
if (resultingCollections == null) {
return true;
}
//$NON-NLS-1$
final String KEY_NAME = "name";
//$NON-NLS-1$
final String KEY_TYPE = "type";
//
AST ast = expression.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
ICompilationUnit cu = context.getCompilationUnit();
// prepare correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_castAndAssign;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.CAST_AND_ASSIGN);
// prepare possible variable names
List<String> excludedNames = Arrays.asList(ASTResolving.getUsedVariableNames(body));
String[] varNames = suggestLocalVariableNames(cu, originalType.resolveBinding(), excludedNames);
for (int i = 0; i < varNames.length; i++) {
proposal.addLinkedPositionProposal(KEY_NAME, varNames[i], null);
}
CastExpression castExpression = ast.newCastExpression();
castExpression.setExpression((Expression) rewrite.createCopyTarget(expression.getLeftOperand()));
castExpression.setType((Type) ASTNode.copySubtree(ast, originalType));
// prepare new variable declaration
VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment();
vdf.setName(ast.newSimpleName(varNames[0]));
vdf.setInitializer(castExpression);
// prepare new variable declaration statement
VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);
vds.setType((Type) ASTNode.copySubtree(ast, originalType));
// add new variable declaration statement
if (negated) {
ListRewrite listRewriter = rewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY);
listRewriter.insertAfter(vds, insertionPosition, null);
} else {
if (body instanceof Block) {
ListRewrite listRewriter = rewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY);
listRewriter.insertAt(vds, 0, null);
} else {
Block newBlock = ast.newBlock();
List<Statement> statements = newBlock.statements();
statements.add(vds);
statements.add((Statement) rewrite.createMoveTarget(body));
rewrite.replace(body, newBlock, null);
}
}
// setup linked positions
proposal.addLinkedPosition(rewrite.track(vdf.getName()), true, KEY_NAME);
proposal.addLinkedPosition(rewrite.track(vds.getType()), false, KEY_TYPE);
proposal.addLinkedPosition(rewrite.track(castExpression.getType()), false, KEY_TYPE);
// set cursor after expression statement
proposal.setEndPosition(rewrite.track(vds));
// add correction proposal
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.dom.IfStatement in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getInverseIfIntoContinueInLoopsProposals.
private static boolean getInverseIfIntoContinueInLoopsProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
if (!(covering instanceof IfStatement)) {
return false;
}
IfStatement ifStatement = (IfStatement) covering;
if (ifStatement.getElseStatement() != null) {
return false;
}
// prepare outer control structure and block that contains 'if' statement
ASTNode ifParent = ifStatement.getParent();
Block ifParentBlock = null;
ASTNode ifParentStructure = ifParent;
if (ifParentStructure instanceof Block) {
ifParentBlock = (Block) ifParent;
ifParentStructure = ifParentStructure.getParent();
}
// check that control structure is loop and 'if' statement if last statement
if (!(ifParentStructure instanceof ForStatement) && !(ifParentStructure instanceof WhileStatement)) {
return false;
}
if (ifParentBlock != null && ifParentBlock.statements().indexOf(ifStatement) != ifParentBlock.statements().size() - 1) {
return false;
}
// we could produce quick assist
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);
newIf.setThenStatement(ast.newContinueStatement());
//
if (ifParentBlock == null) {
// if there is no block, create it
ifParentBlock = ast.newBlock();
ifParentBlock.statements().add(newIf);
for (Iterator<Statement> iter = getUnwrappedStatements(ifStatement.getThenStatement()).iterator(); iter.hasNext(); ) {
Statement statement = iter.next();
ifParentBlock.statements().add(rewrite.createMoveTarget(statement));
}
// replace 'if' statement as body with new block
if (ifParentStructure instanceof ForStatement) {
rewrite.set(ifParentStructure, ForStatement.BODY_PROPERTY, ifParentBlock, null);
} else if (ifParentStructure instanceof WhileStatement) {
rewrite.set(ifParentStructure, WhileStatement.BODY_PROPERTY, ifParentBlock, null);
}
} else {
// if there was block, replace
ListRewrite listRewriter = rewrite.getListRewrite(ifParentBlock, (ChildListPropertyDescriptor) ifStatement.getLocationInParent());
listRewriter.replace(ifStatement, newIf, null);
// add statements from 'then' to the end of block
for (Iterator<Statement> iter = getUnwrappedStatements(ifStatement.getThenStatement()).iterator(); iter.hasNext(); ) {
Statement statement = iter.next();
listRewriter.insertLast(rewrite.createMoveTarget(statement), null);
}
}
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_inverseIfToContinue_description;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INVERT_IF_TO_CONTINUE);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.dom.IfStatement in project flux 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);
resultingCollections.add(proposal);
return true;
}
Aggregations