Lift assignment out: do not suggest if other assignments available
So #KT-18709 Fixed
This commit is contained in:
+14
-4
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
|||||||
import org.jetbrains.kotlin.idea.intentions.branches
|
import org.jetbrains.kotlin.idea.intentions.branches
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.lastBlockStatementOrThis
|
import org.jetbrains.kotlin.psi.psiUtil.lastBlockStatementOrThis
|
||||||
import org.jetbrains.kotlin.types.typeUtil.isNothing
|
import org.jetbrains.kotlin.types.typeUtil.isNothing
|
||||||
@@ -51,7 +52,8 @@ object BranchedFoldingUtils {
|
|||||||
a1.left?.text == a2.left?.text && a1.operationToken == a2.operationToken
|
a1.left?.text == a2.left?.text && a1.operationToken == a2.operationToken
|
||||||
|
|
||||||
internal fun getFoldableAssignmentNumber(expression: KtExpression?): Int {
|
internal fun getFoldableAssignmentNumber(expression: KtExpression?): Int {
|
||||||
val assignments = mutableListOf<KtBinaryExpression>()
|
expression ?: return -1
|
||||||
|
val assignments = linkedSetOf<KtBinaryExpression>()
|
||||||
fun collectAssignmentsAndCheck(e: KtExpression?): Boolean = when (e) {
|
fun collectAssignmentsAndCheck(e: KtExpression?): Boolean = when (e) {
|
||||||
is KtWhenExpression -> {
|
is KtWhenExpression -> {
|
||||||
val entries = e.entries
|
val entries = e.entries
|
||||||
@@ -79,9 +81,17 @@ object BranchedFoldingUtils {
|
|||||||
else -> false
|
else -> false
|
||||||
}
|
}
|
||||||
if (!collectAssignmentsAndCheck(expression)) return -1
|
if (!collectAssignmentsAndCheck(expression)) return -1
|
||||||
if (assignments.isEmpty()) return 0
|
val firstAssignment = assignments.firstOrNull()
|
||||||
val firstAssignment = assignments.first()
|
if (firstAssignment != null && assignments.any { !BranchedFoldingUtils.checkAssignmentsMatch(it, firstAssignment) }) {
|
||||||
if (assignments.any { !BranchedFoldingUtils.checkAssignmentsMatch(it, firstAssignment) }) return -1
|
return -1
|
||||||
|
}
|
||||||
|
if (expression.anyDescendantOfType<KtBinaryExpression>(
|
||||||
|
predicate = {
|
||||||
|
it.operationToken in KtTokens.ALL_ASSIGNMENTS && it !in assignments
|
||||||
|
}
|
||||||
|
)) {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
return assignments.size
|
return assignments.size
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
// PROBLEM: none
|
||||||
|
|
||||||
|
fun foo(x: Boolean): String {
|
||||||
|
var s = ""
|
||||||
|
<caret>if (x) {
|
||||||
|
s += "a"
|
||||||
|
s += "b"
|
||||||
|
} else {
|
||||||
|
s += "c"
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -442,6 +442,12 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("multipleAssignments.kt")
|
||||||
|
public void testMultipleAssignments() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/liftOut/ifToAssignment/multipleAssignments.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("simpleIf.kt")
|
@TestMetadata("simpleIf.kt")
|
||||||
public void testSimpleIf() throws Exception {
|
public void testSimpleIf() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/liftOut/ifToAssignment/simpleIf.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/liftOut/ifToAssignment/simpleIf.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user