use of org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction in project intellij-community by JetBrains.
the class GrRedundantElseIntention method getElementPredicate.
@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
return new PsiElementPredicate() {
@Override
public boolean satisfiedBy(PsiElement element) {
if (!(element.getNode().getElementType() == GroovyTokenTypes.kELSE))
return false;
final PsiElement parent = element.getParent();
if (!(parent instanceof GrIfStatement))
return false;
final GrIfStatement ifStatement = (GrIfStatement) parent;
final GrStatement branch = ifStatement.getThenBranch();
final GrControlFlowOwner flowOwner = ControlFlowUtils.findControlFlowOwner(ifStatement);
if (flowOwner == null)
return false;
final Instruction[] flow = flowOwner.getControlFlow();
for (Instruction instruction : flow) {
if (instruction instanceof IfEndInstruction && instruction.getElement() == ifStatement) {
for (Instruction pred : instruction.allPredecessors()) {
final PsiElement predElement = pred.getElement();
if (predElement != null && PsiTreeUtil.isAncestor(branch, predElement, false)) {
return false;
}
}
}
}
return true;
}
};
}
use of org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction in project intellij-community by JetBrains.
the class InvertIfIntention method isTailAfterIf.
private static boolean isTailAfterIf(@NotNull GrIfStatement ifStatement, @NotNull GrStatementOwner owner) {
final GrControlFlowOwner flowOwner = ControlFlowUtils.findControlFlowOwner(ifStatement);
if (flowOwner == null)
return false;
final Instruction[] flow = flowOwner.getControlFlow();
final GrStatement[] statements = owner.getStatements();
final int index = ArrayUtilRt.find(statements, ifStatement);
if (index == statements.length - 1)
return false;
final GrStatement then = ifStatement.getThenBranch();
for (Instruction i : flow) {
final PsiElement element = i.getElement();
if (element == null || !PsiTreeUtil.isAncestor(then, element, true))
continue;
for (Instruction succ : i.allSuccessors()) {
if (succ instanceof IfEndInstruction) {
return false;
}
}
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction in project intellij-community by JetBrains.
the class ControlFlowUtils method visitAllExitPointsInner.
private static boolean visitAllExitPointsInner(Instruction last, Instruction first, boolean[] visited, ExitPointVisitor visitor) {
if (first == last)
return true;
if (last instanceof AfterCallInstruction) {
visited[last.num()] = true;
return visitAllExitPointsInner(((AfterCallInstruction) last).myCall, first, visited, visitor);
}
if (last instanceof MaybeReturnInstruction) {
return visitor.visitExitPoint(last, (GrExpression) last.getElement());
} else if (last instanceof IfEndInstruction) {
visited[last.num()] = true;
for (Instruction instruction : last.allPredecessors()) {
if (!visitAllExitPointsInner(instruction, first, visited, visitor))
return false;
}
return true;
} else if (last instanceof ThrowingInstruction) {
PsiElement element = last.getElement();
if (!(element instanceof GrThrowStatement || element instanceof GrAssertStatement))
return true;
}
PsiElement element = last.getElement();
if (element != null) {
final GrExpression returnValue;
if (element instanceof GrReturnStatement) {
returnValue = ((GrReturnStatement) element).getReturnValue();
} else if (element instanceof GrExpression && PsiUtil.isExpressionStatement(element)) {
returnValue = (GrExpression) element;
} else {
returnValue = null;
}
return visitor.visitExitPoint(last, returnValue);
}
visited[last.num()] = true;
for (Instruction pred : last.allPredecessors()) {
if (!visited[pred.num()]) {
if (!visitAllExitPointsInner(pred, first, visited, visitor))
return false;
}
}
return true;
}
Aggregations