No use of CallChainBuilder anymore
This commit is contained in:
@@ -374,44 +374,6 @@ public class JetPsiFactory(private val project: Project) {
|
||||
return createClass("class A { constructor()$colonOrEmpty$text {}").getSecondaryConstructors().first().getDelegationCall()
|
||||
}
|
||||
|
||||
public inner class IfChainBuilder() {
|
||||
private val sb = StringBuilder()
|
||||
private var first = true
|
||||
private var frozen = false
|
||||
|
||||
public fun ifBranch(conditionText: String, expressionText: String): IfChainBuilder {
|
||||
if (first) {
|
||||
first = false
|
||||
}
|
||||
else {
|
||||
sb.append("else ")
|
||||
}
|
||||
|
||||
sb.append("if (").append(conditionText).append(") ").append(expressionText).append("\n")
|
||||
return this
|
||||
}
|
||||
|
||||
public fun ifBranch(condition: JetExpression, expression: JetExpression): IfChainBuilder {
|
||||
return ifBranch(condition.getText()!!, expression.getText()!!)
|
||||
}
|
||||
|
||||
public fun elseBranch(expressionText: String): IfChainBuilder {
|
||||
sb.append("else ").append(expressionText)
|
||||
return this
|
||||
}
|
||||
|
||||
public fun elseBranch(expression: JetExpression?): IfChainBuilder {
|
||||
return elseBranch(JetPsiUtil.getText(expression))
|
||||
}
|
||||
|
||||
public fun toExpression(): JetIfExpression {
|
||||
if (!frozen) {
|
||||
frozen = true
|
||||
}
|
||||
return createExpression(sb.toString()) as JetIfExpression
|
||||
}
|
||||
}
|
||||
|
||||
public class CallableBuilder(private val target: Target) {
|
||||
public enum class Target {
|
||||
FUNCTION
|
||||
|
||||
+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