From 075620daadf5ec8d6270cc3dd9f463b92024d8d8 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Fri, 12 Apr 2019 17:06:11 +0900 Subject: [PATCH] Invert if condition intention: don't remove line breaks #KT-30900 Fixed --- .../intentions/InvertIfConditionIntention.kt | 34 +++++++++++++------ .../intentions/invertIfCondition/notBlock4.kt | 6 ++++ .../invertIfCondition/notBlock4.kt.after | 6 ++++ .../intentions/invertIfCondition/notBlock5.kt | 7 ++++ .../invertIfCondition/notBlock5.kt.after | 7 ++++ .../intentions/invertIfCondition/notBlock6.kt | 7 ++++ .../invertIfCondition/notBlock6.kt.after | 7 ++++ .../intentions/invertIfCondition/notBlock7.kt | 8 +++++ .../invertIfCondition/notBlock7.kt.after | 7 ++++ .../intentions/invertIfCondition/notBlock8.kt | 7 ++++ .../invertIfCondition/notBlock8.kt.after | 8 +++++ .../intentions/IntentionTestGenerated.java | 25 ++++++++++++++ 12 files changed, 118 insertions(+), 11 deletions(-) create mode 100644 idea/testData/intentions/invertIfCondition/notBlock4.kt create mode 100644 idea/testData/intentions/invertIfCondition/notBlock4.kt.after create mode 100644 idea/testData/intentions/invertIfCondition/notBlock5.kt create mode 100644 idea/testData/intentions/invertIfCondition/notBlock5.kt.after create mode 100644 idea/testData/intentions/invertIfCondition/notBlock6.kt create mode 100644 idea/testData/intentions/invertIfCondition/notBlock6.kt.after create mode 100644 idea/testData/intentions/invertIfCondition/notBlock7.kt create mode 100644 idea/testData/intentions/invertIfCondition/notBlock7.kt.after create mode 100644 idea/testData/intentions/invertIfCondition/notBlock8.kt create mode 100644 idea/testData/intentions/invertIfCondition/notBlock8.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/InvertIfConditionIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/InvertIfConditionIntention.kt index b8b11ff7750..4a2580da239 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/InvertIfConditionIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/InvertIfConditionIntention.kt @@ -17,7 +17,10 @@ package org.jetbrains.kotlin.idea.intentions import com.intellij.openapi.editor.Editor +import com.intellij.psi.PsiComment import com.intellij.psi.PsiElement +import com.intellij.psi.PsiWhiteSpace +import com.intellij.psi.util.PsiTreeUtil import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny import org.jetbrains.kotlin.idea.core.moveCaret import org.jetbrains.kotlin.idea.core.replaced @@ -45,7 +48,10 @@ class InvertIfConditionIntention : SelfTargetingIntention(KtIfEx else PsiChildRange.singleElement(element) val commentSaver = CommentSaver(commentSavingRange) - + if (rBrace != null) { + element.nextEolCommentOnSameLine()?.delete() + } + val newCondition = element.condition!!.negate() val newIf = handleSpecialCases(element, newCondition) ?: handleStandardCase(element, newCondition) @@ -90,18 +96,17 @@ class InvertIfConditionIntention : SelfTargetingIntention(KtIfEx else thenBranch - 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 conditionLineNumber = ifExpression.condition?.getLineNumber(false) + val thenBranchLineNumber = thenBranch.getLineNumber(false) + val elseKeywordLineNumber = ifExpression.elseKeyword?.getLineNumber() + val afterCondition = if (newThen !is KtBlockExpression && elseKeywordLineNumber != elseBranch.getLineNumber(false)) "\n" else "" + val beforeElse = if (newThen !is KtBlockExpression && conditionLineNumber != elseKeywordLineNumber) "\n" else " " + val afterElse = if (newElse !is KtBlockExpression && conditionLineNumber != thenBranchLineNumber) "\n" else " " + val newIf = if (newElse == null) { - psiFactory.createExpressionByPattern("if ($0)$thenSpace$1", newCondition, newThen) + psiFactory.createExpressionByPattern("if ($0)$afterCondition$1", newCondition, newThen) } else { - psiFactory.createExpressionByPattern("if ($0)$thenSpace$1${thenSpace}else$elseSpace$2", newCondition, newThen, newElse) + psiFactory.createExpressionByPattern("if ($0)$afterCondition$1${beforeElse}else$afterElse$2", newCondition, newThen, newElse) } as KtIfExpression return ifExpression.replaced(newIf) @@ -249,4 +254,11 @@ class InvertIfConditionIntention : SelfTargetingIntention(KtIfEx private fun parentBlockRBrace(element: KtIfExpression): PsiElement? { return (element.parent as? KtBlockExpression)?.rBrace } + + private fun KtIfExpression.nextEolCommentOnSameLine(): PsiElement? { + val lastLineNumber = getLineNumber(false) + return siblings(withItself = false) + .takeWhile { it.getLineNumber() == lastLineNumber } + .firstOrNull { it is PsiComment && it.node.elementType == KtTokens.EOL_COMMENT } + } } diff --git a/idea/testData/intentions/invertIfCondition/notBlock4.kt b/idea/testData/intentions/invertIfCondition/notBlock4.kt new file mode 100644 index 00000000000..b686c0ffa75 --- /dev/null +++ b/idea/testData/intentions/invertIfCondition/notBlock4.kt @@ -0,0 +1,6 @@ +fun foo(b: Boolean) { + if (b) bar(1) // comment1 + else bar(2) // comment2 +} + +fun bar(i: Int) {} \ No newline at end of file diff --git a/idea/testData/intentions/invertIfCondition/notBlock4.kt.after b/idea/testData/intentions/invertIfCondition/notBlock4.kt.after new file mode 100644 index 00000000000..a8c4bd72c17 --- /dev/null +++ b/idea/testData/intentions/invertIfCondition/notBlock4.kt.after @@ -0,0 +1,6 @@ +fun foo(b: Boolean) { + if (!b) bar(2) // comment2 + else bar(1) // comment1 +} + +fun bar(i: Int) {} \ No newline at end of file diff --git a/idea/testData/intentions/invertIfCondition/notBlock5.kt b/idea/testData/intentions/invertIfCondition/notBlock5.kt new file mode 100644 index 00000000000..f3024a25e8d --- /dev/null +++ b/idea/testData/intentions/invertIfCondition/notBlock5.kt @@ -0,0 +1,7 @@ +fun foo(b: Boolean) { + if (b) + bar(1) // comment1 + else bar(2) // comment2 +} + +fun bar(i: Int) {} \ No newline at end of file diff --git a/idea/testData/intentions/invertIfCondition/notBlock5.kt.after b/idea/testData/intentions/invertIfCondition/notBlock5.kt.after new file mode 100644 index 00000000000..98c6588db7e --- /dev/null +++ b/idea/testData/intentions/invertIfCondition/notBlock5.kt.after @@ -0,0 +1,7 @@ +fun foo(b: Boolean) { + if (!b) bar(2) // comment2 + else + bar(1) // comment1 +} + +fun bar(i: Int) {} \ No newline at end of file diff --git a/idea/testData/intentions/invertIfCondition/notBlock6.kt b/idea/testData/intentions/invertIfCondition/notBlock6.kt new file mode 100644 index 00000000000..edd85bdd9b9 --- /dev/null +++ b/idea/testData/intentions/invertIfCondition/notBlock6.kt @@ -0,0 +1,7 @@ +fun foo(b: Boolean) { + if (b) bar(1) // comment1 + else + bar(2) // comment2 +} + +fun bar(i: Int) {} \ No newline at end of file diff --git a/idea/testData/intentions/invertIfCondition/notBlock6.kt.after b/idea/testData/intentions/invertIfCondition/notBlock6.kt.after new file mode 100644 index 00000000000..15cc371dd91 --- /dev/null +++ b/idea/testData/intentions/invertIfCondition/notBlock6.kt.after @@ -0,0 +1,7 @@ +fun foo(b: Boolean) { + if (!b) + bar(2) // comment2 + else bar(1) // comment1 +} + +fun bar(i: Int) {} \ No newline at end of file diff --git a/idea/testData/intentions/invertIfCondition/notBlock7.kt b/idea/testData/intentions/invertIfCondition/notBlock7.kt new file mode 100644 index 00000000000..5a73fef8e6f --- /dev/null +++ b/idea/testData/intentions/invertIfCondition/notBlock7.kt @@ -0,0 +1,8 @@ +fun foo() { + if (true) bar(1) + else { + bar(2) + } +} + +fun bar(i: Int) {} \ No newline at end of file diff --git a/idea/testData/intentions/invertIfCondition/notBlock7.kt.after b/idea/testData/intentions/invertIfCondition/notBlock7.kt.after new file mode 100644 index 00000000000..eab131071cc --- /dev/null +++ b/idea/testData/intentions/invertIfCondition/notBlock7.kt.after @@ -0,0 +1,7 @@ +fun foo() { + if (false) { + bar(2) + } else bar(1) +} + +fun bar(i: Int) {} \ No newline at end of file diff --git a/idea/testData/intentions/invertIfCondition/notBlock8.kt b/idea/testData/intentions/invertIfCondition/notBlock8.kt new file mode 100644 index 00000000000..c12252ac7c1 --- /dev/null +++ b/idea/testData/intentions/invertIfCondition/notBlock8.kt @@ -0,0 +1,7 @@ +fun foo() { + if (true) { + bar(1) + } else bar(2) +} + +fun bar(i: Int) {} \ No newline at end of file diff --git a/idea/testData/intentions/invertIfCondition/notBlock8.kt.after b/idea/testData/intentions/invertIfCondition/notBlock8.kt.after new file mode 100644 index 00000000000..77cef398b9e --- /dev/null +++ b/idea/testData/intentions/invertIfCondition/notBlock8.kt.after @@ -0,0 +1,8 @@ +fun foo() { + if (false) bar(2) + else { + bar(1) + } +} + +fun bar(i: Int) {} \ 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 4424f6169bd..b9aed83d542 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -10509,6 +10509,31 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/invertIfCondition/notBlock3.kt"); } + @TestMetadata("notBlock4.kt") + public void testNotBlock4() throws Exception { + runTest("idea/testData/intentions/invertIfCondition/notBlock4.kt"); + } + + @TestMetadata("notBlock5.kt") + public void testNotBlock5() throws Exception { + runTest("idea/testData/intentions/invertIfCondition/notBlock5.kt"); + } + + @TestMetadata("notBlock6.kt") + public void testNotBlock6() throws Exception { + runTest("idea/testData/intentions/invertIfCondition/notBlock6.kt"); + } + + @TestMetadata("notBlock7.kt") + public void testNotBlock7() throws Exception { + runTest("idea/testData/intentions/invertIfCondition/notBlock7.kt"); + } + + @TestMetadata("notBlock8.kt") + public void testNotBlock8() throws Exception { + runTest("idea/testData/intentions/invertIfCondition/notBlock8.kt"); + } + @TestMetadata("notIn.kt") public void testNotIn() throws Exception { runTest("idea/testData/intentions/invertIfCondition/notIn.kt");