invert if: ensure outer if becomes braces when nested if has no else brunch (IDEA-206882)

This commit is contained in:
Anna.Kozlova
2019-02-07 16:49:03 +01:00
parent d5ac0d1f81
commit b6f6bae31a
3 changed files with 31 additions and 1 deletions
@@ -142,7 +142,11 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction {
elseBranch = Objects.requireNonNull(ifStatement.getElseBranch()).replace(elseBranch);
if (emptyBlock(((PsiBlockStatement)elseBranch).getCodeBlock())) {
new CommentTracker().deleteAndRestoreComments(ifStatement.getElseBranch());
PsiElement parent = ifStatement.getParent();
if (parent instanceof PsiIfStatement && ((PsiIfStatement)parent).getElseBranch() != null) {
ifStatement = (PsiIfStatement)wrapWithCodeBlock(ifStatement);
}
new CommentTracker().deleteAndRestoreComments(Objects.requireNonNull(ifStatement.getElseBranch()));
}
}
}
@@ -0,0 +1,13 @@
// "Invert 'if' condition" "true"
class Main {
void foo(boolean a1, boolean b1){
if (a1) {
if (!b1) {
System.out.println("1");
}
}
else {
System.out.println("2");
}
}
}
@@ -0,0 +1,13 @@
// "Invert 'if' condition" "true"
class Main {
void foo(boolean a1, boolean b1){
if (a1)
if (b<caret>1) {
} else {
System.out.println("1");
}
else {
System.out.println("2");
}
}
}