summaryrefslogtreecommitdiff
path: root/plugins/IntentionPowerPak
diff options
context:
space:
mode:
authorTor Norbye <tnorbye@google.com>2013-04-19 13:32:49 -0700
committerTor Norbye <tnorbye@google.com>2013-04-19 13:32:49 -0700
commitb569bc6aa78f6eacf72e8b90622d300e1a9db25f (patch)
tree309048e52eab434a381cf8554436f14de6342cbf /plugins/IntentionPowerPak
parentd1a59a0799588a226d255d9b45c4825b19651554 (diff)
downloadidea-b569bc6aa78f6eacf72e8b90622d300e1a9db25f.tar.gz
Snapshot 0b0329a61f47b6070bb9084be8e176635a16fa5c from master branch of git://git.jetbrains.org/idea/community.git
Change-Id: I2e8857776f197bed2d768ea37c67d2e2a1e97952
Diffstat (limited to 'plugins/IntentionPowerPak')
-rw-r--r--plugins/IntentionPowerPak/src/com/siyeh/ipp/trivialif/ConvertToNestedIfIntention.java17
-rw-r--r--plugins/IntentionPowerPak/test/com/siyeh/ipp/trivialif/convert_to_nested_if/AndOrMixed.java6
-rw-r--r--plugins/IntentionPowerPak/test/com/siyeh/ipp/trivialif/convert_to_nested_if/AndOrMixed_after.java8
-rw-r--r--plugins/IntentionPowerPak/test/com/siyeh/ipp/trivialif/convert_to_nested_if/Nested_after.java2
-rw-r--r--plugins/IntentionPowerPak/test/com/siyeh/ipp/trivialif/convert_to_nested_if/OrAndMixed.java6
-rw-r--r--plugins/IntentionPowerPak/test/com/siyeh/ipp/trivialif/convert_to_nested_if/OrAndMixed_after.java8
-rw-r--r--plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/trivialif/ConvertToNestedIfIntentionTest.java3
7 files changed, 41 insertions, 9 deletions
diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/trivialif/ConvertToNestedIfIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/trivialif/ConvertToNestedIfIntention.java
index fed8918e26aa..d911c0d1ab72 100644
--- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/trivialif/ConvertToNestedIfIntention.java
+++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/trivialif/ConvertToNestedIfIntention.java
@@ -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;");
diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/trivialif/convert_to_nested_if/AndOrMixed.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/trivialif/convert_to_nested_if/AndOrMixed.java
new file mode 100644
index 000000000000..08624319e916
--- /dev/null
+++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/trivialif/convert_to_nested_if/AndOrMixed.java
@@ -0,0 +1,6 @@
+public class Test {
+ boolean A, B, C;
+ boolean f() {
+ return A &<caret>& B || C;
+ }
+}
diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/trivialif/convert_to_nested_if/AndOrMixed_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/trivialif/convert_to_nested_if/AndOrMixed_after.java
new file mode 100644
index 000000000000..674db9265a09
--- /dev/null
+++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/trivialif/convert_to_nested_if/AndOrMixed_after.java
@@ -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;
+ }
+}
diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/trivialif/convert_to_nested_if/Nested_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/trivialif/convert_to_nested_if/Nested_after.java
index 2fec2c096d6d..82c3763205e1 100644
--- a/plugins/IntentionPowerPak/test/com/siyeh/ipp/trivialif/convert_to_nested_if/Nested_after.java
+++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/trivialif/convert_to_nested_if/Nested_after.java
@@ -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;
}
} \ No newline at end of file
diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/trivialif/convert_to_nested_if/OrAndMixed.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/trivialif/convert_to_nested_if/OrAndMixed.java
new file mode 100644
index 000000000000..72cbc88c75c5
--- /dev/null
+++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/trivialif/convert_to_nested_if/OrAndMixed.java
@@ -0,0 +1,6 @@
+public class Test {
+ boolean A, B, C;
+ boolean f() {
+ return C || A &<caret>& B;
+ }
+}
diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/trivialif/convert_to_nested_if/OrAndMixed_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/trivialif/convert_to_nested_if/OrAndMixed_after.java
new file mode 100644
index 000000000000..e2b75a2fc639
--- /dev/null
+++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/trivialif/convert_to_nested_if/OrAndMixed_after.java
@@ -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;
+ }
+}
diff --git a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/trivialif/ConvertToNestedIfIntentionTest.java b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/trivialif/ConvertToNestedIfIntentionTest.java
index 338f4da9b4e9..84b140376ff4 100644
--- a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/trivialif/ConvertToNestedIfIntentionTest.java
+++ b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/trivialif/ConvertToNestedIfIntentionTest.java
@@ -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");