Merge else-if intention (KT-34218)
#KT-34218 Fixed
This commit is contained in:
committed by
Nikolay Krasko
parent
383239aff6
commit
613fbf7a5d
@@ -1479,6 +1479,11 @@
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.kotlin.idea.intentions.MergeElseIfIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.kotlin.idea.intentions.AddMissingDestructuringIntention</className>
|
||||
<category>Kotlin</category>
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
if (a) {
|
||||
println("a")
|
||||
} else if (b) {
|
||||
println("not a and b")
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
if (a) {
|
||||
println("a")
|
||||
} else {
|
||||
if (b) {
|
||||
println("not a and b")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention merges else and nested <b>if</b> statement without <b>else</b> branches into a single <b>else if</b>.
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.intentions
|
||||
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
|
||||
|
||||
class MergeElseIfIntention : SelfTargetingIntention<KtIfExpression>(KtIfExpression::class.java, "Merge 'else if'") {
|
||||
override fun isApplicableTo(element: KtIfExpression, caretOffset: Int): Boolean {
|
||||
val elseBody = element.`else` ?: return false
|
||||
val nestedIf = elseBody.nestedIf() ?: return false
|
||||
return nestedIf.`else` == null
|
||||
}
|
||||
|
||||
override fun applyTo(element: KtIfExpression, editor: Editor?) {
|
||||
val nestedIf = element.`else`?.nestedIf() ?: return
|
||||
val condition = nestedIf.condition ?: return
|
||||
val nestedBody = nestedIf.then ?: return
|
||||
|
||||
val factory = KtPsiFactory(element)
|
||||
element.`else`?.replace(
|
||||
factory.createExpressionByPattern("if ($0) $1", condition, nestedBody)
|
||||
)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private fun KtExpression.nestedIf() =
|
||||
if (this is KtBlockExpression) {
|
||||
this.statements.singleOrNull() as? KtIfExpression
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.intentions.MergeElseIfIntention
|
||||
@@ -0,0 +1,11 @@
|
||||
fun foo() {
|
||||
// comment 1
|
||||
if (true) {
|
||||
|
||||
} <caret>else /* comment 2 */ {
|
||||
if (true) {
|
||||
// comment 4
|
||||
foo()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fun foo() {
|
||||
// comment 1
|
||||
if (true) {
|
||||
|
||||
} <caret>else /* comment 2 */ if (true) {
|
||||
// comment 4
|
||||
foo()
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
fun foo() {
|
||||
if (true) {
|
||||
|
||||
} <caret>else {
|
||||
if (false) {
|
||||
foo()
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun foo() {
|
||||
if (true) {
|
||||
|
||||
} <caret>else {
|
||||
if (false) foo()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
if (true) {
|
||||
|
||||
} <caret>else if (false) foo()
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
fun foo() {
|
||||
if (true) {
|
||||
|
||||
} <caret>else {
|
||||
if (false) {
|
||||
foo()
|
||||
}
|
||||
val a = 5
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fun foo() {
|
||||
if (true) {
|
||||
|
||||
} <caret>else {
|
||||
if (false) {
|
||||
foo()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun foo() {
|
||||
if (true) {
|
||||
|
||||
} <caret>else if (false) {
|
||||
foo()
|
||||
}
|
||||
}
|
||||
@@ -11722,6 +11722,44 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/mergeElseIf")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class MergeElseIf extends AbstractIntentionTest {
|
||||
private void runTest(String testDataFilePath) throws Exception {
|
||||
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInMergeElseIf() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/mergeElseIf"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("comments.kt")
|
||||
public void testComments() throws Exception {
|
||||
runTest("idea/testData/intentions/mergeElseIf/comments.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("else.kt")
|
||||
public void testElse() throws Exception {
|
||||
runTest("idea/testData/intentions/mergeElseIf/else.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("expression.kt")
|
||||
public void testExpression() throws Exception {
|
||||
runTest("idea/testData/intentions/mergeElseIf/expression.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("ifNotSingle.kt")
|
||||
public void testIfNotSingle() throws Exception {
|
||||
runTest("idea/testData/intentions/mergeElseIf/ifNotSingle.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simple.kt")
|
||||
public void testSimple() throws Exception {
|
||||
runTest("idea/testData/intentions/mergeElseIf/simple.kt");
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/mergeIfs")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user