From bda0682dc8074b7dc4b100676bf002552170c7ad Mon Sep 17 00:00:00 2001 From: Andrey Cherkasov Date: Thu, 3 Oct 2024 19:54:10 +0400 Subject: [PATCH] [kotlin] Extract variable: choose class body as container when introducing class property and replacing all occurrences ^KTIJ-9043 Fixed ^KTIJ-14455 Fixed GitOrigin-RevId: cabc4ee5ad81253f9f51dedfe072a54c9b18923a --- .../refactoring/introduce/ExtractionTestGenerated.java | 10 ++++++++++ .../refactoring/introduceVariable/KTIJ-14455.kt | 5 +++++ .../refactoring/introduceVariable/KTIJ-14455.kt.after | 6 ++++++ .../refactoring/introduceVariable/KTIJ-9043.kt | 4 ++++ .../refactoring/introduceVariable/KTIJ-9043.kt.after | 5 +++++ .../introduce/KotlinIntroduceVariableHandler.kt | 2 +- .../K2IntroduceVariableTestGenerated.java | 10 ++++++++++ 7 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceVariable/KTIJ-14455.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceVariable/KTIJ-14455.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceVariable/KTIJ-9043.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceVariable/KTIJ-9043.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 ba1225639122..b6ce2be3304f 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 @@ -872,6 +872,16 @@ public abstract class ExtractionTestGenerated extends AbstractExtractionTest { runTest("testData/refactoring/introduceVariable/javaInnerClassQualifier.kt"); } + @TestMetadata("KTIJ-14455.kt") + public void testKTIJ_14455() throws Exception { + runTest("testData/refactoring/introduceVariable/KTIJ-14455.kt"); + } + + @TestMetadata("KTIJ-9043.kt") + public void testKTIJ_9043() throws Exception { + runTest("testData/refactoring/introduceVariable/KTIJ-9043.kt"); + } + @TestMetadata("kt10808.kt") public void testKt10808() throws Exception { runTest("testData/refactoring/introduceVariable/kt10808.kt"); diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceVariable/KTIJ-14455.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceVariable/KTIJ-14455.kt new file mode 100644 index 000000000000..adb5fa8076a1 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceVariable/KTIJ-14455.kt @@ -0,0 +1,5 @@ +class X { + val s0 = "" + val s1 = s0.substring(0) + val s2 = s0.substring(0) +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceVariable/KTIJ-14455.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceVariable/KTIJ-14455.kt.after new file mode 100644 index 000000000000..587db03576df --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceVariable/KTIJ-14455.kt.after @@ -0,0 +1,6 @@ +class X { + val s0 = "" + val substring = s0.substring(0) + val s1 = substring + val s2 = substring +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceVariable/KTIJ-9043.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceVariable/KTIJ-9043.kt new file mode 100644 index 000000000000..4f4f9650ff68 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceVariable/KTIJ-9043.kt @@ -0,0 +1,4 @@ +class Foo { + val bar = 0 + val baz = 0 +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceVariable/KTIJ-9043.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceVariable/KTIJ-9043.kt.after new file mode 100644 index 000000000000..3fbce9b2918d --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceVariable/KTIJ-9043.kt.after @@ -0,0 +1,5 @@ +class Foo { + val i = 0 + val bar = i + val baz = i +} diff --git a/plugins/kotlin/refactorings/kotlin.refactorings.common/src/org/jetbrains/kotlin/idea/refactoring/introduce/KotlinIntroduceVariableHandler.kt b/plugins/kotlin/refactorings/kotlin.refactorings.common/src/org/jetbrains/kotlin/idea/refactoring/introduce/KotlinIntroduceVariableHandler.kt index 5d5b80f61b79..b4b9beec8376 100644 --- a/plugins/kotlin/refactorings/kotlin.refactorings.common/src/org/jetbrains/kotlin/idea/refactoring/introduce/KotlinIntroduceVariableHandler.kt +++ b/plugins/kotlin/refactorings/kotlin.refactorings.common/src/org/jetbrains/kotlin/idea/refactoring/introduce/KotlinIntroduceVariableHandler.kt @@ -217,7 +217,7 @@ abstract class KotlinIntroduceVariableHandler : RefactoringActionHandler { } fun KtElement.getContainer(): KtElement? { - if (this is KtBlockExpression) return this + if (this is KtBlockExpression || this is KtClassBody) return this return (parentsWithSelf.zip(parents)).firstOrNull { val (place, parent) = it diff --git a/plugins/kotlin/refactorings/kotlin.refactorings.tests.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/introduce/introduceVariable/K2IntroduceVariableTestGenerated.java b/plugins/kotlin/refactorings/kotlin.refactorings.tests.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/introduce/introduceVariable/K2IntroduceVariableTestGenerated.java index 3231833534d1..037ca7b66dc3 100644 --- a/plugins/kotlin/refactorings/kotlin.refactorings.tests.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/introduce/introduceVariable/K2IntroduceVariableTestGenerated.java +++ b/plugins/kotlin/refactorings/kotlin.refactorings.tests.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/introduce/introduceVariable/K2IntroduceVariableTestGenerated.java @@ -870,6 +870,16 @@ public abstract class K2IntroduceVariableTestGenerated extends AbstractK2Introdu runTest("../../idea/tests/testData/refactoring/introduceVariable/javaInnerClassQualifier.kt"); } + @TestMetadata("KTIJ-14455.kt") + public void testKTIJ_14455() throws Exception { + runTest("../../idea/tests/testData/refactoring/introduceVariable/KTIJ-14455.kt"); + } + + @TestMetadata("KTIJ-9043.kt") + public void testKTIJ_9043() throws Exception { + runTest("../../idea/tests/testData/refactoring/introduceVariable/KTIJ-9043.kt"); + } + @TestMetadata("kt10808.kt") public void testKt10808() throws Exception { runTest("../../idea/tests/testData/refactoring/introduceVariable/kt10808.kt");