diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/refactoring/introduce/ExtractionTestGenerated.java b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/refactoring/introduce/ExtractionTestGenerated.java
index 4e18dd762162..bbd755528c5a 100644
--- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/refactoring/introduce/ExtractionTestGenerated.java
+++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/refactoring/introduce/ExtractionTestGenerated.java
@@ -478,6 +478,11 @@ public abstract class ExtractionTestGenerated extends AbstractExtractionTest {
runTest("testData/refactoring/introduceVariable/IntroduceAndCreateBlock.kt");
}
+ @TestMetadata("IntroduceDestructuringDeclarationAndCreateBlock.kt")
+ public void testIntroduceDestructuringDeclarationAndCreateBlock() throws Exception {
+ runTest("testData/refactoring/introduceVariable/IntroduceDestructuringDeclarationAndCreateBlock.kt");
+ }
+
@TestMetadata("IntroduceLambdaAndCreateBlock.kt")
public void testIntroduceLambdaAndCreateBlock() throws Exception {
runTest("testData/refactoring/introduceVariable/IntroduceLambdaAndCreateBlock.kt");
diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceVariable/IntroduceDestructuringDeclarationAndCreateBlock.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceVariable/IntroduceDestructuringDeclarationAndCreateBlock.kt
new file mode 100644
index 000000000000..2b9ddb520cbe
--- /dev/null
+++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceVariable/IntroduceDestructuringDeclarationAndCreateBlock.kt
@@ -0,0 +1,5 @@
+fun test(n: Int) {
+ if (n > 0) Dimension(1, 2)
+}
+
+data class Dimension(val width: Int, val height: Int)
\ No newline at end of file
diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceVariable/IntroduceDestructuringDeclarationAndCreateBlock.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceVariable/IntroduceDestructuringDeclarationAndCreateBlock.kt.after
new file mode 100644
index 000000000000..d1e713ce5f61
--- /dev/null
+++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceVariable/IntroduceDestructuringDeclarationAndCreateBlock.kt.after
@@ -0,0 +1,7 @@
+fun test(n: Int) {
+ if (n > 0) {
+ val (width, height) = Dimension(1, 2)
+ }
+}
+
+data class Dimension(val width: Int, val height: Int)
diff --git a/plugins/kotlin/refactorings/kotlin.refactorings.common/src/org/jetbrains/kotlin/idea/refactoring/introduce/KotlinIntroduceVariableContext.kt b/plugins/kotlin/refactorings/kotlin.refactorings.common/src/org/jetbrains/kotlin/idea/refactoring/introduce/KotlinIntroduceVariableContext.kt
index 06c0fc2eb8d1..7971822f26a0 100644
--- a/plugins/kotlin/refactorings/kotlin.refactorings.common/src/org/jetbrains/kotlin/idea/refactoring/introduce/KotlinIntroduceVariableContext.kt
+++ b/plugins/kotlin/refactorings/kotlin.refactorings.common/src/org/jetbrains/kotlin/idea/refactoring/introduce/KotlinIntroduceVariableContext.kt
@@ -17,6 +17,8 @@ import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.*
+import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstance
+import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
import org.jetbrains.kotlin.utils.exceptions.errorWithAttachment
import org.jetbrains.kotlin.utils.exceptions.withPsiEntry
import org.jetbrains.kotlin.utils.sure
@@ -216,11 +218,8 @@ abstract class KotlinIntroduceVariableContext(
}
emptyBody = anchor.replace(emptyBody) as KtBlockExpression
}
- for (child in emptyBody.children) {
- if (child is KtProperty) {
- property = child
- }
- }
+ property = emptyBody.children.firstIsInstance()
+
if (commonContainer is KtContainerNode) {
if (commonContainer.parent is KtIfExpression) {
val next = commonContainer.nextSibling
diff --git a/plugins/kotlin/refactorings/kotlin.refactorings.introduce.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/introduce/introduceVariable/K2IntroduceVariableTestGenerated.java b/plugins/kotlin/refactorings/kotlin.refactorings.introduce.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/introduce/introduceVariable/K2IntroduceVariableTestGenerated.java
index a3a7392ed61e..5468e52b3e1f 100644
--- a/plugins/kotlin/refactorings/kotlin.refactorings.introduce.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/introduce/introduceVariable/K2IntroduceVariableTestGenerated.java
+++ b/plugins/kotlin/refactorings/kotlin.refactorings.introduce.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/introduce/introduceVariable/K2IntroduceVariableTestGenerated.java
@@ -476,6 +476,11 @@ public abstract class K2IntroduceVariableTestGenerated extends AbstractK2Introdu
runTest("../../idea/tests/testData/refactoring/introduceVariable/IntroduceAndCreateBlock.kt");
}
+ @TestMetadata("IntroduceDestructuringDeclarationAndCreateBlock.kt")
+ public void testIntroduceDestructuringDeclarationAndCreateBlock() throws Exception {
+ runTest("../../idea/tests/testData/refactoring/introduceVariable/IntroduceDestructuringDeclarationAndCreateBlock.kt");
+ }
+
@TestMetadata("IntroduceLambdaAndCreateBlock.kt")
public void testIntroduceLambdaAndCreateBlock() throws Exception {
runTest("../../idea/tests/testData/refactoring/introduceVariable/IntroduceLambdaAndCreateBlock.kt");