[kotlin] KTIJ-35524 Fix "Safe delete" of variable in when condition deleting the whole condition

GitOrigin-RevId: a9243ac23571cc19ad9ca6ec27297ce9d14147bf
This commit is contained in:
Andrey Cherkasov
2025-09-16 11:47:12 +00:00
committed by intellij-monorepo-bot
parent 20eb5d3c95
commit b708ab3f4d
5 changed files with 45 additions and 0 deletions
@@ -554,6 +554,11 @@ public abstract class SafeDeleteTestGenerated extends AbstractSafeDeleteTest {
public void testPropertyInLocalObject2() throws Exception {
runTest("testData/refactoring/safeDelete/deleteProperty/kotlinProperty/propertyInLocalObject2.kt");
}
@TestMetadata("when.kt")
public void testWhen() throws Exception {
runTest("testData/refactoring/safeDelete/deleteProperty/kotlinProperty/when.kt");
}
}
@RunWith(JUnit3RunnerWithInners.class)
@@ -0,0 +1,13 @@
// IGNORE_K1
enum class Direction {
NORTH, SOUTH, WEST, EAST
}
fun foo(direction: Direction): String {
return when (/* lead */ val x<caret> /* mid */ = /* rhs */ direction /* trail */) {
Direction.NORTH -> "north"
Direction.SOUTH -> "south"
Direction.WEST -> "west"
Direction.EAST -> "east"
}
}
@@ -0,0 +1,13 @@
// IGNORE_K1
enum class Direction {
NORTH, SOUTH, WEST, EAST
}
fun foo(direction: Direction): String {
return when (/* lead */ /* mid */ /* rhs */ direction /* trail */) {
Direction.NORTH -> "north"
Direction.SOUTH -> "south"
Direction.WEST -> "west"
Direction.EAST -> "east"
}
}
@@ -48,6 +48,7 @@ import org.jetbrains.kotlin.idea.references.mainReference
import org.jetbrains.kotlin.idea.search.ExpectActualUtils
import org.jetbrains.kotlin.idea.searching.inheritors.DirectKotlinClassInheritorsSearch
import org.jetbrains.kotlin.idea.searching.inheritors.findAllOverridings
import org.jetbrains.kotlin.idea.util.CommentSaver
import org.jetbrains.kotlin.idea.util.application.isUnitTestMode
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
@@ -357,6 +358,14 @@ class KotlinFirSafeDeleteProcessor : SafeDeleteProcessorDelegateBase() {
deleteBracesAroundEmptyList(element)
}
is KtProperty if ((element.parent as? KtWhenExpression)?.subjectVariable === element) -> {
val commentSaver = CommentSaver(element)
element.initializer?.let { initializer ->
val replaced = element.replace(initializer.copy())
commentSaver.restore(replaced)
}
}
is KtParameter -> {
element.ownerFunction?.let {
with(KotlinSafeDeleteSettings) {
@@ -525,6 +525,11 @@ public abstract class K2SafeDeleteTestGenerated extends AbstractK2SafeDeleteTest
public void testPropertyInLocalObject2() throws Exception {
runTest("../../idea/tests/testData/refactoring/safeDelete/deleteProperty/kotlinProperty/propertyInLocalObject2.kt");
}
@TestMetadata("when.kt")
public void testWhen() throws Exception {
runTest("../../idea/tests/testData/refactoring/safeDelete/deleteProperty/kotlinProperty/when.kt");
}
}
@RunWith(JUnit3RunnerWithInners.class)