"Remove braces from 'if' statement": do not suggest when lambda is directly under 'if'
#KT-35994 Fixed
This commit is contained in:
committed by
Ilya Kirillov
parent
30366148bf
commit
a96b359a09
@@ -38,6 +38,7 @@ class RemoveBracesIntention : SelfTargetingIntention<KtElement>(KtElement::class
|
|||||||
override fun isApplicableTo(element: KtElement, caretOffset: Int): Boolean {
|
override fun isApplicableTo(element: KtElement, caretOffset: Int): Boolean {
|
||||||
val block = element.findChildBlock() ?: return false
|
val block = element.findChildBlock() ?: return false
|
||||||
val singleStatement = block.statements.singleOrNull() ?: return false
|
val singleStatement = block.statements.singleOrNull() ?: return false
|
||||||
|
if (singleStatement is KtLambdaExpression && singleStatement.functionLiteral.arrow == null) return false
|
||||||
when (val container = block.parent) {
|
when (val container = block.parent) {
|
||||||
is KtContainerNode -> {
|
is KtContainerNode -> {
|
||||||
if (singleStatement is KtIfExpression) {
|
if (singleStatement is KtIfExpression) {
|
||||||
@@ -52,12 +53,12 @@ class RemoveBracesIntention : SelfTargetingIntention<KtElement>(KtElement::class
|
|||||||
is KtWhenEntry -> {
|
is KtWhenEntry -> {
|
||||||
text = KotlinBundle.message("remove.braces.from.when.entry")
|
text = KotlinBundle.message("remove.braces.from.when.entry")
|
||||||
return singleStatement !is KtNamedDeclaration
|
return singleStatement !is KtNamedDeclaration
|
||||||
&& !(singleStatement is KtLambdaExpression && singleStatement.functionLiteral.arrow == null)
|
|
||||||
}
|
}
|
||||||
else -> return false
|
else -> return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
override fun applyTo(element: KtElement, editor: Editor?) {
|
override fun applyTo(element: KtElement, editor: Editor?) {
|
||||||
val block = element.findChildBlock() ?: return
|
val block = element.findChildBlock() ?: return
|
||||||
val factory = KtPsiFactory(element)
|
val factory = KtPsiFactory(element)
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
fun test(i: Int) {
|
||||||
|
val predicate: () -> Boolean =
|
||||||
|
if (i == 1) {
|
||||||
|
{ true }
|
||||||
|
} else {
|
||||||
|
<caret>{ false }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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 }
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
fun test(i: Int) {
|
||||||
|
val predicate: () -> Boolean =
|
||||||
|
if (i == 1) {
|
||||||
|
<caret>{ true }
|
||||||
|
} else {
|
||||||
|
{ false }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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");
|
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")
|
@TestMetadata("for.kt")
|
||||||
public void testFor() throws Exception {
|
public void testFor() throws Exception {
|
||||||
runTest("idea/testData/intentions/removeBraces/for.kt");
|
runTest("idea/testData/intentions/removeBraces/for.kt");
|
||||||
@@ -13079,6 +13089,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
runTest("idea/testData/intentions/removeBraces/ifInsideIf7.kt");
|
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")
|
@TestMetadata("ifWithComment.kt")
|
||||||
public void testIfWithComment() throws Exception {
|
public void testIfWithComment() throws Exception {
|
||||||
runTest("idea/testData/intentions/removeBraces/ifWithComment.kt");
|
runTest("idea/testData/intentions/removeBraces/ifWithComment.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user