private boolean isLastStmt(final Statement node) { final Statement nextStmt = ASTHelper.getNextStatement(node); if (nextStmt == null) { if (node.getParent() instanceof MethodDeclaration) { return true; } else if (node.getParent() instanceof WhileStatement || node.getParent() instanceof DoStatement || node.getParent() instanceof ForStatement || node.getParent() instanceof EnhancedForStatement) { return false; } else if (node.getParent() instanceof Statement) { return isLastStmt((Statement) node.getParent()); } else { return false; } } else { return false; } } }
private static Statement getSibling(Statement node, boolean isPrevious) { if (node.getParent() instanceof Block) { final List<Statement> stmts = asList((Statement) node.getParent()); final int indexOfNode = stmts.indexOf(node); final int siblingIndex = isPrevious ? indexOfNode - 1 : indexOfNode + 1; if (0 <= siblingIndex && siblingIndex < stmts.size()) { return stmts.get(siblingIndex); } } return null; }
private void registerLoopLabel(Statement node) { String identifier; if (node.getParent() instanceof LabeledStatement) { LabeledStatement labeledStatement= (LabeledStatement)node.getParent(); identifier= labeledStatement.getLabel().getIdentifier(); } else { identifier= null; } fOpenLoopLabels.add(identifier); }
private void registerLoopLabel(Statement node) { String identifier; if (node.getParent() instanceof LabeledStatement) { LabeledStatement labeledStatement = (LabeledStatement) node.getParent(); identifier = labeledStatement.getLabel().getIdentifier(); } else { identifier = null; } fOpenLoopLabels.add(identifier); }
private void registerLoopLabel(Statement node) { String identifier; if (node.getParent() instanceof LabeledStatement) { LabeledStatement labeledStatement= (LabeledStatement)node.getParent(); identifier= labeledStatement.getLabel().getIdentifier(); } else { identifier= null; } fOpenLoopLabels.add(identifier); }
/** * Returns the next statements in the same block if it exists. * * @param startNode the start node * @return the next statements in the same block if it exists, empty list otherwise */ public static List<Statement> getNextSiblings(Statement startNode) { if (startNode.getParent() instanceof Block) { final List<Statement> stmts = asList((Statement) startNode.getParent()); final int indexOfNode = stmts.indexOf(startNode); final int siblingIndex = indexOfNode + 1; if (0 <= siblingIndex && siblingIndex < stmts.size()) { return stmts.subList(siblingIndex, stmts.size()); } } return Collections.emptyList(); }
private void removeForwardCode(final Statement astNode, final Statement unconditionnalStatement) { if (astNode.getParent() instanceof Block) { this.ctx.getRefactorings().remove(getNextSiblings(astNode)); removeForwardCode((Block) astNode.getParent(), unconditionnalStatement); } else if (astNode.getParent() instanceof TryStatement) { removeForwardCode((TryStatement) astNode.getParent(), unconditionnalStatement); } } }
private static boolean isLastStatementInEnclosingMethodOrLambda(Statement statement) { ASTNode currentStructure= statement; ASTNode currentParent= statement.getParent(); while (!(currentParent instanceof MethodDeclaration || currentParent instanceof LambdaExpression)) { // should not be in a loop if (currentParent instanceof ForStatement || currentParent instanceof EnhancedForStatement || currentParent instanceof WhileStatement || currentParent instanceof DoStatement) { return false; } if (currentParent instanceof Block) { Block parentBlock= (Block) currentParent; if (parentBlock.statements().indexOf(currentStructure) != parentBlock.statements().size() - 1) { // not last statement in the block return false; } } currentStructure= currentParent; currentParent= currentParent.getParent(); } return true; }
/** * Returns the next statement in the source file if it exists. * * @param startNode the start node * @return the next statement in the source file if it exists, null otherwise */ public static Statement getNextStatement(Statement startNode) { final Statement nextSibling = getNextSibling(startNode); if (nextSibling != null) { return nextSibling; } final ASTNode parent = startNode.getParent(); if (parent instanceof Statement) { return getNextStatement((Statement) parent); } return null; }
/** * Returns the previous statement in the source file if it exists. * * @param startNode the start node * @return the previous statement in the source file if it exists, null otherwise */ public static Statement getPreviousStatement(Statement startNode) { final Statement previousSibling = getPreviousSibling(startNode); if (previousSibling != null) { return previousSibling; } final ASTNode parent = startNode.getParent(); if (parent instanceof Statement) { return getPreviousStatement((Statement) parent); } return null; }
private static boolean isLastStatementInEnclosingMethodOrLambda(Statement statement) { ASTNode currentStructure= statement; ASTNode currentParent= statement.getParent(); while (!(currentParent instanceof MethodDeclaration || currentParent instanceof LambdaExpression)) { // should not be in a loop if (currentParent instanceof ForStatement || currentParent instanceof EnhancedForStatement || currentParent instanceof WhileStatement || currentParent instanceof DoStatement) { return false; } if (currentParent instanceof Block) { Block parentBlock= (Block) currentParent; if (parentBlock.statements().indexOf(currentStructure) != parentBlock.statements().size() - 1) { // not last statement in the block return false; } } currentStructure= currentParent; currentParent= currentParent.getParent(); } return true; }
public static RearrangeStrategy create(Statement origStmt, ASTRewrite rewrite, TextEditGroup group) { ASTNode parent = origStmt.getParent(); if (parent instanceof Block) { Block block= (Block)parent; if (origStmt instanceof VariableDeclarationStatement) return new ModifyBlockWithLocalDecl(origStmt, block, rewrite, group); else return new ModifyBlock(origStmt, block, rewrite, group); } else { return new ReplaceStatement(origStmt, rewrite, group); } }
private boolean maybeRemoveCode(final Statement node) { final ASTNode parent = node.getParent(); if (parent instanceof Block && isEmptyCode(node)) { this.ctx.getRefactorings().remove(node); return DO_NOT_VISIT_SUBTREE; } return VISIT_SUBTREE; }
public static RearrangeStrategy create(Statement origStmt, ASTRewrite rewrite, TextEditGroup group) { ASTNode parent = origStmt.getParent(); if (parent instanceof Block) { Block block= (Block)parent; if (origStmt instanceof VariableDeclarationStatement) return new ModifyBlockWithLocalDecl(origStmt, block, rewrite, group); else return new ModifyBlock(origStmt, block, rewrite, group); } else { return new ReplaceStatement(origStmt, rewrite, group); } }
private void handleLoopBody(Statement body) { if (body instanceof Block) return; if (body instanceof EmptyStatement && !this.options.put_empty_statement_on_new_line && !(body.getParent() instanceof IfStatement)) return; breakLineBefore(body); adjustEmptyLineAfter(this.tm.lastIndexIn(body, -1), -1); indent(body); }
private void handleLoopBody(Statement body) { if (body instanceof Block) return; if (body instanceof EmptyStatement && !this.options.put_empty_statement_on_new_line && !(body.getParent() instanceof IfStatement)) return; breakLineBefore(body); adjustEmptyLineAfter(this.tm.lastIndexIn(body, -1), -1); indent(body); }
private void handleLoopBody(Statement body) { if (body instanceof Block) return; if (body instanceof EmptyStatement && !this.options.put_empty_statement_on_new_line && !(body.getParent() instanceof IfStatement)) return; breakLineBefore(body); adjustEmptyLineAfter(this.tm.lastIndexIn(body, -1), -1); indent(body); }
private void handleLoopBody(Statement body) { if (body instanceof Block) return; if (body instanceof EmptyStatement && !this.options.put_empty_statement_on_new_line && !(body.getParent() instanceof IfStatement)) return; breakLineBefore(body); adjustEmptyLineAfter(this.tm.lastIndexIn(body, -1), -1); indent(body); }
private void handleSimpleLoop(Statement body, int wrappingOption) { if (!(body instanceof Block)) { this.wrapIndexes.add(this.tm.firstIndexIn(body, -1)); this.wrapParentIndex = this.tm.firstIndexBefore(body, TokenNameRPAREN); this.wrapGroupEnd = this.tm.lastIndexIn(body, -1); handleWrap(wrappingOption, body.getParent()); body.accept(new ASTVisitor() { @Override public boolean visit(Block node) { forceContinuousWrapping(node, WrapPreparator.this.tm.firstIndexIn(node, -1)); return false; } }); } }
private void replaceBlockByPlainCode(final Statement sourceNode, final Statement unconditionnalStatement) { final ASTBuilder b = this.ctx.getASTBuilder(); final Refactorings r = this.ctx.getRefactorings(); if (unconditionnalStatement instanceof Block && sourceNode.getParent() instanceof Block) { r.replace(sourceNode, b.copyRange(statements((Block) unconditionnalStatement))); } else { r.replace(sourceNode, b.copy(unconditionnalStatement)); } }