Compare commits

...

4 Commits

Author SHA1 Message Date
Nikolay Krasko
b3b6d5fdde 193: Adjust test data for HierarchyTestGenerated tests in 193 2019-10-19 11:12:30 +03:00
Nikolay Krasko
916538f09c 193: Mute join lines tests in 193 (KT-34408) 2019-10-19 11:12:30 +03:00
Nikolay Krasko
0db94f7b14 Fix AbstractSmartStepIntoTest tests in 193 2019-10-19 11:12:29 +03:00
DmiitriiJarosh
8c4dd1f331 Merge else-if intention (KT-34218)
#KT-34218 Fixed
2019-10-19 11:12:29 +03:00
45 changed files with 279 additions and 2 deletions

View File

@@ -11,7 +11,6 @@ import org.jetbrains.kotlin.idea.debugger.stepping.KotlinSmartStepIntoHandler
import org.jetbrains.kotlin.idea.debugger.test.mock.MockSourcePosition
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
import org.jetbrains.kotlin.idea.test.PluginTestCaseBase
import org.jetbrains.kotlin.test.InTextDirectivesUtils
abstract class AbstractSmartStepIntoTest : KotlinLightCodeInsightFixtureTestCase() {
@@ -19,7 +18,7 @@ abstract class AbstractSmartStepIntoTest : KotlinLightCodeInsightFixtureTestCase
get() = myFixture
protected fun doTest(path: String) {
fixture.configureByFile(path)
fixture.configureByFile(fileName())
val offset = fixture.caretOffset
val line = fixture.getDocument(fixture.file!!)!!.getLineNumber(offset)

View File

@@ -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>

View File

@@ -0,0 +1,5 @@
if (a) {
println("a")
} else if (b) {
println("not a and b")
}

View File

@@ -0,0 +1,7 @@
if (a) {
println("a")
} else {
if (b) {
println("not a and b")
}
}

View File

@@ -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>

View File

@@ -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
}
}
}

View File

@@ -0,0 +1,12 @@
<node text="KA ()" base="true">
<node text="JA"/>
<node text="JA.newKA() ()"/>
<node text="KClient ()"/>
<node text="KClient ()"/>
<node text="KClient.bar ()"/>
<node text="KClient.bar() ()"/>
<node text="KClient.bar.localFun() ()"/>
<node text="KClientObj ()"/>
<node text="main0.kt ()"/>
<node text="packageFun(String) ()"/>
</node>

View File

@@ -0,0 +1,12 @@
<node text="KA.foo(String) ()" base="true">
<node text="JA(2 usages)"/>
<node text="JA.foo()(2 usages) ()"/>
<node text="KClient(2 usages) ()"/>
<node text="KClient(2 usages) ()"/>
<node text="KClient.bar()(2 usages) ()"/>
<node text="KClient.bar(2 usages) ()"/>
<node text="KClient.bar.localFun()(2 usages) ()"/>
<node text="KClientObj(2 usages) ()"/>
<node text="main0.kt(2 usages) ()"/>
<node text="packageFun(String)(2 usages) ()"/>
</node>

View File

@@ -0,0 +1,12 @@
<node text="T.KA ()" base="true">
<node text="JA"/>
<node text="JA.newKA() ()"/>
<node text="KClient ()"/>
<node text="KClient ()"/>
<node text="KClient.bar ()"/>
<node text="KClient.bar() ()"/>
<node text="KClient.bar.localFun() ()"/>
<node text="KClientObj ()"/>
<node text="main0.kt ()"/>
<node text="packageFun(String) ()"/>
</node>

View File

@@ -0,0 +1,12 @@
<node text="T.KA ()" base="true">
<node text="JA"/>
<node text="JA.newKA() ()"/>
<node text="KClient ()"/>
<node text="KClient ()"/>
<node text="KClient.bar ()"/>
<node text="KClient.bar() ()"/>
<node text="KClient.bar.localFun() ()"/>
<node text="KClientObj ()"/>
<node text="main0.kt ()"/>
<node text="packageFun(String) ()"/>
</node>

View File

@@ -0,0 +1,11 @@
<node text="packageFun(String) ()" base="true">
<node text="JA"/>
<node text="JA.foo() ()"/>
<node text="KClient ()"/>
<node text="KClient ()"/>
<node text="KClient.bar ()"/>
<node text="KClient.bar() ()"/>
<node text="KClient.bar.localFun() ()"/>
<node text="KClientObj ()"/>
<node text="main0.kt ()"/>
</node>

View File

@@ -0,0 +1,11 @@
<node text="packageVal ()" base="true">
<node text="JA"/>
<node text="JA.getName()(2 usages) ()"/>
<node text="KClient ()"/>
<node text="KClient ()"/>
<node text="KClient.bar() ()"/>
<node text="KClient.bar(2 usages) ()"/>
<node text="KClient.bar.localFun() ()"/>
<node text="KClientObj(2 usages) ()"/>
<node text="packageFun(String) ()"/>
</node>

View File

@@ -0,0 +1,12 @@
<node text="KA.name ()" base="true">
<node text="JA(2 usages)"/>
<node text="JA.getName()(2 usages) ()"/>
<node text="KClient(2 usages) ()"/>
<node text="KClient(2 usages) ()"/>
<node text="KClient.bar()(2 usages) ()"/>
<node text="KClient.bar(4 usages) ()"/>
<node text="KClient.bar.localFun()(2 usages) ()"/>
<node text="KClientObj(4 usages) ()"/>
<node text="main0.kt(2 usages) ()"/>
<node text="packageFun(String)(2 usages) ()"/>
</node>

View File

@@ -0,0 +1,5 @@
<node text="J.foo() ()" base="true">
<node text="A(2 usages)"/>
<node text="Main1Kt"/>
<node text="Main1Kt.test(J) ()"/>
</node>

View File

@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.MergeElseIfIntention

View File

@@ -0,0 +1,11 @@
fun foo() {
// comment 1
if (true) {
} <caret>else /* comment 2 */ {
if (true) {
// comment 4
foo()
}
}
}

View File

@@ -0,0 +1,9 @@
fun foo() {
// comment 1
if (true) {
} <caret>else /* comment 2 */ if (true) {
// comment 4
foo()
}
}

View File

@@ -0,0 +1,13 @@
// IS_APPLICABLE: false
fun foo() {
if (true) {
} <caret>else {
if (false) {
foo()
} else {
}
}
}

View File

@@ -0,0 +1,7 @@
fun foo() {
if (true) {
} <caret>else {
if (false) foo()
}
}

View File

@@ -0,0 +1,5 @@
fun foo() {
if (true) {
} <caret>else if (false) foo()
}

View File

@@ -0,0 +1,12 @@
// IS_APPLICABLE: false
fun foo() {
if (true) {
} <caret>else {
if (false) {
foo()
}
val a = 5
}
}

View File

@@ -0,0 +1,9 @@
fun foo() {
if (true) {
} <caret>else {
if (false) {
foo()
}
}
}

View File

@@ -0,0 +1,7 @@
fun foo() {
if (true) {
} <caret>else if (false) {
foo()
}
}

View File

@@ -0,0 +1 @@
https://youtrack.jetbrains.com/issue/KT-34408

View File

@@ -0,0 +1 @@
https://youtrack.jetbrains.com/issue/KT-34408

View File

@@ -0,0 +1 @@
https://youtrack.jetbrains.com/issue/KT-34408

View File

@@ -0,0 +1 @@
https://youtrack.jetbrains.com/issue/KT-34408

View File

@@ -0,0 +1 @@
https://youtrack.jetbrains.com/issue/KT-34408

View File

@@ -0,0 +1 @@
https://youtrack.jetbrains.com/issue/KT-34408

View File

@@ -0,0 +1 @@
https://youtrack.jetbrains.com/issue/KT-34408

View File

@@ -0,0 +1 @@
https://youtrack.jetbrains.com/issue/KT-34408

View File

@@ -0,0 +1 @@
https://youtrack.jetbrains.com/issue/KT-34408

View File

@@ -0,0 +1 @@
https://youtrack.jetbrains.com/issue/KT-34408

View File

@@ -0,0 +1 @@
https://youtrack.jetbrains.com/issue/KT-34408

View File

@@ -0,0 +1 @@
https://youtrack.jetbrains.com/issue/KT-34408

View File

@@ -0,0 +1 @@
https://youtrack.jetbrains.com/issue/KT-34408

View File

@@ -0,0 +1 @@
https://youtrack.jetbrains.com/issue/KT-34408

View File

@@ -0,0 +1 @@
https://youtrack.jetbrains.com/issue/KT-34408

View File

@@ -0,0 +1 @@
https://youtrack.jetbrains.com/issue/KT-34408

View File

@@ -0,0 +1 @@
https://youtrack.jetbrains.com/issue/KT-34408

View File

@@ -0,0 +1 @@
https://youtrack.jetbrains.com/issue/KT-34408

View File

@@ -0,0 +1 @@
https://youtrack.jetbrains.com/issue/KT-34408

View File

@@ -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)