Invert if condition intention: don't remove line breaks
#KT-30900 Fixed
This commit is contained in:
committed by
Dmitry Gridin
parent
a66aebdc2c
commit
075620daad
@@ -17,7 +17,10 @@
|
|||||||
package org.jetbrains.kotlin.idea.intentions
|
package org.jetbrains.kotlin.idea.intentions
|
||||||
|
|
||||||
import com.intellij.openapi.editor.Editor
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import com.intellij.psi.PsiComment
|
||||||
import com.intellij.psi.PsiElement
|
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.caches.resolve.resolveToDescriptorIfAny
|
||||||
import org.jetbrains.kotlin.idea.core.moveCaret
|
import org.jetbrains.kotlin.idea.core.moveCaret
|
||||||
import org.jetbrains.kotlin.idea.core.replaced
|
import org.jetbrains.kotlin.idea.core.replaced
|
||||||
@@ -45,7 +48,10 @@ class InvertIfConditionIntention : SelfTargetingIntention<KtIfExpression>(KtIfEx
|
|||||||
else
|
else
|
||||||
PsiChildRange.singleElement(element)
|
PsiChildRange.singleElement(element)
|
||||||
val commentSaver = CommentSaver(commentSavingRange)
|
val commentSaver = CommentSaver(commentSavingRange)
|
||||||
|
if (rBrace != null) {
|
||||||
|
element.nextEolCommentOnSameLine()?.delete()
|
||||||
|
}
|
||||||
|
|
||||||
val newCondition = element.condition!!.negate()
|
val newCondition = element.condition!!.negate()
|
||||||
|
|
||||||
val newIf = handleSpecialCases(element, newCondition) ?: handleStandardCase(element, newCondition)
|
val newIf = handleSpecialCases(element, newCondition) ?: handleStandardCase(element, newCondition)
|
||||||
@@ -90,18 +96,17 @@ class InvertIfConditionIntention : SelfTargetingIntention<KtIfExpression>(KtIfEx
|
|||||||
else
|
else
|
||||||
thenBranch
|
thenBranch
|
||||||
|
|
||||||
var thenSpace = " "
|
val conditionLineNumber = ifExpression.condition?.getLineNumber(false)
|
||||||
var elseSpace = " "
|
val thenBranchLineNumber = thenBranch.getLineNumber(false)
|
||||||
if (ifExpression.condition?.getLineNumber(false) != thenBranch.getLineNumber()
|
val elseKeywordLineNumber = ifExpression.elseKeyword?.getLineNumber()
|
||||||
|| ifExpression.elseKeyword?.getLineNumber() != elseBranch.getLineNumber()
|
val afterCondition = if (newThen !is KtBlockExpression && elseKeywordLineNumber != elseBranch.getLineNumber(false)) "\n" else ""
|
||||||
) {
|
val beforeElse = if (newThen !is KtBlockExpression && conditionLineNumber != elseKeywordLineNumber) "\n" else " "
|
||||||
if (newThen !is KtBlockExpression) thenSpace = "\n"
|
val afterElse = if (newElse !is KtBlockExpression && conditionLineNumber != thenBranchLineNumber) "\n" else " "
|
||||||
if (newElse !is KtBlockExpression) elseSpace = "\n"
|
|
||||||
}
|
|
||||||
val newIf = if (newElse == null) {
|
val newIf = if (newElse == null) {
|
||||||
psiFactory.createExpressionByPattern("if ($0)$thenSpace$1", newCondition, newThen)
|
psiFactory.createExpressionByPattern("if ($0)$afterCondition$1", newCondition, newThen)
|
||||||
} else {
|
} 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
|
} as KtIfExpression
|
||||||
|
|
||||||
return ifExpression.replaced(newIf)
|
return ifExpression.replaced(newIf)
|
||||||
@@ -249,4 +254,11 @@ class InvertIfConditionIntention : SelfTargetingIntention<KtIfExpression>(KtIfEx
|
|||||||
private fun parentBlockRBrace(element: KtIfExpression): PsiElement? {
|
private fun parentBlockRBrace(element: KtIfExpression): PsiElement? {
|
||||||
return (element.parent as? KtBlockExpression)?.rBrace
|
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");
|
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")
|
@TestMetadata("notIn.kt")
|
||||||
public void testNotIn() throws Exception {
|
public void testNotIn() throws Exception {
|
||||||
runTest("idea/testData/intentions/invertIfCondition/notIn.kt");
|
runTest("idea/testData/intentions/invertIfCondition/notIn.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user