use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement 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.api.statements.GrIfStatement in project intellij-community by JetBrains.
the class GroovyElseRemover method deleteSelectedElseIf.
private static void deleteSelectedElseIf(GrIfStatement selectedBranch, Context context) throws IncorrectOperationException {
GrIfStatement parentIf = (GrIfStatement) selectedBranch.getParent();
GrStatement childElse = selectedBranch.getElseBranch();
if (childElse == null) {
context.delete(selectedBranch);
return;
}
context.setElseBranch(parentIf, childElse);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement in project intellij-community by JetBrains.
the class IfElseSurrounder method doSurroundElements.
@Override
protected GroovyPsiElement doSurroundElements(PsiElement[] elements, PsiElement context) throws IncorrectOperationException {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(elements[0].getProject());
GrIfStatement ifStatement = (GrIfStatement) factory.createStatementFromText("if (a) {\n} else {\n}", context);
addStatements(((GrBlockStatement) ifStatement.getThenBranch()).getBlock(), elements);
return ifStatement;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement in project intellij-community by JetBrains.
the class IfSurrounder method doSurroundElements.
@Override
protected GroovyPsiElement doSurroundElements(PsiElement[] elements, PsiElement context) throws IncorrectOperationException {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(elements[0].getProject());
GrIfStatement ifStatement = (GrIfStatement) factory.createStatementFromText("if (a) {\n}", context);
addStatements(((GrBlockStatement) ifStatement.getThenBranch()).getBlock(), elements);
return ifStatement;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement in project intellij-community by JetBrains.
the class IfSurrounder method getSurroundSelectionRange.
@Override
protected TextRange getSurroundSelectionRange(GroovyPsiElement element) {
assert element instanceof GrIfStatement;
GrCondition condition = ((GrIfStatement) element).getCondition();
int endOffset = element.getTextRange().getEndOffset();
if (condition != null) {
PsiElement child = condition.getFirstChild();
assert child != null;
endOffset = child.getTextRange().getStartOffset();
condition.getParent().getNode().removeChild(condition.getNode());
}
return new TextRange(endOffset, endOffset);
}
Aggregations