Invert if condition intention: do not remove line breaks #KT-11740 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
dce5cb3ce9
commit
fee74273aa
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||
import org.jetbrains.kotlin.idea.core.moveCaret
|
||||
import org.jetbrains.kotlin.idea.core.replaced
|
||||
import org.jetbrains.kotlin.idea.core.unblockDocument
|
||||
import org.jetbrains.kotlin.idea.refactoring.getLineNumber
|
||||
import org.jetbrains.kotlin.idea.util.CommentSaver
|
||||
import org.jetbrains.kotlin.idea.util.psi.patternMatching.matches
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
@@ -89,7 +90,21 @@ class InvertIfConditionIntention : SelfTargetingIntention<KtIfExpression>(KtIfEx
|
||||
else
|
||||
thenBranch
|
||||
|
||||
return ifExpression.replaced(psiFactory.createIf(newCondition, newThen, newElse))
|
||||
var thenSpace = " "
|
||||
var elseSpace = " "
|
||||
if (ifExpression.condition?.getLineNumber(false) != thenBranch.getLineNumber()
|
||||
|| ifExpression.elseKeyword?.getLineNumber() != elseBranch.getLineNumber()
|
||||
) {
|
||||
if (newThen !is KtBlockExpression) thenSpace = "\n"
|
||||
if (newElse !is KtBlockExpression) elseSpace = "\n"
|
||||
}
|
||||
val newIf = if (newElse == null) {
|
||||
psiFactory.createExpressionByPattern("if ($0)$thenSpace$1", newCondition, newThen)
|
||||
} else {
|
||||
psiFactory.createExpressionByPattern("if ($0)$thenSpace$1${thenSpace}else$elseSpace$2", newCondition, newThen, newElse)
|
||||
} as KtIfExpression
|
||||
|
||||
return ifExpression.replaced(newIf)
|
||||
}
|
||||
|
||||
private fun handleSpecialCases(ifExpression: KtIfExpression, newCondition: KtExpression): KtIfExpression? {
|
||||
|
||||
@@ -6,5 +6,6 @@ fun main() {
|
||||
a + 1
|
||||
else
|
||||
a + 2
|
||||
} else a
|
||||
} else
|
||||
a
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
fun foo(i: Int): Int {
|
||||
return <caret>if (i > 0)
|
||||
i
|
||||
else
|
||||
i + 1
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
fun foo(i: Int): Int {
|
||||
return if (i <= 0)
|
||||
i + 1
|
||||
else
|
||||
i
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
fun foo(i: Int): Int {
|
||||
return <caret>if (i > 0) {
|
||||
i
|
||||
} else
|
||||
i + 1
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun foo(i: Int): Int {
|
||||
return if (i <= 0)
|
||||
i + 1
|
||||
else {
|
||||
i
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fun foo(i: Int) {
|
||||
<caret>if (i > 0)
|
||||
bar()
|
||||
else {
|
||||
}
|
||||
}
|
||||
|
||||
fun bar() {}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun foo(i: Int) {
|
||||
if (i <= 0) {
|
||||
} else
|
||||
bar()
|
||||
}
|
||||
|
||||
fun bar() {}
|
||||
@@ -9687,6 +9687,24 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("notBlock.kt")
|
||||
public void testNotBlock() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/invertIfCondition/notBlock.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("notBlock2.kt")
|
||||
public void testNotBlock2() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/invertIfCondition/notBlock2.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("notBlock3.kt")
|
||||
public void testNotBlock3() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/invertIfCondition/notBlock3.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("notIn.kt")
|
||||
public void testNotIn() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/invertIfCondition/notIn.kt");
|
||||
|
||||
Reference in New Issue
Block a user