diff --git a/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java b/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java index 5051620e9acc..a2f0b9f98e1d 100644 --- a/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java +++ b/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java @@ -7652,6 +7652,11 @@ public abstract class HighLevelQuickFixTestGenerated extends AbstractHighLevelQu KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); } + @TestMetadata("importList.kt") + public void testImportList() throws Exception { + runTest("../../../idea/tests/testData/quickfix/moveTypeAliasToTopLevel/importList.kt"); + } + @TestMetadata("inClass.kt") public void testInClass() throws Exception { runTest("../../../idea/tests/testData/quickfix/moveTypeAliasToTopLevel/inClass.kt"); @@ -7671,6 +7676,11 @@ public abstract class HighLevelQuickFixTestGenerated extends AbstractHighLevelQu public void testInFunction2() throws Exception { runTest("../../../idea/tests/testData/quickfix/moveTypeAliasToTopLevel/inFunction2.kt"); } + + @TestMetadata("package.kt") + public void testPackage() throws Exception { + runTest("../../../idea/tests/testData/quickfix/moveTypeAliasToTopLevel/package.kt"); + } } diff --git a/plugins/kotlin/frontend-independent/src/org/jetbrains/kotlin/idea/quickfix/MoveTypeAliasToTopLevelFix.kt b/plugins/kotlin/frontend-independent/src/org/jetbrains/kotlin/idea/quickfix/MoveTypeAliasToTopLevelFix.kt index 3943c8303473..e06ca86290da 100644 --- a/plugins/kotlin/frontend-independent/src/org/jetbrains/kotlin/idea/quickfix/MoveTypeAliasToTopLevelFix.kt +++ b/plugins/kotlin/frontend-independent/src/org/jetbrains/kotlin/idea/quickfix/MoveTypeAliasToTopLevelFix.kt @@ -5,10 +5,8 @@ import com.intellij.modcommand.ActionContext import com.intellij.modcommand.ModPsiUpdater import org.jetbrains.kotlin.idea.base.resources.KotlinBundle import org.jetbrains.kotlin.idea.codeinsight.api.applicable.intentions.KotlinPsiUpdateModCommandAction -import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.KtPsiFactory import org.jetbrains.kotlin.psi.KtTypeAlias -import org.jetbrains.kotlin.psi.psiUtil.parents class MoveTypeAliasToTopLevelFix( element: KtTypeAlias, @@ -22,9 +20,8 @@ class MoveTypeAliasToTopLevelFix( elementContext: Unit, updater: ModPsiUpdater, ) { - val parents = element.parents.toList().reversed() - val containingFile = parents.firstOrNull() as? KtFile ?: return - val target = parents.getOrNull(1) ?: return + val containingFile = element.containingKtFile + val target = containingFile.importList ?: containingFile.packageDirective containingFile.addAfter(element, target) containingFile.addAfter(KtPsiFactory(actionContext.project).createNewLine(2), target) element.delete() diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/K1QuickFixTestGenerated.java b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/K1QuickFixTestGenerated.java index db8c8823af4c..cc9793a96d50 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/K1QuickFixTestGenerated.java +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/K1QuickFixTestGenerated.java @@ -11798,6 +11798,11 @@ public abstract class K1QuickFixTestGenerated extends AbstractK1QuickFixTest { KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); } + @TestMetadata("importList.kt") + public void testImportList() throws Exception { + runTest("testData/quickfix/moveTypeAliasToTopLevel/importList.kt"); + } + @TestMetadata("inClass.kt") public void testInClass() throws Exception { runTest("testData/quickfix/moveTypeAliasToTopLevel/inClass.kt"); @@ -11817,6 +11822,11 @@ public abstract class K1QuickFixTestGenerated extends AbstractK1QuickFixTest { public void testInFunction2() throws Exception { runTest("testData/quickfix/moveTypeAliasToTopLevel/inFunction2.kt"); } + + @TestMetadata("package.kt") + public void testPackage() throws Exception { + runTest("testData/quickfix/moveTypeAliasToTopLevel/package.kt"); + } } @RunWith(JUnit3RunnerWithInners.class) diff --git a/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/importList.kt b/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/importList.kt new file mode 100644 index 000000000000..a9902faf1673 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/importList.kt @@ -0,0 +1,15 @@ +// "Move typealias to top level" "true" +package bar + +import java.util.Hashtable + +class C { + typealias Foo = String + + fun bar(foo: Foo) { + } +} + +fun baz() {} +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveTypeAliasToTopLevelFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveTypeAliasToTopLevelFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/importList.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/importList.kt.after new file mode 100644 index 000000000000..09256b887833 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/importList.kt.after @@ -0,0 +1,16 @@ +// "Move typealias to top level" "true" +package bar + +import java.util.Hashtable + +typealias Foo = String + +class C { + + fun bar(foo: Foo) { + } +} + +fun baz() {} +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveTypeAliasToTopLevelFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveTypeAliasToTopLevelFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/inClass.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/inClass.kt.after index a23fbd5cbc59..c4c4abcbebf1 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/inClass.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/inClass.kt.after @@ -1,3 +1,5 @@ +typealias Foo = String + // "Move typealias to top level" "true" class C { @@ -5,8 +7,6 @@ class C { } } -typealias Foo = String - fun baz() {} // FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveTypeAliasToTopLevelFix // FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveTypeAliasToTopLevelFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/inClass2.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/inClass2.kt.after index c98b6ecacbc1..f821d682b7b1 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/inClass2.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/inClass2.kt.after @@ -1,3 +1,5 @@ +typealias Foo = String + // "Move typealias to top level" "true" class C { class CC { @@ -7,8 +9,6 @@ class C { } } -typealias Foo = String - fun baz() {} // FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveTypeAliasToTopLevelFix // FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveTypeAliasToTopLevelFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/inFunction.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/inFunction.kt.after index 8455b440b035..0239921aab2c 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/inFunction.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/inFunction.kt.after @@ -1,3 +1,5 @@ +typealias Foo = String + // "Move typealias to top level" "true" fun bar() { @@ -5,8 +7,6 @@ fun bar() { } } -typealias Foo = String - fun qux() {} // FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveTypeAliasToTopLevelFix // FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveTypeAliasToTopLevelFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/inFunction2.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/inFunction2.kt.after index cc0a95049edd..b22e5acc5f8b 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/inFunction2.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/inFunction2.kt.after @@ -1,3 +1,5 @@ +typealias Foo = String + // "Move typealias to top level" "true" fun bar() { class C { @@ -7,8 +9,6 @@ fun bar() { } } -typealias Foo = String - fun qux() {} // FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveTypeAliasToTopLevelFix /* IGNORE_K2 */ \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/package.kt b/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/package.kt new file mode 100644 index 000000000000..b2a7753cc2b8 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/package.kt @@ -0,0 +1,13 @@ +// "Move typealias to top level" "true" +package bar + +class C { + typealias Foo = String + + fun bar(foo: Foo) { + } +} + +fun baz() {} +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveTypeAliasToTopLevelFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveTypeAliasToTopLevelFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/package.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/package.kt.after new file mode 100644 index 000000000000..6d5bbbb196fe --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/quickfix/moveTypeAliasToTopLevel/package.kt.after @@ -0,0 +1,14 @@ +// "Move typealias to top level" "true" +package bar + +typealias Foo = String + +class C { + + fun bar(foo: Foo) { + } +} + +fun baz() {} +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveTypeAliasToTopLevelFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveTypeAliasToTopLevelFix \ No newline at end of file