No use of WhenBuilder in introduceSubject
This commit is contained in:
@@ -502,10 +502,6 @@ public class JetPsiFactory(private val project: Project) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun WhenBuilder(): WhenBuilder {
|
|
||||||
return WhenBuilder(null: String?)
|
|
||||||
}
|
|
||||||
|
|
||||||
public fun WhenBuilder(subject: JetExpression?): WhenBuilder {
|
public fun WhenBuilder(subject: JetExpression?): WhenBuilder {
|
||||||
return WhenBuilder(subject?.getText())
|
return WhenBuilder(subject?.getText())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -198,7 +198,7 @@ public class ExpressionBuilder {
|
|||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun appendExpression(expression: JetExpression): ExpressionBuilder {
|
public fun appendExpression(expression: JetExpression?): ExpressionBuilder {
|
||||||
patternBuilder.append("$" + arguments.size())
|
patternBuilder.append("$" + arguments.size())
|
||||||
arguments.add(expression)
|
arguments.add(expression)
|
||||||
return this
|
return this
|
||||||
|
|||||||
@@ -126,8 +126,8 @@ public fun <T: PsiElement> PsiElement.replaced(newElement: T): T = replace(newEl
|
|||||||
[suppress("UNCHECKED_CAST")]
|
[suppress("UNCHECKED_CAST")]
|
||||||
public fun <T: PsiElement> T.copied(): T = copy() as T
|
public fun <T: PsiElement> T.copied(): T = copy() as T
|
||||||
|
|
||||||
public fun JetElement.blockExpressionsOrSingle(): Stream<JetElement> =
|
public fun JetElement.blockExpressionsOrSingle(): Sequence<JetElement> =
|
||||||
if (this is JetBlockExpression) getStatements().stream() else listOf(this).stream()
|
if (this is JetBlockExpression) getStatements().asSequence() else sequenceOf(this)
|
||||||
|
|
||||||
public fun JetElement.outermostLastBlockElement(predicate: (JetElement) -> Boolean = { true }): JetElement? {
|
public fun JetElement.outermostLastBlockElement(predicate: (JetElement) -> Boolean = { true }): JetElement? {
|
||||||
return JetPsiUtil.getOutermostLastBlockElement(this) { e -> e != null && predicate(e) }
|
return JetPsiUtil.getOutermostLastBlockElement(this) { e -> e != null && predicate(e) }
|
||||||
|
|||||||
+48
-33
@@ -102,7 +102,7 @@ fun JetWhenExpression.getSubjectCandidate(): JetExpression? {
|
|||||||
var lastCandidate: JetExpression? = null
|
var lastCandidate: JetExpression? = null
|
||||||
for (entry in getEntries()) {
|
for (entry in getEntries()) {
|
||||||
val conditions = entry.getConditions()
|
val conditions = entry.getConditions()
|
||||||
if (!entry.isElse() && conditions.size == 0) return null
|
if (!entry.isElse() && conditions.isEmpty()) return null
|
||||||
|
|
||||||
for (condition in conditions) {
|
for (condition in conditions) {
|
||||||
if (condition !is JetWhenConditionWithExpression) return null
|
if (condition !is JetWhenConditionWithExpression) return null
|
||||||
@@ -152,54 +152,69 @@ public fun JetWhenExpression.flatten(): JetWhenExpression {
|
|||||||
public fun JetWhenExpression.introduceSubject(): JetWhenExpression {
|
public fun JetWhenExpression.introduceSubject(): JetWhenExpression {
|
||||||
val subject = getSubjectCandidate()!!
|
val subject = getSubjectCandidate()!!
|
||||||
|
|
||||||
val builder = JetPsiFactory(this).WhenBuilder(subject)
|
val whenExpression = JetPsiFactory(this).buildExpression {
|
||||||
for (entry in getEntries()) {
|
appendFixedText("when(").appendExpression(subject).appendFixedText("){\n")
|
||||||
val branchExpression = entry.getExpression()
|
|
||||||
if (entry.isElse()) {
|
|
||||||
builder.elseEntry(branchExpression)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
for (condition in entry.getConditions()) {
|
for (entry in getEntries()) {
|
||||||
assert(condition is JetWhenConditionWithExpression, TRANSFORM_WITHOUT_CHECK)
|
val branchExpression = entry.getExpression()
|
||||||
|
|
||||||
val conditionExpression = ((condition as JetWhenConditionWithExpression)).getExpression()
|
if (entry.isElse()) {
|
||||||
when (conditionExpression) {
|
appendFixedText("else")
|
||||||
is JetIsExpression -> {
|
}
|
||||||
builder.pattern(conditionExpression.getTypeReference(), conditionExpression.isNegated())
|
else {
|
||||||
}
|
for ((i, condition) in entry.getConditions().withIndex()) {
|
||||||
is JetBinaryExpression -> {
|
if (i > 0) appendFixedText(",")
|
||||||
val lhs = conditionExpression.getLeft()
|
assert(condition is JetWhenConditionWithExpression, TRANSFORM_WITHOUT_CHECK)
|
||||||
val rhs = conditionExpression.getRight()
|
|
||||||
val op = conditionExpression.getOperationToken()
|
val conditionExpression = (condition as JetWhenConditionWithExpression).getExpression()
|
||||||
when (op) {
|
when (conditionExpression) {
|
||||||
JetTokens.IN_KEYWORD -> builder.range(rhs, false)
|
is JetIsExpression -> {
|
||||||
JetTokens.NOT_IN -> builder.range(rhs, true)
|
if (conditionExpression.isNegated()) {
|
||||||
JetTokens.EQEQ -> builder.condition(if (subject.matches(lhs)) rhs else lhs)
|
appendFixedText("!")
|
||||||
else -> assert(false, TRANSFORM_WITHOUT_CHECK)
|
}
|
||||||
|
appendFixedText("is ")
|
||||||
|
appendNonFormattedText(conditionExpression.getTypeReference()?.getText() ?: "")
|
||||||
|
}
|
||||||
|
|
||||||
|
is JetBinaryExpression -> {
|
||||||
|
val lhs = conditionExpression.getLeft()
|
||||||
|
val rhs = conditionExpression.getRight()
|
||||||
|
val op = conditionExpression.getOperationToken()
|
||||||
|
when (op) {
|
||||||
|
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 -> error(TRANSFORM_WITHOUT_CHECK)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else -> assert(false, TRANSFORM_WITHOUT_CHECK)
|
|
||||||
}
|
}
|
||||||
|
appendFixedText("->")
|
||||||
|
|
||||||
|
appendExpression(branchExpression)
|
||||||
|
appendFixedText("\n")
|
||||||
}
|
}
|
||||||
builder.branchExpression(branchExpression)
|
|
||||||
}
|
|
||||||
|
|
||||||
return replaced(builder.toExpression())
|
appendFixedText("}")
|
||||||
|
} as JetWhenExpression
|
||||||
|
|
||||||
|
return replaced(whenExpression)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun JetWhenExpression.canTransformToIf(): Boolean = !getEntries().isEmpty()
|
public fun JetWhenExpression.canTransformToIf(): Boolean = !getEntries().isEmpty()
|
||||||
|
|
||||||
public fun JetWhenExpression.transformToIf() {
|
public fun JetWhenExpression.transformToIf() {
|
||||||
fun combineWhenConditions(conditions: Array<JetWhenCondition>, subject: JetExpression?): String {
|
fun combineWhenConditions(conditions: Array<JetWhenCondition>, subject: JetExpression?): String {
|
||||||
return when (conditions.size) {
|
return when (conditions.size()) {
|
||||||
0 -> ""
|
0 -> ""
|
||||||
1 -> conditions[0].toExpressionText(subject)
|
1 -> conditions[0].toExpressionText(subject)
|
||||||
else -> {
|
else -> {
|
||||||
conditions
|
conditions
|
||||||
.map { condition -> parenthesizeTextIfNeeded(condition.toExpressionText(subject)) }
|
.map { condition -> parenthesizeTextIfNeeded(condition.toExpressionText(subject)) }
|
||||||
.makeString(separator = " || ")
|
.joinToString(separator = " || ")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -227,8 +242,8 @@ public fun JetWhenExpression.canMergeWithNext(): Boolean {
|
|||||||
fun JetWhenEntry.declarationNames(): Set<String> =
|
fun JetWhenEntry.declarationNames(): Set<String> =
|
||||||
getExpression()?.blockExpressionsOrSingle()
|
getExpression()?.blockExpressionsOrSingle()
|
||||||
?.filter { it is JetNamedDeclaration }
|
?.filter { it is JetNamedDeclaration }
|
||||||
?.map { decl -> decl.getName() }
|
?.map { it.getName() }
|
||||||
?.filterNotNull()?.toSet() ?: Collections.emptySet<String>()
|
?.filterNotNull()?.toSet() ?: emptySet()
|
||||||
|
|
||||||
fun checkBodies(e1: JetWhenEntry, e2: JetWhenEntry): Boolean {
|
fun checkBodies(e1: JetWhenEntry, e2: JetWhenEntry): Boolean {
|
||||||
if (ContainerUtil.intersects(e1.declarationNames(), e2.declarationNames())) return false
|
if (ContainerUtil.intersects(e1.declarationNames(), e2.declarationNames())) return false
|
||||||
@@ -246,7 +261,7 @@ public fun JetWhenExpression.canMergeWithNext(): Boolean {
|
|||||||
|
|
||||||
val entries1 = getEntries()
|
val entries1 = getEntries()
|
||||||
val entries2 = sibling.getEntries()
|
val entries2 = sibling.getEntries()
|
||||||
return entries1.size == entries2.size && (entries1 zip entries2).all { pair ->
|
return entries1.size() == entries2.size() && (entries1 zip entries2).all { pair ->
|
||||||
checkConditions(pair.first, pair.second) && checkBodies(pair.first, pair.second)
|
checkConditions(pair.first, pair.second) && checkBodies(pair.first, pair.second)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-3
@@ -51,9 +51,7 @@ public class EliminateWhenSubjectIntention : JetSelfTargetingIntention<JetWhenEx
|
|||||||
}
|
}
|
||||||
appendFixedText("->")
|
appendFixedText("->")
|
||||||
|
|
||||||
if (branchExpression != null) {
|
appendExpression(branchExpression)
|
||||||
appendExpression(branchExpression)
|
|
||||||
}
|
|
||||||
appendFixedText("\n")
|
appendFixedText("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-3
@@ -49,9 +49,7 @@ public class IfToWhenIntention : JetSelfTargetingIntention<JetIfExpression>(java
|
|||||||
appendFixedText("->")
|
appendFixedText("->")
|
||||||
|
|
||||||
val thenBranch = ifExpression.getThen()
|
val thenBranch = ifExpression.getThen()
|
||||||
if (thenBranch != null) {
|
appendExpression(thenBranch)
|
||||||
appendExpression(thenBranch)
|
|
||||||
}
|
|
||||||
appendFixedText("\n")
|
appendFixedText("\n")
|
||||||
|
|
||||||
val elseBranch = ifExpression.getElse() ?: break
|
val elseBranch = ifExpression.getElse() ?: break
|
||||||
|
|||||||
Reference in New Issue
Block a user