Fix intention for when: insert '||' instead of ','
This commit is contained in:
+37
-23
@@ -83,6 +83,11 @@ private fun KtExpression?.getWhenConditionSubjectCandidate(): KtExpression? {
|
|||||||
when (op) {
|
when (op) {
|
||||||
KtTokens.IN_KEYWORD, KtTokens.NOT_IN -> lhs
|
KtTokens.IN_KEYWORD, KtTokens.NOT_IN -> lhs
|
||||||
KtTokens.EQEQ -> lhs as? KtNameReferenceExpression ?: getRight()
|
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
|
else -> null
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,29 +114,7 @@ public fun KtWhenExpression.introduceSubject(): KtWhenExpression {
|
|||||||
if (i > 0) appendFixedText(",")
|
if (i > 0) appendFixedText(",")
|
||||||
|
|
||||||
val conditionExpression = (condition as KtWhenConditionWithExpression).getExpression()
|
val conditionExpression = (condition as KtWhenConditionWithExpression).getExpression()
|
||||||
when (conditionExpression) {
|
appendConditionWithSubjectRemoved(conditionExpression, subject)
|
||||||
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()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
appendFixedText("->")
|
appendFixedText("->")
|
||||||
@@ -146,6 +129,37 @@ public fun KtWhenExpression.introduceSubject(): KtWhenExpression {
|
|||||||
return replaced(whenExpression)
|
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? {
|
fun KtPsiFactory.combineWhenConditions(conditions: Array<KtWhenCondition>, subject: KtExpression?): KtExpression? {
|
||||||
when (conditions.size) {
|
when (conditions.size) {
|
||||||
0 -> return null
|
0 -> return null
|
||||||
|
|||||||
+1
-1
@@ -46,7 +46,7 @@ public class EliminateWhenSubjectIntention : SelfTargetingIntention<KtWhenExpres
|
|||||||
appendFixedText("else")
|
appendFixedText("else")
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
appendExpressions(entry.conditions.map { it.toExpression(subject) })
|
appendExpressions(entry.conditions.map { it.toExpression(subject) }, separator = "||")
|
||||||
}
|
}
|
||||||
appendFixedText("->")
|
appendFixedText("->")
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -43,7 +43,7 @@ public class IfToWhenIntention : SelfTargetingRangeIntention<KtIfExpression>(jav
|
|||||||
orBranches.addOrBranches(condition)
|
orBranches.addOrBranches(condition)
|
||||||
}
|
}
|
||||||
|
|
||||||
appendExpressions(orBranches)
|
appendExpressions(orBranches, separator = "||")
|
||||||
|
|
||||||
appendFixedText("->")
|
appendFixedText("->")
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
fun test(n: Int): String {
|
fun test(n: Int): String {
|
||||||
return <caret>when {
|
return <caret>when {
|
||||||
n < 0, n > 1000 -> "unknown"
|
n < 0 || n > 1000 -> "unknown"
|
||||||
n <= 10 -> "small"
|
n <= 10 -> "small"
|
||||||
n <= 100 -> "average"
|
n <= 100 -> "average"
|
||||||
else -> "big"
|
else -> "big"
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
// ERROR: Expected condition of type kotlin.Boolean
|
// ERROR: Expected condition of type kotlin.Boolean
|
||||||
// ERROR: Expected condition of type kotlin.Boolean
|
// ERROR: Expected condition of type kotlin.Boolean
|
||||||
|
// SKIP_ERRORS_AFTER
|
||||||
|
|
||||||
fun test(n: Int): String {
|
fun test(n: Int): String {
|
||||||
return <caret>when {
|
return <caret>when {
|
||||||
|
|||||||
+1
@@ -1,5 +1,6 @@
|
|||||||
// ERROR: Expected condition of type kotlin.Boolean
|
// ERROR: Expected condition of type kotlin.Boolean
|
||||||
// ERROR: Expected condition of type kotlin.Boolean
|
// ERROR: Expected condition of type kotlin.Boolean
|
||||||
|
// SKIP_ERRORS_AFTER
|
||||||
|
|
||||||
fun test(n: Int): String {
|
fun test(n: Int): String {
|
||||||
return if (_ is String) "String"
|
return if (_ is String) "String"
|
||||||
|
|||||||
+4
-4
@@ -1,8 +1,8 @@
|
|||||||
fun test(n: Int): String {
|
fun test(n: Int): String {
|
||||||
return <caret>when {
|
return when {
|
||||||
n in 0..5, n in 5..10 -> "small"
|
n in 0..5 || n in 5..10 -> "small"
|
||||||
n in 10..50, n in 50..100 -> "average"
|
n in 10..50 || n in 50..100 -> "average"
|
||||||
n in 100..500, n in 500..1000 -> "big"
|
n in 100..500 || n in 500..1000 -> "big"
|
||||||
else -> "unknown"
|
else -> "unknown"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user