mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
convert to multiple ifs: respect operations priority (IDEA-105275)
This commit is contained in:
+9
-8
@@ -15,8 +15,10 @@
|
||||
*/
|
||||
package com.siyeh.ipp.trivialif;
|
||||
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.siyeh.ipp.base.Intention;
|
||||
import com.siyeh.ipp.base.PsiElementPredicate;
|
||||
@@ -60,7 +62,13 @@ public class ConvertToNestedIfIntention extends Intention {
|
||||
return;
|
||||
}
|
||||
final String newStatementText = buildIf(returnValue, new StringBuilder()).toString();
|
||||
addStatementBefore(newStatementText, returnStatement);
|
||||
final Project project = returnStatement.getProject();
|
||||
final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory();
|
||||
final PsiBlockStatement blockStatement = (PsiBlockStatement)elementFactory.createStatementFromText("{" + newStatementText + "}", returnStatement);
|
||||
final PsiElement parent = returnStatement.getParent();
|
||||
for (PsiStatement st : blockStatement.getCodeBlock().getStatements()) {
|
||||
CodeStyleManager.getInstance(project).reformat(parent.addBefore(st, returnStatement));
|
||||
}
|
||||
replaceStatement("return false;", returnStatement);
|
||||
}
|
||||
|
||||
@@ -79,14 +87,7 @@ public class ConvertToNestedIfIntention extends Intention {
|
||||
return out;
|
||||
}
|
||||
else if (JavaTokenType.OROR.equals(tokenType)) {
|
||||
boolean insertElse = false;
|
||||
for (PsiExpression operand : operands) {
|
||||
if (insertElse) {
|
||||
out.append("else ");
|
||||
}
|
||||
else {
|
||||
insertElse = true;
|
||||
}
|
||||
buildIf(operand, out);
|
||||
if (!StringUtil.endsWith(out, "return true;")) {
|
||||
out.append("return true;");
|
||||
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
public class Test {
|
||||
boolean A, B, C;
|
||||
boolean f() {
|
||||
return A &<caret>& B || C;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
public class Test {
|
||||
boolean A, B, C;
|
||||
boolean f() {
|
||||
if (A) if (B) return true;
|
||||
if (C) return true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -3,7 +3,7 @@ package com.siyeh.ipp.trivialif.convert_to_nested_if;
|
||||
class Nested {
|
||||
boolean foo(boolean a, boolean b, boolean c) {
|
||||
if (a) return true;
|
||||
else if (b ^ c) if (a) return true;
|
||||
if (b ^ c) if (a) return true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
public class Test {
|
||||
boolean A, B, C;
|
||||
boolean f() {
|
||||
return C || A &<caret>& B;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
public class Test {
|
||||
boolean A, B, C;
|
||||
boolean f() {
|
||||
if (C) return true;
|
||||
if (A) if (B) return true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+3
@@ -24,6 +24,9 @@ public class ConvertToNestedIfIntentionTest extends IPPTestCase {
|
||||
public void testStaircase() { doTest(); }
|
||||
public void testOneLevelStaircase() { assertIntentionNotAvailable(); }
|
||||
|
||||
public void testAndOrMixed() { doTest(); }
|
||||
public void testOrAndMixed() { doTest(); }
|
||||
|
||||
@Override
|
||||
protected String getIntentionName() {
|
||||
return IntentionPowerPackBundle.message("convert.to.nested.if.intention.name");
|
||||
|
||||
Reference in New Issue
Block a user