KT-4868 Bug Fix for Swap Binary Expression Intention
This commit is contained in:
committed by
Andrey Breslav
parent
7e66d67b95
commit
acf1dc1912
@@ -21,6 +21,7 @@ import org.jetbrains.jet.lang.psi.JetBinaryExpression
|
||||
import org.jetbrains.jet.lang.psi.JetPsiFactory
|
||||
import org.jetbrains.jet.lang.psi.JetExpression
|
||||
import org.jetbrains.jet.plugin.intentions.SwapBinaryExpression.Position.*
|
||||
import org.jetbrains.jet.plugin.util.JetPsiPrecedences
|
||||
|
||||
public class SwapBinaryExpression : JetSelfTargetingIntention<JetBinaryExpression>(
|
||||
"swap.binary.expression", javaClass()
|
||||
@@ -35,16 +36,24 @@ public class SwapBinaryExpression : JetSelfTargetingIntention<JetBinaryExpressio
|
||||
fun getInnermostOperand(element: JetBinaryExpression, position: Position): JetExpression? {
|
||||
val left = element.getLeft()
|
||||
val right = element.getRight()
|
||||
|
||||
if (left == null || right == null) {
|
||||
return null
|
||||
}
|
||||
|
||||
val parentPrecedence = JetPsiPrecedences.getPrecedence(element)
|
||||
val leftPrecedence = JetPsiPrecedences.getPrecedence(left)
|
||||
val rightPrecedence = JetPsiPrecedences.getPrecedence(right)
|
||||
return when (position) {
|
||||
FIRST_LEFT -> if (left is JetBinaryExpression) getInnermostOperand(left, NEXT_RIGHT) else left as JetExpression
|
||||
FIRST_RIGHT -> if (right is JetBinaryExpression) getInnermostOperand(right, NEXT_LEFT) else right as JetExpression
|
||||
NEXT_LEFT -> if (left is JetBinaryExpression) getInnermostOperand(left, NEXT_LEFT) else left as JetExpression
|
||||
NEXT_RIGHT -> if (right is JetBinaryExpression) getInnermostOperand(right, NEXT_RIGHT) else right as JetExpression
|
||||
FIRST_LEFT -> if (leftPrecedence < parentPrecedence) left
|
||||
else if (left is JetBinaryExpression) getInnermostOperand(left, NEXT_RIGHT)
|
||||
else left as JetExpression
|
||||
FIRST_RIGHT -> if (rightPrecedence < parentPrecedence) right
|
||||
else if (right is JetBinaryExpression) getInnermostOperand(right, NEXT_LEFT)
|
||||
else right as JetExpression
|
||||
NEXT_LEFT -> if (leftPrecedence < parentPrecedence || left !is JetBinaryExpression) left
|
||||
else getInnermostOperand(left, NEXT_LEFT)
|
||||
NEXT_RIGHT -> if (rightPrecedence < parentPrecedence || right !is JetBinaryExpression) right
|
||||
else getInnermostOperand(right, NEXT_RIGHT)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun main() {
|
||||
val rabbit = 1 == 2<caret> || 3 == 5
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun main() {
|
||||
val rabbit = 3 == 5 || 1 == 2
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun main() {
|
||||
val rabbit = 4 + 2 <caret>* 5
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun main() {
|
||||
val rabbit = 4 + 5 * 2
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun main() {
|
||||
val rabbit = true || false && 1 + 2 <caret>== 3 || false
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun main() {
|
||||
val rabbit = true || false && 3 == 1 + 2 || false
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun main() {
|
||||
val rabbit = true || false && 3 == 1 + 2 ||<caret> false
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun main() {
|
||||
val rabbit = true || false || false && 3 == 1 + 2
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun main() {
|
||||
val rabbit = 4 + 2 * 5 <caret>+ 1
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun main() {
|
||||
val rabbit = 4 + 1 + 2 * 5
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun main() {
|
||||
val rabbit = 4 + 2 == 12 ||<caret> true && 5 + 5 == 10
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun main() {
|
||||
val rabbit = true && 5 + 5 == 10 || 4 + 2 == 12
|
||||
}
|
||||
@@ -3365,6 +3365,36 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT
|
||||
doTestSwapBinaryExpression("idea/testData/intentions/swapBinaryExpression/multipleOperands.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("multipleOperandsWithDifferentPrecedence.kt")
|
||||
public void testMultipleOperandsWithDifferentPrecedence() throws Exception {
|
||||
doTestSwapBinaryExpression("idea/testData/intentions/swapBinaryExpression/multipleOperandsWithDifferentPrecedence.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("multipleOperandsWithDifferentPrecedence2.kt")
|
||||
public void testMultipleOperandsWithDifferentPrecedence2() throws Exception {
|
||||
doTestSwapBinaryExpression("idea/testData/intentions/swapBinaryExpression/multipleOperandsWithDifferentPrecedence2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("multipleOperandsWithDifferentPrecedence3.kt")
|
||||
public void testMultipleOperandsWithDifferentPrecedence3() throws Exception {
|
||||
doTestSwapBinaryExpression("idea/testData/intentions/swapBinaryExpression/multipleOperandsWithDifferentPrecedence3.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("multipleOperandsWithDifferentPrecedence4.kt")
|
||||
public void testMultipleOperandsWithDifferentPrecedence4() throws Exception {
|
||||
doTestSwapBinaryExpression("idea/testData/intentions/swapBinaryExpression/multipleOperandsWithDifferentPrecedence4.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("multipleOperandsWithDifferentPrecedence5.kt")
|
||||
public void testMultipleOperandsWithDifferentPrecedence5() throws Exception {
|
||||
doTestSwapBinaryExpression("idea/testData/intentions/swapBinaryExpression/multipleOperandsWithDifferentPrecedence5.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("multipleOperandsWithDifferentPrecedence6.kt")
|
||||
public void testMultipleOperandsWithDifferentPrecedence6() throws Exception {
|
||||
doTestSwapBinaryExpression("idea/testData/intentions/swapBinaryExpression/multipleOperandsWithDifferentPrecedence6.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("multiply.kt")
|
||||
public void testMultiply() throws Exception {
|
||||
doTestSwapBinaryExpression("idea/testData/intentions/swapBinaryExpression/multiply.kt");
|
||||
|
||||
Reference in New Issue
Block a user