No use of CallChainBuilder anymore
This commit is contained in:
+24
-12
@@ -23,25 +23,37 @@ import org.jetbrains.kotlin.psi.*
|
||||
|
||||
public class WhenToIfIntention : JetSelfTargetingIntention<JetWhenExpression>(javaClass(), "Replace 'when' with 'if'") {
|
||||
override fun isApplicableTo(element: JetWhenExpression, caretOffset: Int): Boolean {
|
||||
if (element.getEntries().isEmpty()) return false
|
||||
val entries = element.getEntries()
|
||||
if (entries.isEmpty()) return false
|
||||
val lastEntry = entries.last()
|
||||
if (entries.any { it != lastEntry && it.isElse() }) return false
|
||||
|
||||
return element.getWhenKeywordElement().getTextRange().containsOffset(caretOffset)
|
||||
}
|
||||
|
||||
override fun applyTo(element: JetWhenExpression, editor: Editor) {
|
||||
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))
|
||||
val ifExpression = JetPsiFactory(element).buildExpression {
|
||||
for ((i, entry) in element.getEntries().withIndex()) {
|
||||
if (i > 0) {
|
||||
appendFixedText("else ")
|
||||
}
|
||||
val branch = entry.getExpression()
|
||||
if (entry.isElse()) {
|
||||
appendExpression(branch)
|
||||
appendFixedText("\n")
|
||||
}
|
||||
else {
|
||||
val branchConditionText = combineWhenConditions(entry.getConditions(), element.getSubjectExpression())
|
||||
appendFixedText("if (")
|
||||
appendNonFormattedText(branchConditionText)
|
||||
appendFixedText(")")
|
||||
appendExpression(branch)
|
||||
appendFixedText("\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
element.replace(builder.toExpression())
|
||||
element.replace(ifExpression)
|
||||
}
|
||||
|
||||
private fun combineWhenConditions(conditions: Array<JetWhenCondition>, subject: JetExpression?): String {
|
||||
|
||||
Reference in New Issue
Block a user