Invert if condition intention: don't remove line breaks

#KT-30900 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-04-12 17:06:11 +09:00
committed by Dmitry Gridin
parent a66aebdc2c
commit 075620daad
12 changed files with 118 additions and 11 deletions
@@ -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<KtIfExpression>(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<KtIfExpression>(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<KtIfExpression>(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 }
}
}
@@ -0,0 +1,6 @@
fun foo(b: Boolean) {
<caret>if (b) bar(1) // comment1
else bar(2) // comment2
}
fun bar(i: Int) {}
@@ -0,0 +1,6 @@
fun foo(b: Boolean) {
if (!b) bar(2) // comment2
else bar(1) // comment1
}
fun bar(i: Int) {}
@@ -0,0 +1,7 @@
fun foo(b: Boolean) {
<caret>if (b)
bar(1) // comment1
else bar(2) // comment2
}
fun bar(i: Int) {}
@@ -0,0 +1,7 @@
fun foo(b: Boolean) {
if (!b) bar(2) // comment2
else
bar(1) // comment1
}
fun bar(i: Int) {}
@@ -0,0 +1,7 @@
fun foo(b: Boolean) {
<caret>if (b) bar(1) // comment1
else
bar(2) // comment2
}
fun bar(i: Int) {}
@@ -0,0 +1,7 @@
fun foo(b: Boolean) {
if (!b)
bar(2) // comment2
else bar(1) // comment1
}
fun bar(i: Int) {}
@@ -0,0 +1,8 @@
fun foo() {
<caret>if (true) bar(1)
else {
bar(2)
}
}
fun bar(i: Int) {}
@@ -0,0 +1,7 @@
fun foo() {
if (false) {
bar(2)
} else bar(1)
}
fun bar(i: Int) {}
@@ -0,0 +1,7 @@
fun foo() {
<caret>if (true) {
bar(1)
} else bar(2)
}
fun bar(i: Int) {}
@@ -0,0 +1,8 @@
fun foo() {
if (false) bar(2)
else {
bar(1)
}
}
fun bar(i: Int) {}
@@ -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");