Minor changes after review

This commit is contained in:
Valentin Kipyatkov
2015-05-13 12:52:45 +03:00
parent d09c1e7c5b
commit d67b678a65
6 changed files with 19 additions and 19 deletions
@@ -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)
@@ -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()
@@ -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 {
@@ -41,13 +41,13 @@ public class IfThenToDoubleBangIntention : JetSelfTargetingRangeIntention<JetIfE
val matchingClause: JetExpression?
when (token) {
JetTokens.EQEQ -> {
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()
@@ -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()) {
@@ -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