committed by
Dmitry Jemerov
parent
456037a30d
commit
956094e062
@@ -0,0 +1,3 @@
|
||||
if (a && b) {
|
||||
println("a and b")
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
if (a) {
|
||||
if (b) {
|
||||
println("a and b")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention merges two nested 'if's without 'else' branches into a single one.
|
||||
</body>
|
||||
</html>
|
||||
@@ -1502,6 +1502,11 @@
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.kotlin.idea.intentions.MergeIfsIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
|
||||
displayName="Object literal can be converted to lambda"
|
||||
groupName="Kotlin"
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 2010-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.intentions
|
||||
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
|
||||
class MergeIfsIntention : SelfTargetingIntention<KtIfExpression>(KtIfExpression::class.java, "Merge 'if's") {
|
||||
|
||||
override fun isApplicableTo(element: KtIfExpression, caretOffset: Int): Boolean {
|
||||
if (element.`else` != null) return false
|
||||
val then = element.then ?: return false
|
||||
|
||||
val nestedIf = then.nestedIf() ?: return false
|
||||
if (nestedIf.`else` != null) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun applyTo(element: KtIfExpression, editor: Editor?) {
|
||||
applyTo(element)
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun applyTo(element: KtIfExpression): Int {
|
||||
val nestedIf = element.then?.nestedIf() ?: return -1
|
||||
val condition = element.condition ?: return -1
|
||||
val secondCondition = nestedIf.condition ?: return -1
|
||||
val nestedBody = nestedIf.then ?: return -1
|
||||
|
||||
val factory = KtPsiFactory(element)
|
||||
|
||||
condition.replace(factory.createExpressionByPattern("$0 && $1", condition, secondCondition))
|
||||
val newBody = element.then!!.replace(nestedBody)
|
||||
|
||||
return newBody.textRange!!.startOffset
|
||||
}
|
||||
|
||||
private fun KtExpression.nestedIf() = when (this) {
|
||||
is KtBlockExpression -> this.statements.singleOrNull() as? KtIfExpression
|
||||
is KtIfExpression -> this
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-10
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
* Copyright 2010-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.idea.joinLines
|
||||
import com.intellij.codeInsight.editorActions.JoinRawLinesHandlerDelegate
|
||||
import com.intellij.openapi.editor.Document
|
||||
import com.intellij.psi.PsiFile
|
||||
import org.jetbrains.kotlin.idea.intentions.MergeIfsIntention
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
|
||||
@@ -45,15 +46,7 @@ class JoinBlockIntoSingleStatementHandler : JoinRawLinesHandlerDelegate {
|
||||
// if outer if has else-branch and inner does not have it, do not remove braces otherwise else-branch will belong to different if!
|
||||
if (pparent.`else` != null) return -1
|
||||
|
||||
val condition1 = pparent.condition
|
||||
val condition2 = statement.condition
|
||||
val body = statement.then
|
||||
if (condition1 != null && condition2 != null && body != null) {
|
||||
val newCondition = KtPsiFactory(pparent).createExpressionByPattern("$0 && $1", condition1, condition2)
|
||||
condition1.replace(newCondition)
|
||||
val newBody = block.replace(body)
|
||||
return newBody.textRange!!.startOffset
|
||||
}
|
||||
return MergeIfsIntention.applyTo(pparent)
|
||||
}
|
||||
|
||||
val newStatement = block.replace(statement)
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.intentions.MergeIfsIntention
|
||||
@@ -0,0 +1,9 @@
|
||||
fun foo() {
|
||||
// comment 1
|
||||
<caret>if (/* comment 2 */ true /* comment 3 */) {
|
||||
if (false) {
|
||||
// comment 4
|
||||
foo()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun foo() {
|
||||
// comment 1
|
||||
<caret>if (/* comment 2 */ true && false /* comment 3 */) {
|
||||
// comment 4
|
||||
foo()
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
fun foo() {
|
||||
<caret>if (true) {
|
||||
if (false) {
|
||||
foo()
|
||||
}
|
||||
} else {
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
fun foo() {
|
||||
<caret>if (true) {
|
||||
if (false) {
|
||||
foo()
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
<caret>if (true) if (false) {
|
||||
foo()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
<caret>if (true && false) {
|
||||
foo()
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
fun foo() {
|
||||
<caret>if (true) {
|
||||
if (false) {
|
||||
foo()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
<caret>if (true && false) {
|
||||
foo()
|
||||
}
|
||||
}
|
||||
@@ -10507,6 +10507,45 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/mergeIfs")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class MergeIfs extends AbstractIntentionTest {
|
||||
public void testAllFilesPresentInMergeIfs() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/mergeIfs"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("comments.kt")
|
||||
public void testComments() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/mergeIfs/comments.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("else1.kt")
|
||||
public void testElse1() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/mergeIfs/else1.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("else2.kt")
|
||||
public void testElse2() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/mergeIfs/else2.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("expression.kt")
|
||||
public void testExpression() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/mergeIfs/expression.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("simple.kt")
|
||||
public void testSimple() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/mergeIfs/simple.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/moveLambdaInsideParentheses")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user