[kotlin] refactor "Introduce Variable": use firstIsInstance instead of loop-check

KTIJ-27855

GitOrigin-RevId: d3c2ca95bc8f5d501c1c3dbb151f8c79d7e405a7
This commit is contained in:
aleksandrina-streltsova
2024-01-17 13:01:04 +00:00
committed by intellij-monorepo-bot
parent 5b77cadb81
commit 4db2e44f9e
5 changed files with 26 additions and 5 deletions
@@ -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");
@@ -0,0 +1,5 @@
fun test(n: Int) {
if (n > 0) <selection>Dimension(1, 2)</selection>
}
data class Dimension(val width: Int, val height: Int)
@@ -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)
@@ -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<KtDeclaration>()
if (commonContainer is KtContainerNode) {
if (commonContainer.parent is KtIfExpression) {
val next = commonContainer.nextSibling
@@ -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");