diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/InvertIfConditionIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/InvertIfConditionIntention.kt index d7175966734..b8b11ff7750 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/InvertIfConditionIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/InvertIfConditionIntention.kt @@ -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(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? { diff --git a/idea/testData/intentions/invertIfCondition/branchingIfStatements.kt.after b/idea/testData/intentions/invertIfCondition/branchingIfStatements.kt.after index c08f0ff13a3..51d294ae383 100644 --- a/idea/testData/intentions/invertIfCondition/branchingIfStatements.kt.after +++ b/idea/testData/intentions/invertIfCondition/branchingIfStatements.kt.after @@ -6,5 +6,6 @@ fun main() { a + 1 else a + 2 - } else a + } else + a } \ No newline at end of file diff --git a/idea/testData/intentions/invertIfCondition/notBlock.kt b/idea/testData/intentions/invertIfCondition/notBlock.kt new file mode 100644 index 00000000000..84a414a6c81 --- /dev/null +++ b/idea/testData/intentions/invertIfCondition/notBlock.kt @@ -0,0 +1,6 @@ +fun foo(i: Int): Int { + return if (i > 0) + i + else + i + 1 +} \ No newline at end of file diff --git a/idea/testData/intentions/invertIfCondition/notBlock.kt.after b/idea/testData/intentions/invertIfCondition/notBlock.kt.after new file mode 100644 index 00000000000..c9a04fc0930 --- /dev/null +++ b/idea/testData/intentions/invertIfCondition/notBlock.kt.after @@ -0,0 +1,6 @@ +fun foo(i: Int): Int { + return if (i <= 0) + i + 1 + else + i +} \ No newline at end of file diff --git a/idea/testData/intentions/invertIfCondition/notBlock2.kt b/idea/testData/intentions/invertIfCondition/notBlock2.kt new file mode 100644 index 00000000000..8652ce06294 --- /dev/null +++ b/idea/testData/intentions/invertIfCondition/notBlock2.kt @@ -0,0 +1,6 @@ +fun foo(i: Int): Int { + return if (i > 0) { + i + } else + i + 1 +} \ No newline at end of file diff --git a/idea/testData/intentions/invertIfCondition/notBlock2.kt.after b/idea/testData/intentions/invertIfCondition/notBlock2.kt.after new file mode 100644 index 00000000000..794f69aa999 --- /dev/null +++ b/idea/testData/intentions/invertIfCondition/notBlock2.kt.after @@ -0,0 +1,7 @@ +fun foo(i: Int): Int { + return if (i <= 0) + i + 1 + else { + i + } +} \ No newline at end of file diff --git a/idea/testData/intentions/invertIfCondition/notBlock3.kt b/idea/testData/intentions/invertIfCondition/notBlock3.kt new file mode 100644 index 00000000000..8dd50fd9568 --- /dev/null +++ b/idea/testData/intentions/invertIfCondition/notBlock3.kt @@ -0,0 +1,8 @@ +fun foo(i: Int) { + if (i > 0) + bar() + else { + } +} + +fun bar() {} \ No newline at end of file diff --git a/idea/testData/intentions/invertIfCondition/notBlock3.kt.after b/idea/testData/intentions/invertIfCondition/notBlock3.kt.after new file mode 100644 index 00000000000..49fb8d9341e --- /dev/null +++ b/idea/testData/intentions/invertIfCondition/notBlock3.kt.after @@ -0,0 +1,7 @@ +fun foo(i: Int) { + if (i <= 0) { + } else + bar() +} + +fun bar() {} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 9a2de2e5997..269af36862b 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -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");