"Remove braces from 'if' statement": do not suggest when lambda is directly under 'if'

#KT-35994 Fixed
This commit is contained in:
Toshiaki Kameyama
2020-01-18 14:50:59 +09:00
committed by Ilya Kirillov
parent 30366148bf
commit a96b359a09
8 changed files with 68 additions and 1 deletions
@@ -38,6 +38,7 @@ class RemoveBracesIntention : SelfTargetingIntention<KtElement>(KtElement::class
override fun isApplicableTo(element: KtElement, caretOffset: Int): Boolean {
val block = element.findChildBlock() ?: return false
val singleStatement = block.statements.singleOrNull() ?: return false
if (singleStatement is KtLambdaExpression && singleStatement.functionLiteral.arrow == null) return false
when (val container = block.parent) {
is KtContainerNode -> {
if (singleStatement is KtIfExpression) {
@@ -52,12 +53,12 @@ class RemoveBracesIntention : SelfTargetingIntention<KtElement>(KtElement::class
is KtWhenEntry -> {
text = KotlinBundle.message("remove.braces.from.when.entry")
return singleStatement !is KtNamedDeclaration
&& !(singleStatement is KtLambdaExpression && singleStatement.functionLiteral.arrow == null)
}
else -> return false
}
}
override fun applyTo(element: KtElement, editor: Editor?) {
val block = element.findChildBlock() ?: return
val factory = KtPsiFactory(element)
+9
View File
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
fun test(i: Int) {
val predicate: () -> Boolean =
if (i == 1) {
{ true }
} else {
<caret>{ false }
}
}
+8
View File
@@ -0,0 +1,8 @@
fun test(i: Int) {
val predicate: () -> Boolean =
if (i == 1) {
{ true }
} else {
<caret>{ -> false }
}
}
@@ -0,0 +1,6 @@
fun test(i: Int) {
val predicate: () -> Boolean =
if (i == 1) {
{ true }
} else { -> false }
}
+9
View File
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
fun test(i: Int) {
val predicate: () -> Boolean =
if (i == 1) {
<caret>{ true }
} else {
{ false }
}
}
+8
View File
@@ -0,0 +1,8 @@
fun test(i: Int) {
val predicate: () -> Boolean =
if (i == 1) {
<caret>{ -> true }
} else {
{ false }
}
}
@@ -0,0 +1,6 @@
fun test(i: Int) {
val predicate: () -> Boolean =
if (i == 1) { -> true } else {
{ false }
}
}
@@ -13024,6 +13024,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/removeBraces/elseInPlusAssignExpression.kt");
}
@TestMetadata("elseLambda.kt")
public void testElseLambda() throws Exception {
runTest("idea/testData/intentions/removeBraces/elseLambda.kt");
}
@TestMetadata("elseLambda2.kt")
public void testElseLambda2() throws Exception {
runTest("idea/testData/intentions/removeBraces/elseLambda2.kt");
}
@TestMetadata("for.kt")
public void testFor() throws Exception {
runTest("idea/testData/intentions/removeBraces/for.kt");
@@ -13079,6 +13089,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/removeBraces/ifInsideIf7.kt");
}
@TestMetadata("ifLambda.kt")
public void testIfLambda() throws Exception {
runTest("idea/testData/intentions/removeBraces/ifLambda.kt");
}
@TestMetadata("ifLambda2.kt")
public void testIfLambda2() throws Exception {
runTest("idea/testData/intentions/removeBraces/ifLambda2.kt");
}
@TestMetadata("ifWithComment.kt")
public void testIfWithComment() throws Exception {
runTest("idea/testData/intentions/removeBraces/ifWithComment.kt");