Refactored IfThenToSafeAccessIntention

This commit is contained in:
Valentin Kipyatkov
2015-05-05 22:39:04 +03:00
parent f2cced4384
commit 3702add034
2 changed files with 13 additions and 22 deletions
@@ -248,8 +248,6 @@ elvis.to.if.then=Replace elvis expression with 'if' expression
elvis.to.if.then.family=Replace Elvis Expression with 'if' Expression
safe.access.to.if.then=Replace safe access expression with 'if' expression
safe.access.to.if.then.family=Replace Safe Access Expression with 'if' Expression
if.then.to.safe.access=Replace 'if' expression with safe access expression
if.then.to.safe.access.family=Replace 'if' Expression with Safe Access Expression
if.to.when=Replace 'if' with 'when'
if.to.when.family=Replace 'if' with 'when'
when.to.if=Replace 'when' with 'if'
@@ -21,8 +21,9 @@ import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentInt
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.*
import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.replaced
public class IfThenToSafeAccessIntention : JetSelfTargetingOffsetIndependentIntention<JetIfExpression>("if.then.to.safe.access", javaClass()) {
public class IfThenToSafeAccessIntention : JetSelfTargetingOffsetIndependentIntention<JetIfExpression>(javaClass(), "Replace 'if' expression with safe access expression") {
override fun isApplicableTo(element: JetIfExpression): Boolean {
val condition = element.getCondition() as? JetBinaryExpression ?: return false
@@ -57,33 +58,25 @@ public class IfThenToSafeAccessIntention : JetSelfTargetingOffsetIndependentInte
val selectorExpression =
when(condition.getOperationToken()) {
JetTokens.EQEQ -> findSelectorExpressionInClause(element.getElse()!!, receiverExpression)!!
JetTokens.EQEQ -> {
val elseClause = checkNotNull(element.getElse(), "The else clause cannot be null")
findSelectorExpressionInClause(elseClause, receiverExpression)
}
JetTokens.EXCLEQ -> findSelectorExpressionInClause(element.getThen()!!, receiverExpression)!!
JetTokens.EXCLEQ -> {
val thenClause = checkNotNull(element.getThen(), "The then clause cannot be null")
findSelectorExpressionInClause(thenClause, receiverExpression)
}
else ->
throw IllegalStateException("Operation token must be either null or not null")
else -> throw IllegalArgumentException()
}
val resultingExprString = "${receiverExpression.getText()}?.${selectorExpression?.getText()}"
return element.replace(resultingExprString) as JetSafeQualifiedExpression
val newExpr = JetPsiFactory(element).createExpressionByPattern("$0?.$1", receiverExpression, selectorExpression) as JetSafeQualifiedExpression
return element.replaced(newExpr)
}
fun clauseContainsAppropriateDotQualifiedExpression(clause: JetExpression, receiverExpression: JetExpression): Boolean =
findSelectorExpressionInClause(clause, receiverExpression) != null
private fun clauseContainsAppropriateDotQualifiedExpression(clause: JetExpression, receiverExpression: JetExpression)
= findSelectorExpressionInClause(clause, receiverExpression) != null
fun findSelectorExpressionInClause(clause: JetExpression, receiverExpression: JetExpression): JetExpression? {
val expression = clause.unwrapBlock() as? JetDotQualifiedExpression
private fun findSelectorExpressionInClause(clause: JetExpression, receiverExpression: JetExpression): JetExpression? {
val expression = clause.unwrapBlock() as? JetDotQualifiedExpression ?: return null
if (expression?.getReceiverExpression()?.getText() != receiverExpression.getText()) return null
if (expression.getReceiverExpression().getText() != receiverExpression.getText()) return null
return expression?.getSelectorExpression()
return expression.getSelectorExpression()
}
}