Fix intention for when: insert '||' instead of ','

This commit is contained in:
Natalia Ukhorskaya
2015-12-16 14:58:43 +03:00
parent 6a3ac66208
commit abd7ed5c70
7 changed files with 46 additions and 30 deletions
@@ -83,6 +83,11 @@ private fun KtExpression?.getWhenConditionSubjectCandidate(): KtExpression? {
when (op) {
KtTokens.IN_KEYWORD, KtTokens.NOT_IN -> lhs
KtTokens.EQEQ -> lhs as? KtNameReferenceExpression ?: getRight()
KtTokens.OROR -> {
val leftCandidate = lhs.getWhenConditionSubjectCandidate()
val rightCandidate = right.getWhenConditionSubjectCandidate()
if (leftCandidate.matches(rightCandidate)) leftCandidate else null
}
else -> null
}
@@ -109,29 +114,7 @@ public fun KtWhenExpression.introduceSubject(): KtWhenExpression {
if (i > 0) appendFixedText(",")
val conditionExpression = (condition as KtWhenConditionWithExpression).getExpression()
when (conditionExpression) {
is KtIsExpression -> {
if (conditionExpression.isNegated()) {
appendFixedText("!")
}
appendFixedText("is ")
appendNonFormattedText(conditionExpression.getTypeReference()?.getText() ?: "")
}
is KtBinaryExpression -> {
val lhs = conditionExpression.getLeft()
val rhs = conditionExpression.getRight()
val op = conditionExpression.getOperationToken()
when (op) {
KtTokens.IN_KEYWORD -> appendFixedText("in ").appendExpression(rhs)
KtTokens.NOT_IN -> appendFixedText("!in ").appendExpression(rhs)
KtTokens.EQEQ -> appendExpression(if (subject.matches(lhs)) rhs else lhs)
else -> throw IllegalStateException()
}
}
else -> throw IllegalStateException()
}
appendConditionWithSubjectRemoved(conditionExpression, subject)
}
}
appendFixedText("->")
@@ -146,6 +129,37 @@ public fun KtWhenExpression.introduceSubject(): KtWhenExpression {
return replaced(whenExpression)
}
private fun BuilderByPattern<KtExpression>.appendConditionWithSubjectRemoved(conditionExpression: KtExpression?, subject: KtExpression) {
when (conditionExpression) {
is KtIsExpression -> {
if (conditionExpression.isNegated()) {
appendFixedText("!")
}
appendFixedText("is ")
appendNonFormattedText(conditionExpression.getTypeReference()?.getText() ?: "")
}
is KtBinaryExpression -> {
val lhs = conditionExpression.getLeft()
val rhs = conditionExpression.getRight()
val op = conditionExpression.getOperationToken()
when (op) {
KtTokens.IN_KEYWORD -> appendFixedText("in ").appendExpression(rhs)
KtTokens.NOT_IN -> appendFixedText("!in ").appendExpression(rhs)
KtTokens.EQEQ -> appendExpression(if (subject.matches(lhs)) rhs else lhs)
KtTokens.OROR -> {
appendConditionWithSubjectRemoved(lhs, subject)
appendFixedText(", ")
appendConditionWithSubjectRemoved(rhs, subject)
}
else -> throw IllegalStateException()
}
}
else -> throw IllegalStateException()
}
}
fun KtPsiFactory.combineWhenConditions(conditions: Array<KtWhenCondition>, subject: KtExpression?): KtExpression? {
when (conditions.size) {
0 -> return null
@@ -46,7 +46,7 @@ public class EliminateWhenSubjectIntention : SelfTargetingIntention<KtWhenExpres
appendFixedText("else")
}
else {
appendExpressions(entry.conditions.map { it.toExpression(subject) })
appendExpressions(entry.conditions.map { it.toExpression(subject) }, separator = "||")
}
appendFixedText("->")
@@ -43,7 +43,7 @@ public class IfToWhenIntention : SelfTargetingRangeIntention<KtIfExpression>(jav
orBranches.addOrBranches(condition)
}
appendExpressions(orBranches)
appendExpressions(orBranches, separator = "||")
appendFixedText("->")
@@ -1,6 +1,6 @@
fun test(n: Int): String {
return <caret>when {
n < 0, n > 1000 -> "unknown"
n < 0 || n > 1000 -> "unknown"
n <= 10 -> "small"
n <= 100 -> "average"
else -> "big"
@@ -1,5 +1,6 @@
// ERROR: Expected condition of type kotlin.Boolean
// ERROR: Expected condition of type kotlin.Boolean
// SKIP_ERRORS_AFTER
fun test(n: Int): String {
return <caret>when {
@@ -1,5 +1,6 @@
// ERROR: Expected condition of type kotlin.Boolean
// ERROR: Expected condition of type kotlin.Boolean
// SKIP_ERRORS_AFTER
fun test(n: Int): String {
return if (_ is String) "String"
@@ -1,8 +1,8 @@
fun test(n: Int): String {
return <caret>when {
n in 0..5, n in 5..10 -> "small"
n in 10..50, n in 50..100 -> "average"
n in 100..500, n in 500..1000 -> "big"
return when {
n in 0..5 || n in 5..10 -> "small"
n in 10..50 || n in 50..100 -> "average"
n in 100..500 || n in 500..1000 -> "big"
else -> "unknown"
}
}