Remove braces: add parentheses to if-else in more general case
Related to KT-18308
This commit is contained in:
@@ -22,7 +22,6 @@ 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") {
|
||||
|
||||
@@ -71,7 +70,8 @@ class RemoveBracesIntention : SelfTargetingIntention<KtElement>(KtElement::class
|
||||
|
||||
if (construct is KtIfExpression &&
|
||||
container.node.elementType == KtNodeTypes.ELSE &&
|
||||
construct.getStrictParentOfType<KtDotQualifiedExpression>() != null) {
|
||||
construct.parent is KtExpression &&
|
||||
construct.parent !is KtStatementExpression) {
|
||||
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} += 3
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// WITH_RUNTIME
|
||||
fun test(b: Boolean) {
|
||||
val list1 = mutableListOf(1)
|
||||
val list2 = mutableListOf(2)
|
||||
|
||||
(if (b) {list1} else list2) += 3
|
||||
}
|
||||
@@ -12129,6 +12129,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("elseInPlusAssignExpression.kt")
|
||||
public void testElseInPlusAssignExpression() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBraces/elseInPlusAssignExpression.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("for.kt")
|
||||
public void testFor() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBraces/for.kt");
|
||||
|
||||
Reference in New Issue
Block a user