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 com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.util.ShortenReferences import org.jetbrains.kotlin.idea.util.ShortenReferences
import org.jetbrains.kotlin.resolve.DescriptorUtils 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.idea.intentions.branchedTransformations.isNullExpression
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.analyze
@@ -32,7 +32,7 @@ public class ConvertIfWithThrowToAssertIntention : JetSelfTargetingOffsetIndepen
override fun isApplicableTo(element: JetIfExpression): Boolean { override fun isApplicableTo(element: JetIfExpression): Boolean {
if (element.getElse() != null) return false 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()) val thrownExpr = getSelector(throwExpr?.getThrownExpression())
if (thrownExpr !is JetCallExpression) return false if (thrownExpr !is JetCallExpression) return false
@@ -45,7 +45,7 @@ public class ConvertIfWithThrowToAssertIntention : JetSelfTargetingOffsetIndepen
override fun applyTo(element: JetIfExpression, editor: Editor) { override fun applyTo(element: JetIfExpression, editor: Editor) {
val condition = element.getCondition() ?: return 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 thrownExpr = getSelector(thenExpr.getThrownExpression()) as JetCallExpression
val psiFactory = JetPsiFactory(element) val psiFactory = JetPsiFactory(element)
@@ -89,18 +89,18 @@ public class SimplifyBooleanWithConstantsIntention : JetSelfTargetingOffsetIndep
if (left != null && right != null && (op == JetTokens.ANDAND || op == JetTokens.OROR)) { if (left != null && right != null && (op == JetTokens.ANDAND || op == JetTokens.OROR)) {
val simpleLeft = simplifyExpression(left) val simpleLeft = simplifyExpression(left)
val simpleRight = simplifyExpression(right) val simpleRight = simplifyExpression(right)
when { return when {
simpleLeft.canBeReducedToTrue() -> return toSimplifiedBooleanBinaryExpressionWithConstantOperand(true, simpleRight, op) 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 -> { else -> {
val opText = expression.getOperationReference().getText() 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 { private fun JetExpression.canBeReducedToBooleanConstant(constant: Boolean?): Boolean {
val bindingContext = this.analyze() val bindingContext = this.analyze()
@@ -50,7 +50,7 @@ fun JetBinaryExpression.expressionComparedToNull(): JetExpression? {
return if (leftIsNull) right else left return if (leftIsNull) right else left
} }
fun JetExpression.unwrapBlock(): JetExpression { fun JetExpression.unwrapBlockOrParenthesis(): JetExpression {
val innerExpression = JetPsiUtil.safeDeparenthesize(this) val innerExpression = JetPsiUtil.safeDeparenthesize(this)
if (innerExpression is JetBlockExpression) { if (innerExpression is JetBlockExpression) {
val statement = innerExpression.getStatements().singleOrNull() as? JetExpression ?: return this val statement = innerExpression.getStatements().singleOrNull() as? JetExpression ?: return this
@@ -59,7 +59,7 @@ fun JetExpression.unwrapBlock(): JetExpression {
return innerExpression 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() 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 { 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 { fun JetExpression.convertToIfNotNullExpression(conditionLhs: JetExpression, thenClause: JetExpression, elseClause: JetExpression?): JetIfExpression {
@@ -41,13 +41,13 @@ public class IfThenToDoubleBangIntention : JetSelfTargetingRangeIntention<JetIfE
val matchingClause: JetExpression? val matchingClause: JetExpression?
when (token) { when (token) {
JetTokens.EQEQ -> { JetTokens.EQEQ -> {
throwExpression = thenClause.unwrapBlock() as? JetThrowExpression ?: return null throwExpression = thenClause.unwrapBlockOrParenthesis() as? JetThrowExpression ?: return null
matchingClause = elseClause matchingClause = elseClause
} }
JetTokens.EXCLEQ -> { JetTokens.EXCLEQ -> {
matchingClause = thenClause matchingClause = thenClause
throwExpression = elseClause?.unwrapBlock() as? JetThrowExpression ?: return null throwExpression = elseClause?.unwrapBlockOrParenthesis() as? JetThrowExpression ?: return null
} }
else -> throw IllegalStateException() else -> throw IllegalStateException()
@@ -52,7 +52,7 @@ public class IfThenToElvisIntention : JetSelfTargetingOffsetIndependentIntention
} }
private fun JetExpression.isNotNullExpression(): Boolean { private fun JetExpression.isNotNullExpression(): Boolean {
val innerExpression = this.unwrapBlock() val innerExpression = this.unwrapBlockOrParenthesis()
return innerExpression !is JetBlockExpression && innerExpression.getNode().getElementType() != JetNodeTypes.NULL return innerExpression !is JetBlockExpression && innerExpression.getNode().getElementType() != JetNodeTypes.NULL
} }
@@ -66,8 +66,8 @@ public class IfThenToElvisIntention : JetSelfTargetingOffsetIndependentIntention
val thenClause = element.getThen()!! val thenClause = element.getThen()!!
val elseClause = element.getElse()!! val elseClause = element.getElse()!!
val thenExpression = thenClause.unwrapBlock() val thenExpression = thenClause.unwrapBlockOrParenthesis()
val elseExpression = elseClause.unwrapBlock() val elseExpression = elseClause.unwrapBlockOrParenthesis()
val (left, right) = val (left, right) =
when(condition.getOperationToken()) { when(condition.getOperationToken()) {
@@ -76,7 +76,7 @@ public class IfThenToSafeAccessIntention : JetSelfTargetingOffsetIndependentInte
= findSelectorExpressionInClause(clause, receiverExpression) != null = findSelectorExpressionInClause(clause, receiverExpression) != null
private fun findSelectorExpressionInClause(clause: JetExpression, receiverExpression: JetExpression): JetExpression? { 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 if (expression.getReceiverExpression().getText() != receiverExpression.getText()) return null