From f44ba60d5126d2c213b90c0a447c2c82c303620e Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Sun, 20 Jul 2014 19:09:34 +0400 Subject: [PATCH] Fix SimplifyBooleanWithConstantsIntention intention to avoid analyzing files created by JetPsiFactory --- .../jet/lang/psi/psiUtil/jetPsiUtil.kt | 3 ++ .../SimplifyBooleanWithConstantsIntention.kt | 38 ++++++++++--------- .../deeplyParenthesized.kt | 5 +++ .../deeplyParenthesized.kt.after | 5 +++ .../intentions/IntentionTestGenerated.java | 5 +++ 5 files changed, 39 insertions(+), 17 deletions(-) create mode 100644 idea/testData/intentions/simplifyBooleanWithConstants/deeplyParenthesized.kt create mode 100644 idea/testData/intentions/simplifyBooleanWithConstants/deeplyParenthesized.kt.after diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/psiUtil/jetPsiUtil.kt b/compiler/frontend/src/org/jetbrains/jet/lang/psi/psiUtil/jetPsiUtil.kt index cf41f24c4b4..7e6e013dddf 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/psiUtil/jetPsiUtil.kt +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/psiUtil/jetPsiUtil.kt @@ -96,6 +96,9 @@ public fun JetClass.isAbstract(): Boolean = isTrait() || hasModifier(JetTokens.A [suppress("UNCHECKED_CAST")] public fun PsiElement.replaced(newElement: T): T = replace(newElement) as T +[suppress("UNCHECKED_CAST")] +public fun T.copied(): T = copy() as T + public fun JetElement.blockExpressionsOrSingle(): Stream = if (this is JetBlockExpression) getStatements().stream() else listOf(this).stream() diff --git a/idea/src/org/jetbrains/jet/plugin/intentions/SimplifyBooleanWithConstantsIntention.kt b/idea/src/org/jetbrains/jet/plugin/intentions/SimplifyBooleanWithConstantsIntention.kt index bc16812f7ba..86365963132 100644 --- a/idea/src/org/jetbrains/jet/plugin/intentions/SimplifyBooleanWithConstantsIntention.kt +++ b/idea/src/org/jetbrains/jet/plugin/intentions/SimplifyBooleanWithConstantsIntention.kt @@ -27,6 +27,8 @@ import org.jetbrains.jet.lang.resolve.DelegatingBindingTrace import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache import com.intellij.psi.tree.IElementType import com.intellij.psi.util.PsiTreeUtil +import org.jetbrains.jet.lang.psi.psiUtil.copied +import org.jetbrains.jet.lang.psi.psiUtil.replaced public class SimplifyBooleanWithConstantsIntention : JetSelfTargetingIntention( "simplify.boolean.with.constants", javaClass()) { @@ -54,7 +56,7 @@ public class SimplifyBooleanWithConstantsIntention : JetSelfTargetingIntention { val expr = element.getExpression() if (expr == null) return element - val simplified = simplifyBoolean(expr) - if (expr == simplified) return element - if (simplified is JetBinaryExpression) { - val simpText = simplified.getText() - if (simpText == null) return element + val innerSimplified = toSimplifiedExpression(expr) + if (innerSimplified is JetBinaryExpression) { + val simpText = innerSimplified.getText() ?: return element.copied() // wrap in new parentheses to keep the user's original format return psiFactory.createExpression("($simpText)") } // if we now have a simpleName, constant, or parenthesized we don't need parentheses - return simplified + return innerSimplified } is JetBinaryExpression -> { val left = element.getLeft() val right = element.getRight() val op = element.getOperationToken() if (left == null || right == null || op == null || (op != JetTokens.ANDAND && op != JetTokens.OROR)) - return element + return element.copied() - val simpleLeft = simplifyBoolean(left) - val simpleRight = simplifyBoolean(right) + val simpleLeft = simplifyExpression(left) + val simpleRight = simplifyExpression(right) if (simpleLeft.canBeReducedToTrue() || simpleLeft.canBeReducedToFalse()) - return simplifyBooleanBinaryExpressionWithConstantOperand(simpleLeft, simpleRight, op) + return toSimplifiedBooleanBinaryExpressionWithConstantOperand(simpleLeft, simpleRight, op) if (simpleRight.canBeReducedToTrue() || simpleRight.canBeReducedToFalse()) - return simplifyBooleanBinaryExpressionWithConstantOperand(simpleRight, simpleLeft, op) + return toSimplifiedBooleanBinaryExpressionWithConstantOperand(simpleRight, simpleLeft, op) val opText = element.getOperationReference().getText() - if (opText == null) return element + if (opText == null) return element.copied() return psiFactory.createBinaryExpression(simpleLeft, opText, simpleRight) } - else -> return element + else -> return element.copied() } } - private fun simplifyBooleanBinaryExpressionWithConstantOperand( + private fun toSimplifiedBooleanBinaryExpressionWithConstantOperand( booleanConstantOperand: JetExpression, otherOperand: JetExpression, operation: IElementType @@ -120,7 +120,11 @@ public class SimplifyBooleanWithConstantsIntention : JetSelfTargetingIntention|| false) +} \ No newline at end of file diff --git a/idea/testData/intentions/simplifyBooleanWithConstants/deeplyParenthesized.kt.after b/idea/testData/intentions/simplifyBooleanWithConstants/deeplyParenthesized.kt.after new file mode 100644 index 00000000000..21b5d7e22ae --- /dev/null +++ b/idea/testData/intentions/simplifyBooleanWithConstants/deeplyParenthesized.kt.after @@ -0,0 +1,5 @@ +fun foo() { + val x = true + val y = false + (x && y) +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/intentions/IntentionTestGenerated.java index 3eeb133d029..85ddc96d065 100644 --- a/idea/tests/org/jetbrains/jet/plugin/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/intentions/IntentionTestGenerated.java @@ -4514,6 +4514,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/intentions/simplifyBooleanWithConstants"), Pattern.compile("^(.+)\\.kt$"), true); } + @TestMetadata("deeplyParenthesized.kt") + public void testDeeplyParenthesized() throws Exception { + doTest("idea/testData/intentions/simplifyBooleanWithConstants/deeplyParenthesized.kt"); + } + @TestMetadata("inapplicableNoConstants.kt") public void testInapplicableNoConstants() throws Exception { doTest("idea/testData/intentions/simplifyBooleanWithConstants/inapplicableNoConstants.kt");