WhenToIfIntention - smaller range and refactoring

This commit is contained in:
Valentin Kipyatkov
2015-05-06 11:12:16 +03:00
parent 76fc0c1418
commit ea6b632cea
3 changed files with 34 additions and 41 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
when.to.if=Replace 'when' with 'if'
when.to.if.family=Replace 'when' with 'if'
merge.when=Merge 'when' expressions
merge.when.family=Merge 'when' Expression
introduce.when.subject=Introduce argument to 'when'
@@ -24,7 +24,6 @@ import org.jetbrains.kotlin.idea.util.psi.patternMatching.toRange
import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.JetPsiUnparsingUtils.parenthesizeIfNeeded
import org.jetbrains.kotlin.psi.JetPsiUnparsingUtils.parenthesizeTextIfNeeded
import org.jetbrains.kotlin.psi.JetPsiUnparsingUtils.toBinaryExpression
import org.jetbrains.kotlin.psi.psiUtil.*
@@ -168,37 +167,6 @@ public fun JetWhenExpression.introduceSubject(): JetWhenExpression {
return replaced(whenExpression)
}
public fun JetWhenExpression.canTransformToIf(): Boolean = !getEntries().isEmpty()
public fun JetWhenExpression.transformToIf() {
fun combineWhenConditions(conditions: Array<JetWhenCondition>, subject: JetExpression?): String {
return when (conditions.size()) {
0 -> ""
1 -> conditions[0].toExpressionText(subject)
else -> {
conditions
.map { condition -> parenthesizeTextIfNeeded(condition.toExpressionText(subject)) }
.joinToString(separator = " || ")
}
}
}
val builder = JetPsiFactory(this).IfChainBuilder()
for (entry in getEntries()) {
val branch = entry.getExpression()
if (entry.isElse()) {
builder.elseBranch(branch)
}
else {
val branchConditionText = combineWhenConditions(entry.getConditions(), getSubjectExpression())
builder.ifBranch(branchConditionText, JetPsiUtil.getText(branch))
}
}
replace(builder.toExpression())
}
public fun JetWhenExpression.canMergeWithNext(): Boolean {
fun checkConditions(e1: JetWhenEntry, e2: JetWhenEntry): Boolean =
e1.getConditions().toList().toRange().matches(e2.getConditions().toList().toRange())
@@ -17,15 +17,42 @@
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.canTransformToIf
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.transformToIf
import org.jetbrains.kotlin.psi.JetWhenExpression
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingIntention
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.toExpressionText
import org.jetbrains.kotlin.psi.*
public class WhenToIfIntention : JetSelfTargetingOffsetIndependentIntention<JetWhenExpression>("when.to.if", javaClass()) {
override fun isApplicableTo(element: JetWhenExpression): Boolean = element.canTransformToIf()
public class WhenToIfIntention : JetSelfTargetingIntention<JetWhenExpression>(javaClass(), "Replace 'when' with 'if'") {
override fun isApplicableTo(element: JetWhenExpression, caretOffset: Int): Boolean {
if (element.getEntries().isEmpty()) return false
return element.getWhenKeywordElement().getTextRange().containsOffset(caretOffset)
}
override fun applyTo(element: JetWhenExpression, editor: Editor) {
element.transformToIf()
val builder = JetPsiFactory(element).IfChainBuilder()
for (entry in element.getEntries()) {
val branch = entry.getExpression()
if (entry.isElse()) {
builder.elseBranch(branch)
}
else {
val branchConditionText = combineWhenConditions(entry.getConditions(), element.getSubjectExpression())
builder.ifBranch(branchConditionText, JetPsiUtil.getText(branch))
}
}
element.replace(builder.toExpression())
}
private fun combineWhenConditions(conditions: Array<JetWhenCondition>, subject: JetExpression?): String {
return when (conditions.size()) {
0 -> ""
1 -> conditions[0].toExpressionText(subject)
else -> {
conditions
.map { condition -> JetPsiUnparsingUtils.parenthesizeTextIfNeeded(condition.toExpressionText(subject)) }
.joinToString(separator = " || ")
}
}
}
}