Refactored IntroduceWhenSubjectIntention
This commit is contained in:
@@ -250,8 +250,6 @@ 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
|
||||
merge.when=Merge 'when' expressions
|
||||
merge.when.family=Merge 'when' Expression
|
||||
introduce.when.subject=Introduce argument to 'when'
|
||||
introduce.when.subject.family=Introduce Argument to 'when'
|
||||
transform.if.statement.with.assignments.to.expression=Transform 'if' statement with assignments to expression
|
||||
transform.assignment.with.if.expression.to.statement=Transform assignment with 'if' expression to statement
|
||||
transform.if.statement.with.assignments.to.expression.family=Transform 'if' Statement with Assignments to Expression
|
||||
|
||||
+26
-35
@@ -26,8 +26,6 @@ import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.*
|
||||
import org.jetbrains.kotlin.psi.typeRefHelpers.getTypeReference
|
||||
|
||||
public val TRANSFORM_WITHOUT_CHECK: String = "Expression must be checked before applying transformation"
|
||||
|
||||
fun JetWhenCondition.toExpression(subject: JetExpression?): JetExpression {
|
||||
val factory = JetPsiFactory(this)
|
||||
when (this) {
|
||||
@@ -54,29 +52,7 @@ fun JetWhenCondition.toExpression(subject: JetExpression?): JetExpression {
|
||||
}
|
||||
}
|
||||
|
||||
fun JetWhenExpression.getSubjectCandidate(): JetExpression? {
|
||||
fun JetExpression?.getWhenConditionSubjectCandidate(): JetExpression? {
|
||||
return when(this) {
|
||||
is JetIsExpression -> getLeftHandSide()
|
||||
is JetBinaryExpression -> {
|
||||
val lhs = getLeft()
|
||||
val op = getOperationToken()
|
||||
when (op) {
|
||||
JetTokens.IN_KEYWORD, JetTokens.NOT_IN -> lhs
|
||||
JetTokens.EQEQ -> {
|
||||
if (lhs is JetSimpleNameExpression)
|
||||
lhs
|
||||
else
|
||||
getRight()
|
||||
}
|
||||
else -> null
|
||||
}
|
||||
|
||||
}
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
public fun JetWhenExpression.getSubjectToIntroduce(): JetExpression? {
|
||||
if (getSubjectExpression() != null) return null
|
||||
|
||||
var lastCandidate: JetExpression? = null
|
||||
@@ -87,13 +63,14 @@ fun JetWhenExpression.getSubjectCandidate(): JetExpression? {
|
||||
for (condition in conditions) {
|
||||
if (condition !is JetWhenConditionWithExpression) return null
|
||||
|
||||
val currCandidate = condition.getExpression().getWhenConditionSubjectCandidate()
|
||||
if (currCandidate !is JetSimpleNameExpression) return null
|
||||
val candidate = condition.getExpression()?.getWhenConditionSubjectCandidate() as? JetSimpleNameExpression ?: return null
|
||||
|
||||
if (lastCandidate == null) {
|
||||
lastCandidate = currCandidate
|
||||
lastCandidate = candidate
|
||||
}
|
||||
else if (!lastCandidate.matches(candidate)) {
|
||||
return null
|
||||
}
|
||||
else if (!lastCandidate.matches(currCandidate)) return null
|
||||
|
||||
}
|
||||
}
|
||||
@@ -101,12 +78,27 @@ fun JetWhenExpression.getSubjectCandidate(): JetExpression? {
|
||||
return lastCandidate
|
||||
}
|
||||
|
||||
public fun JetWhenExpression.canIntroduceSubject(): Boolean {
|
||||
return getSubjectCandidate() != null
|
||||
private fun JetExpression?.getWhenConditionSubjectCandidate(): JetExpression? {
|
||||
return when(this) {
|
||||
is JetIsExpression -> getLeftHandSide()
|
||||
|
||||
is JetBinaryExpression -> {
|
||||
val lhs = getLeft()
|
||||
val op = getOperationToken()
|
||||
when (op) {
|
||||
JetTokens.IN_KEYWORD, JetTokens.NOT_IN -> lhs
|
||||
JetTokens.EQEQ -> lhs as? JetSimpleNameExpression ?: getRight()
|
||||
else -> null
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
public fun JetWhenExpression.introduceSubject(): JetWhenExpression {
|
||||
val subject = getSubjectCandidate()!!
|
||||
val subject = getSubjectToIntroduce()!!
|
||||
|
||||
val whenExpression = JetPsiFactory(this).buildExpression {
|
||||
appendFixedText("when(").appendExpression(subject).appendFixedText("){\n")
|
||||
@@ -120,7 +112,6 @@ public fun JetWhenExpression.introduceSubject(): JetWhenExpression {
|
||||
else {
|
||||
for ((i, condition) in entry.getConditions().withIndex()) {
|
||||
if (i > 0) appendFixedText(",")
|
||||
assert(condition is JetWhenConditionWithExpression, TRANSFORM_WITHOUT_CHECK)
|
||||
|
||||
val conditionExpression = (condition as JetWhenConditionWithExpression).getExpression()
|
||||
when (conditionExpression) {
|
||||
@@ -140,11 +131,11 @@ public fun JetWhenExpression.introduceSubject(): JetWhenExpression {
|
||||
JetTokens.IN_KEYWORD -> appendFixedText("in ").appendExpression(rhs)
|
||||
JetTokens.NOT_IN -> appendFixedText("!in ").appendExpression(rhs)
|
||||
JetTokens.EQEQ -> appendExpression(if (subject.matches(lhs)) rhs else lhs)
|
||||
else -> error(TRANSFORM_WITHOUT_CHECK)
|
||||
else -> throw IllegalStateException()
|
||||
}
|
||||
}
|
||||
|
||||
else -> error(TRANSFORM_WITHOUT_CHECK)
|
||||
else -> throw IllegalStateException()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -18,7 +18,7 @@ package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions
|
||||
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingIntention
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.canIntroduceSubject
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.getSubjectToIntroduce
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.introduceSubject
|
||||
import org.jetbrains.kotlin.lexer.JetTokens
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
@@ -68,7 +68,7 @@ public class IfToWhenIntention : JetSelfTargetingIntention<JetIfExpression>(java
|
||||
} as JetWhenExpression
|
||||
|
||||
|
||||
if (whenExpression.canIntroduceSubject()) {
|
||||
if (whenExpression.getSubjectToIntroduce() != null) {
|
||||
whenExpression = whenExpression.introduceSubject()
|
||||
}
|
||||
|
||||
|
||||
+7
-3
@@ -18,12 +18,16 @@ package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions
|
||||
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.canIntroduceSubject
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.getSubjectToIntroduce
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.introduceSubject
|
||||
import org.jetbrains.kotlin.psi.JetWhenExpression
|
||||
|
||||
public class IntroduceWhenSubjectIntention : JetSelfTargetingOffsetIndependentIntention<JetWhenExpression>("introduce.when.subject", javaClass()) {
|
||||
override fun isApplicableTo(element: JetWhenExpression): Boolean = element.canIntroduceSubject()
|
||||
public class IntroduceWhenSubjectIntention : JetSelfTargetingOffsetIndependentIntention<JetWhenExpression>(javaClass(), "Introduce argument to 'when'") {
|
||||
override fun isApplicableTo(element: JetWhenExpression): Boolean {
|
||||
val subject = element.getSubjectToIntroduce() ?: return false
|
||||
setText("Introduce '$subject' as argument to 'when'")
|
||||
return true
|
||||
}
|
||||
|
||||
override fun applyTo(element: JetWhenExpression, editor: Editor) {
|
||||
element.introduceSubject()
|
||||
|
||||
Reference in New Issue
Block a user