diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceVariable/KotlinIntroduceVariableHandler.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceVariable/KotlinIntroduceVariableHandler.kt
index 706f206efff..c989f1879d8 100644
--- a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceVariable/KotlinIntroduceVariableHandler.kt
+++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceVariable/KotlinIntroduceVariableHandler.kt
@@ -529,7 +529,7 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
else {
expression.parent as KtElement
}
- var commonContainer = commonParent.getContainer()!!
+ var commonContainer = commonParent as? KtFile ?: commonParent.getContainer()!!
if (commonContainer != container && container.isAncestor(commonContainer, true)) {
commonContainer = container
}
diff --git a/idea/testData/refactoring/introduceProperty/kt21530.kt b/idea/testData/refactoring/introduceProperty/kt21530.kt
new file mode 100644
index 00000000000..69f00a155bf
--- /dev/null
+++ b/idea/testData/refactoring/introduceProperty/kt21530.kt
@@ -0,0 +1,6 @@
+// EXTRACTION_TARGET: property with initializer
+
+fun test() = "123"
+
+val f = test() + "456"
+val x = test()
\ No newline at end of file
diff --git a/idea/testData/refactoring/introduceProperty/kt21530.kt.after b/idea/testData/refactoring/introduceProperty/kt21530.kt.after
new file mode 100644
index 00000000000..fe67e0165fd
--- /dev/null
+++ b/idea/testData/refactoring/introduceProperty/kt21530.kt.after
@@ -0,0 +1,8 @@
+// EXTRACTION_TARGET: property with initializer
+
+fun test() = "123"
+
+private val s = test()
+
+val f = s + "456"
+val x = s
\ No newline at end of file
diff --git a/idea/testData/refactoring/introduceVariable/kt21530_withConstructorParam.kt b/idea/testData/refactoring/introduceVariable/kt21530_withConstructorParam.kt
new file mode 100644
index 00000000000..477dcafc370
--- /dev/null
+++ b/idea/testData/refactoring/introduceVariable/kt21530_withConstructorParam.kt
@@ -0,0 +1,8 @@
+fun test() = "123"
+
+open class First(val s: String)
+object Second : First(test() + "456")
+
+fun foo(test: String) {
+ val x = test()
+}
\ No newline at end of file
diff --git a/idea/testData/refactoring/introduceVariable/kt21530_withConstructorParam.kt.after b/idea/testData/refactoring/introduceVariable/kt21530_withConstructorParam.kt.after
new file mode 100644
index 00000000000..e55cd8e235b
--- /dev/null
+++ b/idea/testData/refactoring/introduceVariable/kt21530_withConstructorParam.kt.after
@@ -0,0 +1,11 @@
+fun test() = "123"
+
+open class First(val s: String)
+
+val test1 = test()
+
+object Second : First(test1 + "456")
+
+fun foo(test: String) {
+ val x = test1
+}
\ No newline at end of file
diff --git a/idea/testData/refactoring/introduceVariable/kt21530_withGlobal.kt b/idea/testData/refactoring/introduceVariable/kt21530_withGlobal.kt
new file mode 100644
index 00000000000..0fa73cc424e
--- /dev/null
+++ b/idea/testData/refactoring/introduceVariable/kt21530_withGlobal.kt
@@ -0,0 +1,4 @@
+fun test() = "123"
+
+val f = test() + "456"
+val x = test()
\ No newline at end of file
diff --git a/idea/testData/refactoring/introduceVariable/kt21530_withGlobal.kt.after b/idea/testData/refactoring/introduceVariable/kt21530_withGlobal.kt.after
new file mode 100644
index 00000000000..e54647b9464
--- /dev/null
+++ b/idea/testData/refactoring/introduceVariable/kt21530_withGlobal.kt.after
@@ -0,0 +1,5 @@
+fun test() = "123"
+
+val test = test()
+val f = test + "456"
+val x = test
\ No newline at end of file
diff --git a/idea/testData/refactoring/introduceVariable/kt21530_withParam.kt b/idea/testData/refactoring/introduceVariable/kt21530_withParam.kt
new file mode 100644
index 00000000000..90d8a55f5f8
--- /dev/null
+++ b/idea/testData/refactoring/introduceVariable/kt21530_withParam.kt
@@ -0,0 +1,7 @@
+fun test() = "123"
+
+val f = test() + "456"
+
+fun foo(test: String) {
+ val x = test()
+}
\ No newline at end of file
diff --git a/idea/testData/refactoring/introduceVariable/kt21530_withParam.kt.after b/idea/testData/refactoring/introduceVariable/kt21530_withParam.kt.after
new file mode 100644
index 00000000000..c8c6c7b08cd
--- /dev/null
+++ b/idea/testData/refactoring/introduceVariable/kt21530_withParam.kt.after
@@ -0,0 +1,8 @@
+fun test() = "123"
+
+val test1 = test()
+val f = test1 + "456"
+
+fun foo(test: String) {
+ val x = test1
+}
\ No newline at end of file
diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/introduce/ExtractionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/refactoring/introduce/ExtractionTestGenerated.java
index 21f6d54f993..ab50fb3cb6d 100644
--- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/introduce/ExtractionTestGenerated.java
+++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/introduce/ExtractionTestGenerated.java
@@ -236,6 +236,24 @@ public class ExtractionTestGenerated extends AbstractExtractionTest {
doIntroduceVariableTest(fileName);
}
+ @TestMetadata("kt21530_withConstructorParam.kt")
+ public void testKt21530_withConstructorParam() throws Exception {
+ String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/kt21530_withConstructorParam.kt");
+ doIntroduceVariableTest(fileName);
+ }
+
+ @TestMetadata("kt21530_withGlobal.kt")
+ public void testKt21530_withGlobal() throws Exception {
+ String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/kt21530_withGlobal.kt");
+ doIntroduceVariableTest(fileName);
+ }
+
+ @TestMetadata("kt21530_withParam.kt")
+ public void testKt21530_withParam() throws Exception {
+ String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/kt21530_withParam.kt");
+ doIntroduceVariableTest(fileName);
+ }
+
@TestMetadata("LoopRange.kt")
public void testLoopRange() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/LoopRange.kt");
@@ -3173,6 +3191,12 @@ public class ExtractionTestGenerated extends AbstractExtractionTest {
doIntroducePropertyTest(fileName);
}
+ @TestMetadata("kt21530.kt")
+ public void testKt21530() throws Exception {
+ String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceProperty/kt21530.kt");
+ doIntroducePropertyTest(fileName);
+ }
+
@TestMetadata("primaryConstructorParameterReference.kt")
public void testPrimaryConstructorParameterReference() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceProperty/primaryConstructorParameterReference.kt");