mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
invert if: do not forget continue; if if is not the "last statement" (IDEA-19457)
This commit is contained in:
+15
-1
@@ -36,6 +36,7 @@ import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.controlFlow.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.ArrayUtilRt;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -304,8 +305,21 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction {
|
||||
codeStyle.reformat(statement);
|
||||
}
|
||||
|
||||
private static void setElseBranch(PsiIfStatement ifStatement, PsiStatement thenBranch, ControlFlow flow) throws IncorrectOperationException {
|
||||
private static void setElseBranch(PsiIfStatement ifStatement, PsiStatement thenBranch, ControlFlow flow)
|
||||
throws IncorrectOperationException {
|
||||
if (flow.getEndOffset(ifStatement) == flow.getEndOffset(thenBranch)) {
|
||||
final PsiLoopStatement loopStmt = PsiTreeUtil.getParentOfType(ifStatement, PsiLoopStatement.class);
|
||||
if (loopStmt != null) {
|
||||
final PsiStatement body = loopStmt.getBody();
|
||||
if (body instanceof PsiBlockStatement) {
|
||||
final PsiStatement[] statements = ((PsiBlockStatement)body).getCodeBlock().getStatements();
|
||||
if (statements.length > 0 && !PsiTreeUtil.isAncestor(statements[statements.length - 1], ifStatement, false) &&
|
||||
ArrayUtilRt.find(statements, ifStatement) < 0) {
|
||||
ifStatement.setElseBranch(thenBranch);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (thenBranch instanceof PsiContinueStatement) {
|
||||
PsiStatement elseBranch = ifStatement.getElseBranch();
|
||||
if (elseBranch != null) {
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
// "Invert If Condition" "true"
|
||||
class B {
|
||||
public static void foo() {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
if (i % 2 == 0) {
|
||||
if (i != 0) {
|
||||
|
||||
System.out.println("!= 0");
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
System.out.println("== 0");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("i = " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// "Invert If Condition" "true"
|
||||
class B {
|
||||
public static void foo() {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
if (i % 2 == 0) {
|
||||
if (i != 0) {
|
||||
|
||||
System.out.println("!= 0");
|
||||
}
|
||||
else {
|
||||
System.out.println("== 0");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// "Invert If Condition" "true"
|
||||
class B {
|
||||
public static void foo() {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
if (i % 2 == 0) {
|
||||
<caret>if (i == 0) {
|
||||
System.out.println("== 0");
|
||||
continue;
|
||||
}
|
||||
|
||||
System.out.println("!= 0");
|
||||
continue;
|
||||
}
|
||||
|
||||
System.out.println("i = " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// "Invert If Condition" "true"
|
||||
class B {
|
||||
public static void foo() {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
if (i % 2 == 0) {
|
||||
<caret>if (i == 0) {
|
||||
System.out.println("== 0");
|
||||
continue;
|
||||
}
|
||||
|
||||
System.out.println("!= 0");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user