diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/refactoring/inline/InlineTestGenerated.java b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/refactoring/inline/InlineTestGenerated.java index 804000529402..fda844a6d694 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/refactoring/inline/InlineTestGenerated.java +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/refactoring/inline/InlineTestGenerated.java @@ -773,6 +773,11 @@ public abstract class InlineTestGenerated extends AbstractInlineTest { public void testReturnNull() throws Exception { runTest("testData/refactoring/inline/inlineVariableOrProperty/property/returnNull.kt"); } + + @TestMetadata("when.kt") + public void testWhen() throws Exception { + runTest("testData/refactoring/inline/inlineVariableOrProperty/property/when.kt"); + } } } diff --git a/plugins/kotlin/idea/tests/testData/refactoring/inline/inlineVariableOrProperty/property/when.kt b/plugins/kotlin/idea/tests/testData/refactoring/inline/inlineVariableOrProperty/property/when.kt new file mode 100644 index 000000000000..629efdabca9e --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/inline/inlineVariableOrProperty/property/when.kt @@ -0,0 +1,6 @@ +val x = 42 +fun callUsage() { + when ((x)) { + else -> { println(6) } + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/refactoring/inline/inlineVariableOrProperty/property/when.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/inline/inlineVariableOrProperty/property/when.kt.after new file mode 100644 index 000000000000..85083ef8117d --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/inline/inlineVariableOrProperty/property/when.kt.after @@ -0,0 +1,5 @@ +fun callUsage() { + when { + else -> { println(6) } + } +} diff --git a/plugins/kotlin/refactorings/kotlin.refactorings.common/src/org/jetbrains/kotlin/idea/refactoring/inline/codeInliner/ReplacementPerformer.kt b/plugins/kotlin/refactorings/kotlin.refactorings.common/src/org/jetbrains/kotlin/idea/refactoring/inline/codeInliner/ReplacementPerformer.kt index e553c058e45b..cf825646b930 100644 --- a/plugins/kotlin/refactorings/kotlin.refactorings.common/src/org/jetbrains/kotlin/idea/refactoring/inline/codeInliner/ReplacementPerformer.kt +++ b/plugins/kotlin/refactorings/kotlin.refactorings.common/src/org/jetbrains/kotlin/idea/refactoring/inline/codeInliner/ReplacementPerformer.kt @@ -19,6 +19,7 @@ import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.PsiChildRange import org.jetbrains.kotlin.psi.psiUtil.canPlaceAfterSimpleNameEntry import org.jetbrains.kotlin.psi.psiUtil.findDescendantOfType +import org.jetbrains.kotlin.psi.psiUtil.parents import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments import org.jetbrains.kotlin.utils.addToStdlib.safeAs @@ -178,7 +179,14 @@ class ExpressionReplacementPerformer( } } if (canDropElementToBeReplaced) { - stub.delete() + val parent = stub.parents.first { it !is KtParenthesizedExpression } + if (parent is KtWhenExpression && + parent.subjectExpression?.safeDeparenthesize() == stub && + parent.leftParenthesis != null && parent.rightParenthesis != null) { + parent.deleteChildRange(parent.leftParenthesis, parent.rightParenthesis) + } else { + stub.delete() + } null } else { stub.replaced(psiFactory.createExpression("Unit")) diff --git a/plugins/kotlin/refactorings/kotlin.refactorings.tests.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/inline/KotlinFirInlineTestGenerated.java b/plugins/kotlin/refactorings/kotlin.refactorings.tests.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/inline/KotlinFirInlineTestGenerated.java index 8092f7c4a487..aa54a7af1105 100644 --- a/plugins/kotlin/refactorings/kotlin.refactorings.tests.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/inline/KotlinFirInlineTestGenerated.java +++ b/plugins/kotlin/refactorings/kotlin.refactorings.tests.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/inline/KotlinFirInlineTestGenerated.java @@ -612,6 +612,11 @@ public abstract class KotlinFirInlineTestGenerated extends AbstractKotlinFirInli public void testReturnNull() throws Exception { runTest("../../idea/tests/testData/refactoring/inline/inlineVariableOrProperty/property/returnNull.kt"); } + + @TestMetadata("when.kt") + public void testWhen() throws Exception { + runTest("../../idea/tests/testData/refactoring/inline/inlineVariableOrProperty/property/when.kt"); + } } }