From 4db2e44f9ebfe876e08666f8e603414aaf3bc03a Mon Sep 17 00:00:00 2001 From: aleksandrina-streltsova Date: Wed, 17 Jan 2024 12:21:17 +0200 Subject: [PATCH] [kotlin] refactor "Introduce Variable": use `firstIsInstance` instead of loop-check KTIJ-27855 GitOrigin-RevId: d3c2ca95bc8f5d501c1c3dbb151f8c79d7e405a7 --- .../refactoring/introduce/ExtractionTestGenerated.java | 5 +++++ .../IntroduceDestructuringDeclarationAndCreateBlock.kt | 5 +++++ ...roduceDestructuringDeclarationAndCreateBlock.kt.after | 7 +++++++ .../introduce/KotlinIntroduceVariableContext.kt | 9 ++++----- .../K2IntroduceVariableTestGenerated.java | 5 +++++ 5 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceVariable/IntroduceDestructuringDeclarationAndCreateBlock.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceVariable/IntroduceDestructuringDeclarationAndCreateBlock.kt.after 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");