invert if: fix parent ifStatement (IDEA-191327)

This commit is contained in:
Anna.Kozlova
2018-07-10 16:49:02 +02:00
parent c50c30d764
commit d7e2efc0c0
3 changed files with 24 additions and 0 deletions
@@ -213,6 +213,11 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction {
PsiStatement statement = factory.createStatementFromText("return;", ifStatement);
statement = (PsiStatement) codeStyle.reformat(statement);
if (thenBranch instanceof PsiBlockStatement) {
if (ifStatement.getParent() instanceof PsiIfStatement) {
ifStatement = (PsiIfStatement)wrapWithCodeBlock(ifStatement);
thenBranch = ifStatement.getThenBranch();
assert thenBranch != null;
}
PsiCodeBlock codeBlock = ((PsiBlockStatement)thenBranch).getCodeBlock();
PsiElement firstElement = codeBlock.getFirstBodyElement();
PsiElement lastElement = codeBlock.getLastBodyElement();
@@ -0,0 +1,12 @@
// "Invert 'if' condition" "true"
class A {
public void foo(boolean c, boolean c2) {
if (c) a();
else {
if (c2) {
return;
}
b();
}
}
}
@@ -0,0 +1,7 @@
// "Invert 'if' condition" "true"
class A {
public void foo(boolean c, boolean c2) {
if (c) a();
else if (!c<caret>2) b();
}
}