diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertIfWithThrowToAssertIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertIfWithThrowToAssertIntention.kt index a86569b88b9..6ab897a2493 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertIfWithThrowToAssertIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertIfWithThrowToAssertIntention.kt @@ -19,7 +19,7 @@ package org.jetbrains.kotlin.idea.intentions import com.intellij.openapi.editor.Editor import org.jetbrains.kotlin.idea.util.ShortenReferences import org.jetbrains.kotlin.resolve.DescriptorUtils -import org.jetbrains.kotlin.idea.intentions.branchedTransformations.unwrapBlock +import org.jetbrains.kotlin.idea.intentions.branchedTransformations.unwrapBlockOrParenthesis import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isNullExpression import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.idea.caches.resolve.analyze @@ -32,7 +32,7 @@ public class ConvertIfWithThrowToAssertIntention : JetSelfTargetingOffsetIndepen override fun isApplicableTo(element: JetIfExpression): Boolean { if (element.getElse() != null) return false - val throwExpr = element.getThen()?.unwrapBlock() as? JetThrowExpression + val throwExpr = element.getThen()?.unwrapBlockOrParenthesis() as? JetThrowExpression val thrownExpr = getSelector(throwExpr?.getThrownExpression()) if (thrownExpr !is JetCallExpression) return false @@ -45,7 +45,7 @@ public class ConvertIfWithThrowToAssertIntention : JetSelfTargetingOffsetIndepen override fun applyTo(element: JetIfExpression, editor: Editor) { val condition = element.getCondition() ?: return - val thenExpr = element.getThen()?.unwrapBlock() as JetThrowExpression + val thenExpr = element.getThen()?.unwrapBlockOrParenthesis() as JetThrowExpression val thrownExpr = getSelector(thenExpr.getThrownExpression()) as JetCallExpression val psiFactory = JetPsiFactory(element) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/SimplifyBooleanWithConstantsIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/SimplifyBooleanWithConstantsIntention.kt index ff0338761da..5a7879f0a32 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/SimplifyBooleanWithConstantsIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/SimplifyBooleanWithConstantsIntention.kt @@ -89,18 +89,18 @@ public class SimplifyBooleanWithConstantsIntention : JetSelfTargetingOffsetIndep if (left != null && right != null && (op == JetTokens.ANDAND || op == JetTokens.OROR)) { val simpleLeft = simplifyExpression(left) val simpleRight = simplifyExpression(right) - when { - simpleLeft.canBeReducedToTrue() -> return toSimplifiedBooleanBinaryExpressionWithConstantOperand(true, simpleRight, op) + return when { + simpleLeft.canBeReducedToTrue() -> toSimplifiedBooleanBinaryExpressionWithConstantOperand(true, simpleRight, op) - simpleLeft.canBeReducedToFalse() -> return toSimplifiedBooleanBinaryExpressionWithConstantOperand(false, simpleRight, op) + simpleLeft.canBeReducedToFalse() -> toSimplifiedBooleanBinaryExpressionWithConstantOperand(false, simpleRight, op) - simpleRight.canBeReducedToTrue() -> return toSimplifiedBooleanBinaryExpressionWithConstantOperand(true, simpleLeft, op) + simpleRight.canBeReducedToTrue() -> toSimplifiedBooleanBinaryExpressionWithConstantOperand(true, simpleLeft, op) - simpleRight.canBeReducedToFalse() -> return toSimplifiedBooleanBinaryExpressionWithConstantOperand(false, simpleLeft, op) + simpleRight.canBeReducedToFalse() -> toSimplifiedBooleanBinaryExpressionWithConstantOperand(false, simpleLeft, op) else -> { val opText = expression.getOperationReference().getText() - return psiFactory.createExpressionByPattern("$0 $opText $1", simpleLeft, simpleRight) + psiFactory.createExpressionByPattern("$0 $opText $1", simpleLeft, simpleRight) } } } @@ -118,7 +118,7 @@ public class SimplifyBooleanWithConstantsIntention : JetSelfTargetingOffsetIndep } } - private fun simplifyExpression(element: JetExpression) = element.replaced(toSimplifiedExpression(element)) + private fun simplifyExpression(expression: JetExpression) = expression.replaced(toSimplifiedExpression(expression)) private fun JetExpression.canBeReducedToBooleanConstant(constant: Boolean?): Boolean { val bindingContext = this.analyze() diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt index f2314edb5cd..4bf2a4134ed 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt @@ -50,7 +50,7 @@ fun JetBinaryExpression.expressionComparedToNull(): JetExpression? { return if (leftIsNull) right else left } -fun JetExpression.unwrapBlock(): JetExpression { +fun JetExpression.unwrapBlockOrParenthesis(): JetExpression { val innerExpression = JetPsiUtil.safeDeparenthesize(this) if (innerExpression is JetBlockExpression) { val statement = innerExpression.getStatements().singleOrNull() as? JetExpression ?: return this @@ -59,7 +59,7 @@ fun JetExpression.unwrapBlock(): JetExpression { return innerExpression } -fun JetExpression?.isNullExpression(): Boolean = this?.unwrapBlock()?.getNode()?.getElementType() == JetNodeTypes.NULL +fun JetExpression?.isNullExpression(): Boolean = this?.unwrapBlockOrParenthesis()?.getNode()?.getElementType() == JetNodeTypes.NULL fun JetExpression?.isNullExpressionOrEmptyBlock(): Boolean = this.isNullExpression() || this is JetBlockExpression && this.getStatements().isEmpty() @@ -76,7 +76,7 @@ fun JetThrowExpression.throwsNullPointerExceptionWithNoArguments(): Boolean { } fun JetExpression.evaluatesTo(other: JetExpression): Boolean { - return this.unwrapBlock().getText() == other.getText() + return this.unwrapBlockOrParenthesis().getText() == other.getText() } fun JetExpression.convertToIfNotNullExpression(conditionLhs: JetExpression, thenClause: JetExpression, elseClause: JetExpression?): JetIfExpression { diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToDoubleBangIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToDoubleBangIntention.kt index f1ece36cad3..c19bf14d252 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToDoubleBangIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToDoubleBangIntention.kt @@ -41,13 +41,13 @@ public class IfThenToDoubleBangIntention : JetSelfTargetingRangeIntention { - throwExpression = thenClause.unwrapBlock() as? JetThrowExpression ?: return null + throwExpression = thenClause.unwrapBlockOrParenthesis() as? JetThrowExpression ?: return null matchingClause = elseClause } JetTokens.EXCLEQ -> { matchingClause = thenClause - throwExpression = elseClause?.unwrapBlock() as? JetThrowExpression ?: return null + throwExpression = elseClause?.unwrapBlockOrParenthesis() as? JetThrowExpression ?: return null } else -> throw IllegalStateException() diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToElvisIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToElvisIntention.kt index f8c0f7e2752..137b5467d00 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToElvisIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToElvisIntention.kt @@ -52,7 +52,7 @@ public class IfThenToElvisIntention : JetSelfTargetingOffsetIndependentIntention } private fun JetExpression.isNotNullExpression(): Boolean { - val innerExpression = this.unwrapBlock() + val innerExpression = this.unwrapBlockOrParenthesis() return innerExpression !is JetBlockExpression && innerExpression.getNode().getElementType() != JetNodeTypes.NULL } @@ -66,8 +66,8 @@ public class IfThenToElvisIntention : JetSelfTargetingOffsetIndependentIntention val thenClause = element.getThen()!! val elseClause = element.getElse()!! - val thenExpression = thenClause.unwrapBlock() - val elseExpression = elseClause.unwrapBlock() + val thenExpression = thenClause.unwrapBlockOrParenthesis() + val elseExpression = elseClause.unwrapBlockOrParenthesis() val (left, right) = when(condition.getOperationToken()) { diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToSafeAccessIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToSafeAccessIntention.kt index 922bc4d6f3a..5005d4c6257 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToSafeAccessIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToSafeAccessIntention.kt @@ -76,7 +76,7 @@ public class IfThenToSafeAccessIntention : JetSelfTargetingOffsetIndependentInte = findSelectorExpressionInClause(clause, receiverExpression) != null private fun findSelectorExpressionInClause(clause: JetExpression, receiverExpression: JetExpression): JetExpression? { - val expression = clause.unwrapBlock() as? JetDotQualifiedExpression ?: return null + val expression = clause.unwrapBlockOrParenthesis() as? JetDotQualifiedExpression ?: return null if (expression.getReceiverExpression().getText() != receiverExpression.getText()) return null