Improve: add more cases for MoveVariableDeclarationIntoWhen
#KT-30499 Fixed
This commit is contained in:
+17
-1
@@ -60,10 +60,26 @@ private fun KtProperty.action(element: KtElement): Action = when (val elementUsa
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun KtWhenExpression.findDeclarationNear(): KtProperty? {
|
private fun KtWhenExpression.findDeclarationNear(): KtProperty? {
|
||||||
val previousProperty = previousStatement() as? KtProperty ?: return null
|
val previousProperty = previousStatement() as? KtProperty
|
||||||
|
?: previousPropertyFromParent()
|
||||||
|
?: return null
|
||||||
return previousProperty.takeIf { !it.isVar && it.hasInitializer() && it.nameIdentifier?.text == subjectExpression?.text }
|
return previousProperty.takeIf { !it.isVar && it.hasInitializer() && it.nameIdentifier?.text == subjectExpression?.text }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private tailrec fun KtExpression.previousPropertyFromParent(): KtProperty? {
|
||||||
|
val parentExpression = parent as? KtExpression ?: return null
|
||||||
|
if (this != when (parentExpression) {
|
||||||
|
is KtProperty -> parentExpression.initializer
|
||||||
|
is KtReturnExpression -> parentExpression.returnedExpression
|
||||||
|
is KtBinaryExpression -> parentExpression.left
|
||||||
|
is KtUnaryExpression -> parentExpression.baseExpression
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
) return null
|
||||||
|
|
||||||
|
return parentExpression.previousStatement() as? KtProperty ?: parentExpression.previousPropertyFromParent()
|
||||||
|
}
|
||||||
|
|
||||||
private class VariableDeclarationIntoWhenFix(
|
private class VariableDeclarationIntoWhenFix(
|
||||||
private val actionName: String,
|
private val actionName: String,
|
||||||
private val subjectExpressionPointer: SmartPsiElementPointer<KtExpression>,
|
private val subjectExpressionPointer: SmartPsiElementPointer<KtExpression>,
|
||||||
|
|||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
fun test() = true
|
||||||
|
|
||||||
|
fun foo(): Int {
|
||||||
|
val a<caret> = test()
|
||||||
|
return when (a) {
|
||||||
|
true -> 42
|
||||||
|
else -> null
|
||||||
|
} ?: 55
|
||||||
|
}
|
||||||
+8
@@ -0,0 +1,8 @@
|
|||||||
|
fun test() = true
|
||||||
|
|
||||||
|
fun foo(): Int {
|
||||||
|
return when (test()) {
|
||||||
|
true -> 42
|
||||||
|
else -> null
|
||||||
|
} ?: 55
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
fun test() = 42
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
val a<caret> = test()
|
||||||
|
val b = when (a) {
|
||||||
|
1 -> a
|
||||||
|
else -> 24
|
||||||
|
}
|
||||||
|
}
|
||||||
+8
@@ -0,0 +1,8 @@
|
|||||||
|
fun test() = 42
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
val b = when (val a = test()) {
|
||||||
|
1 -> a
|
||||||
|
else -> 24
|
||||||
|
}
|
||||||
|
}
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
fun test() = 42
|
||||||
|
|
||||||
|
fun foo(): Int {
|
||||||
|
val a<caret> = test()
|
||||||
|
return when (a) {
|
||||||
|
1 -> a
|
||||||
|
else -> 24
|
||||||
|
}
|
||||||
|
}
|
||||||
+8
@@ -0,0 +1,8 @@
|
|||||||
|
fun test() = 42
|
||||||
|
|
||||||
|
fun foo(): Int {
|
||||||
|
return when (val a = test()) {
|
||||||
|
1 -> a
|
||||||
|
else -> 24
|
||||||
|
}
|
||||||
|
}
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
fun test() = true
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
val a<caret> = test()
|
||||||
|
val b = !when (a) {
|
||||||
|
true -> true
|
||||||
|
else -> false
|
||||||
|
}
|
||||||
|
}
|
||||||
+8
@@ -0,0 +1,8 @@
|
|||||||
|
fun test() = true
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
val b = !when (test()) {
|
||||||
|
true -> true
|
||||||
|
else -> false
|
||||||
|
}
|
||||||
|
}
|
||||||
Vendored
+10
@@ -0,0 +1,10 @@
|
|||||||
|
// PROBLEM: none
|
||||||
|
fun test() = true
|
||||||
|
|
||||||
|
fun foo(): Int {
|
||||||
|
val a<caret> = test()
|
||||||
|
return null ?: when (a) {
|
||||||
|
true -> 42
|
||||||
|
else -> 5
|
||||||
|
}
|
||||||
|
}
|
||||||
+25
@@ -4357,6 +4357,31 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
|||||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("inBinaryExpression.kt")
|
||||||
|
public void testInBinaryExpression() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/inBinaryExpression.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("inProperty.kt")
|
||||||
|
public void testInProperty() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/inProperty.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("inReturnExpression.kt")
|
||||||
|
public void testInReturnExpression() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/inReturnExpression.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("inUnaryAndProperty.kt")
|
||||||
|
public void testInUnaryAndProperty() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/inUnaryAndProperty.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("notApplicableInBinaryExpression.kt")
|
||||||
|
public void testNotApplicableInBinaryExpression() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/notApplicableInBinaryExpression.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("notApplicableOtherName.kt")
|
@TestMetadata("notApplicableOtherName.kt")
|
||||||
public void testNotApplicableOtherName() throws Exception {
|
public void testNotApplicableOtherName() throws Exception {
|
||||||
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/notApplicableOtherName.kt");
|
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/notApplicableOtherName.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user