From 4ab25bd0bbe070c0321099357f5648f2d8161332 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Mon, 8 Aug 2016 19:22:14 +0300 Subject: [PATCH] Move: Quote package name (if necessary) when moving declarations to new file #KT-13385 Fixed --- ChangeLog.md | 1 + .../kotlin/idea/refactoring/kotlinRefactoringUtil.kt | 8 +++----- .../moveClassToNewFileAndQuotePackage/after/p/main.kt | 5 +++++ .../after/q/in/fun/To.kt | 3 +++ .../after/q/in/fun/usage.kt | 5 +++++ .../moveClassToNewFileAndQuotePackage/before/p/main.kt | 4 ++++ .../before/q/in/fun/usage.kt | 5 +++++ .../moveClassToNewFileAndQuotePackage.test | 5 +++++ .../kotlin/idea/refactoring/move/MoveTestGenerated.java | 6 ++++++ 9 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveClassToNewFileAndQuotePackage/after/p/main.kt create mode 100644 idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveClassToNewFileAndQuotePackage/after/q/in/fun/To.kt create mode 100644 idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveClassToNewFileAndQuotePackage/after/q/in/fun/usage.kt create mode 100644 idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveClassToNewFileAndQuotePackage/before/p/main.kt create mode 100644 idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveClassToNewFileAndQuotePackage/before/q/in/fun/usage.kt create mode 100644 idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveClassToNewFileAndQuotePackage/moveClassToNewFileAndQuotePackage.test diff --git a/ChangeLog.md b/ChangeLog.md index fc4c88b54a2..17712ae9300 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -15,6 +15,7 @@ - [`KT-13383`](https://youtrack.jetbrains.com/issue/KT-13383), [`KT-13379`](https://youtrack.jetbrains.com/issue/KT-13379) Override/Implement Members: Do not make return type non-nullable if base return type is explicitly nullable - [`KT-13244`](https://youtrack.jetbrains.com/issue/KT-13244) Override/Implement Members: Do not expand type aliases in the generated members - [`KT-13218`](https://youtrack.jetbrains.com/issue/KT-13218) Extract Function: Fix AssertionError on callable references +- [`KT-13385`](https://youtrack.jetbrains.com/issue/KT-13385) Move: Quote package name (if necessary) when moving declarations to new file ## 1.1-M01 (EAP-1) diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/kotlinRefactoringUtil.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/kotlinRefactoringUtil.kt index 1477b696c45..45e8bb4355c 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/kotlinRefactoringUtil.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/kotlinRefactoringUtil.kt @@ -67,10 +67,7 @@ import org.jetbrains.kotlin.idea.KotlinFileType import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.getJavaMemberDescriptor import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor -import org.jetbrains.kotlin.idea.core.KotlinNameSuggester -import org.jetbrains.kotlin.idea.core.ShortenReferences -import org.jetbrains.kotlin.idea.core.getPackage -import org.jetbrains.kotlin.idea.core.quoteIfNeeded +import org.jetbrains.kotlin.idea.core.* import org.jetbrains.kotlin.idea.intentions.RemoveCurlyBracesFromTemplateIntention import org.jetbrains.kotlin.idea.j2k.IdeaJavaToKotlinServices import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers @@ -107,8 +104,9 @@ fun createKotlinFile(fileName: String, targetDir: PsiDirectory, packageName: String? = targetDir.getPackage()?.qualifiedName): KtFile { targetDir.checkCreateFile(fileName) + val packageFqName = packageName?.let(::FqName) ?: FqName.ROOT val file = PsiFileFactory.getInstance(targetDir.project).createFileFromText( - fileName, KotlinFileType.INSTANCE, if (!packageName.isNullOrBlank()) "package $packageName \n\n" else "" + fileName, KotlinFileType.INSTANCE, if (!packageFqName.isRoot) "package ${packageFqName.quoteSegmentsIfNeeded()} \n\n" else "" ) return targetDir.add(file) as KtFile diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveClassToNewFileAndQuotePackage/after/p/main.kt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveClassToNewFileAndQuotePackage/after/p/main.kt new file mode 100644 index 00000000000..d95381f35c9 --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveClassToNewFileAndQuotePackage/after/p/main.kt @@ -0,0 +1,5 @@ +package p + +import q.`in`.`fun`.To + +class From : To() {} \ No newline at end of file diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveClassToNewFileAndQuotePackage/after/q/in/fun/To.kt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveClassToNewFileAndQuotePackage/after/q/in/fun/To.kt new file mode 100644 index 00000000000..4d2a5a50c4f --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveClassToNewFileAndQuotePackage/after/q/in/fun/To.kt @@ -0,0 +1,3 @@ +package q.`in`.`fun` + +open class To {} \ No newline at end of file diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveClassToNewFileAndQuotePackage/after/q/in/fun/usage.kt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveClassToNewFileAndQuotePackage/after/q/in/fun/usage.kt new file mode 100644 index 00000000000..58d268463f7 --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveClassToNewFileAndQuotePackage/after/q/in/fun/usage.kt @@ -0,0 +1,5 @@ +package q.`in`.`fun` + +import q.`in`.`fun`.To + +class Usage : To() {} \ No newline at end of file diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveClassToNewFileAndQuotePackage/before/p/main.kt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveClassToNewFileAndQuotePackage/before/p/main.kt new file mode 100644 index 00000000000..b040c9181fd --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveClassToNewFileAndQuotePackage/before/p/main.kt @@ -0,0 +1,4 @@ +package p + +open class To {} +class From : To() {} \ No newline at end of file diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveClassToNewFileAndQuotePackage/before/q/in/fun/usage.kt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveClassToNewFileAndQuotePackage/before/q/in/fun/usage.kt new file mode 100644 index 00000000000..77fb80ca0e4 --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveClassToNewFileAndQuotePackage/before/q/in/fun/usage.kt @@ -0,0 +1,5 @@ +package q.`in`.`fun` + +import p.To + +class Usage : To() {} \ No newline at end of file diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveClassToNewFileAndQuotePackage/moveClassToNewFileAndQuotePackage.test b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveClassToNewFileAndQuotePackage/moveClassToNewFileAndQuotePackage.test new file mode 100644 index 00000000000..65a57bb97f6 --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveClassToNewFileAndQuotePackage/moveClassToNewFileAndQuotePackage.test @@ -0,0 +1,5 @@ +{ + "mainFile": "p/main.kt", + "type": "MOVE_KOTLIN_TOP_LEVEL_DECLARATIONS", + "targetPackage": "q.in.fun" +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/MoveTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/MoveTestGenerated.java index 74c64221dfd..480e63dfa9f 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/MoveTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/MoveTestGenerated.java @@ -473,6 +473,12 @@ public class MoveTestGenerated extends AbstractMoveTest { doTest(fileName); } + @TestMetadata("kotlin/moveTopLevelDeclarations/moveClassToNewFileAndQuotePackage/moveClassToNewFileAndQuotePackage.test") + public void testKotlin_moveTopLevelDeclarations_moveClassToNewFileAndQuotePackage_MoveClassToNewFileAndQuotePackage() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveClassToNewFileAndQuotePackage/moveClassToNewFileAndQuotePackage.test"); + doTest(fileName); + } + @TestMetadata("kotlin/moveTopLevelDeclarations/moveClassToPackage/moveClassToPackage.test") public void testKotlin_moveTopLevelDeclarations_moveClassToPackage_MoveClassToPackage() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveClassToPackage/moveClassToPackage.test");