Remove braces: add parentheses to if-else inside expression

So #KT-18308 Fixed
This commit is contained in:
Toshiaki Kameyama
2017-11-30 03:16:50 +03:00
committed by Mikhail Glukhikh
parent 19e38bbc72
commit 44c15d8311
8 changed files with 79 additions and 1 deletions
@@ -20,7 +20,9 @@ import com.intellij.openapi.editor.Editor
import com.intellij.psi.PsiComment
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiWhiteSpace
import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
class RemoveBracesIntention : SelfTargetingIntention<KtElement>(KtElement::class.java, "Remove braces") {
@@ -61,8 +63,16 @@ class RemoveBracesIntention : SelfTargetingIntention<KtElement>(KtElement::class
val newElement = block.replace(statement.copy())
val factory = KtPsiFactory(block)
if (construct is KtDoWhileExpression) {
newElement.parent!!.addAfter(KtPsiFactory(block).createNewLine(), newElement)
newElement.parent!!.addAfter(factory.createNewLine(), newElement)
}
if (construct is KtIfExpression &&
container.node.elementType == KtNodeTypes.ELSE &&
construct.getStrictParentOfType<KtDotQualifiedExpression>() != null) {
construct.replace(factory.createExpressionByPattern("($0)", construct))
}
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
fun test(b: Boolean) {
val list1 = mutableListOf(1)
val list2 = mutableListOf(2)
if (b) list1 else <caret>{list2}.add(3)
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
fun test(b: Boolean) {
val list1 = mutableListOf(1)
val list2 = mutableListOf(2)
(if (b) list1 else list2).add(3)
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
fun test(b: Boolean) {
val list1 = mutableListOf(1)
val list2 = mutableListOf(2)
if (b) <caret>{list1} else {list2}.add(3)
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
fun test(b: Boolean) {
val list1 = mutableListOf(1)
val list2 = mutableListOf(2)
if (b) list1 else {list2}.add(3)
}
@@ -0,0 +1,12 @@
// WITH_RUNTIME
fun test(b: Boolean) {
val list1 = mutableListOf(1)
val list2 = mutableListOf(2)
when {
b -> list1
else -> <caret>{
list2
}
}.add(3)
}
@@ -0,0 +1,10 @@
// WITH_RUNTIME
fun test(b: Boolean) {
val list1 = mutableListOf(1)
val list2 = mutableListOf(2)
when {
b -> list1
else -> list2
}.add(3)
}
@@ -12123,6 +12123,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("elseInDotQualifiedExpression.kt")
public void testElseInDotQualifiedExpression() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBraces/elseInDotQualifiedExpression.kt");
doTest(fileName);
}
@TestMetadata("for.kt")
public void testFor() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBraces/for.kt");
@@ -12141,6 +12147,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("ifInDotQualifiedExpression.kt")
public void testIfInDotQualifiedExpression() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBraces/ifInDotQualifiedExpression.kt");
doTest(fileName);
}
@TestMetadata("ifInsideIf.kt")
public void testIfInsideIf() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBraces/ifInsideIf.kt");
@@ -12177,6 +12189,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("whenInDotQualifiedExpression.kt")
public void testWhenInDotQualifiedExpression() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBraces/whenInDotQualifiedExpression.kt");
doTest(fileName);
}
@TestMetadata("whenLambda.kt")
public void testWhenLambda() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBraces/whenLambda.kt");