[kotlin] MoveTypeAliasToTopLevelFix: move the alias to the beginning of the file

IJ-CR-136052
^KTIJ-29516

GitOrigin-RevId: 4e6c4ec39343571c6566b96463e50dbdc85a1283
This commit is contained in:
Andrey Cherkasov
2024-06-21 19:25:40 +00:00
committed by intellij-monorepo-bot
parent 4b876f7064
commit 9a8f802fca
11 changed files with 88 additions and 13 deletions
@@ -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");
}
}
@@ -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()
@@ -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)
@@ -0,0 +1,15 @@
// "Move typealias to top level" "true"
package bar
import java.util.Hashtable
class C {
<caret>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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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 */
@@ -0,0 +1,13 @@
// "Move typealias to top level" "true"
package bar
class C {
<caret>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
@@ -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