From 6b57a3e338470494908bd2010bb96f3935be6cc5 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Wed, 21 Dec 2016 13:41:33 +0300 Subject: [PATCH] Intentions: Implement intention which rename file according to the top-level class name #KT-15068 Fixed --- ChangeLog.md | 10 ++++ .../after.kt.template | 5 ++ .../before.kt.template | 5 ++ .../description.html | 5 ++ idea/src/META-INF/plugin.xml | 5 ++ .../RenameFileToMatchClassIntention.kt | 48 +++++++++++++++++++ .../testData/quickfix/implement/annotation.kt | 1 + idea/testData/quickfix/implement/enum.kt | 1 + .../testData/quickfix/implement/finalClass.kt | 1 + .../quickfix/modifiers/noAbstractForObject.kt | 1 + .../quickfix/removeUnused/importEnumValues.kt | 1 + .../quickfix/removeUnused/importObjectFun.kt | 1 + .../quickfix/removeUnused/usedClassAsAlias.kt | 1 + .../removeUnused/usedObjectAsAlias.kt | 1 + .../usedObjectAsAliasMulti.before.Main.kt | 1 + .../unusedSuppressAnnotation/notForJava.kt | 1 + 16 files changed, 88 insertions(+) create mode 100644 idea/resources/intentionDescriptions/RenameFileToMatchClassIntention/after.kt.template create mode 100644 idea/resources/intentionDescriptions/RenameFileToMatchClassIntention/before.kt.template create mode 100644 idea/resources/intentionDescriptions/RenameFileToMatchClassIntention/description.html create mode 100644 idea/src/org/jetbrains/kotlin/idea/intentions/RenameFileToMatchClassIntention.kt diff --git a/ChangeLog.md b/ChangeLog.md index 310522269c3..4750b20ba26 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -464,6 +464,16 @@ These artifacts include extensions for the types available in the latter JDKs, s + [`KT-12389`](https://youtrack.jetbrains.com/issue/KT-12389) Do not exit from REPL when toString() of user class throws an exception + [`KT-12129`](https://youtrack.jetbrains.com/issue/KT-12129) Fixed link on api reference page in KDoc +## 1.0.7 + +### IDE + +#### Intention actions, inspections and quickfixes + +##### New features + +- [`KT-15068`](https://youtrack.jetbrains.com/issue/KT-15068) Implement intention which rename file according to the top-level class name + ## 1.0.6 ### IDE diff --git a/idea/resources/intentionDescriptions/RenameFileToMatchClassIntention/after.kt.template b/idea/resources/intentionDescriptions/RenameFileToMatchClassIntention/after.kt.template new file mode 100644 index 00000000000..5ea8c3cf589 --- /dev/null +++ b/idea/resources/intentionDescriptions/RenameFileToMatchClassIntention/after.kt.template @@ -0,0 +1,5 @@ +// Foo.kt + +class Foo { + +} diff --git a/idea/resources/intentionDescriptions/RenameFileToMatchClassIntention/before.kt.template b/idea/resources/intentionDescriptions/RenameFileToMatchClassIntention/before.kt.template new file mode 100644 index 00000000000..fb542ec314e --- /dev/null +++ b/idea/resources/intentionDescriptions/RenameFileToMatchClassIntention/before.kt.template @@ -0,0 +1,5 @@ +// bar.kt + +class Foo { + +} diff --git a/idea/resources/intentionDescriptions/RenameFileToMatchClassIntention/description.html b/idea/resources/intentionDescriptions/RenameFileToMatchClassIntention/description.html new file mode 100644 index 00000000000..36493980751 --- /dev/null +++ b/idea/resources/intentionDescriptions/RenameFileToMatchClassIntention/description.html @@ -0,0 +1,5 @@ + + +This intention renames file according to the top-level class name. + + \ No newline at end of file diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index bb808b74f7f..a3830505d06 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -1479,6 +1479,11 @@ Kotlin + + org.jetbrains.kotlin.idea.intentions.RenameFileToMatchClassIntention + Kotlin + + (KtClassOrObject::class.java, "", "Rename file to match top-level class name") { + override fun applicabilityRange(element: KtClassOrObject): TextRange? { + if (!element.isTopLevel()) return null + val fileName = element.containingKtFile.name + if (FileUtil.getNameWithoutExtension(fileName) == element.name) return null + text = "Rename file to ${element.name}.${FileUtilRt.getExtension(fileName)}" + return element.nameIdentifier?.textRange + } + + override fun startInWriteAction() = false + + override fun applyTo(element: KtClassOrObject, editor: Editor?) { + val file = element.containingKtFile + val extension = FileUtilRt.getExtension(file.name) + RenameProcessor( + file.project, + file, + "${element.name}.$extension", + RefactoringSettings.getInstance().RENAME_SEARCH_IN_COMMENTS_FOR_FILE, + RefactoringSettings.getInstance().RENAME_SEARCH_FOR_TEXT_FOR_FILE).run() + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/implement/annotation.kt b/idea/testData/quickfix/implement/annotation.kt index 46f1cfbcfc0..33abe32400e 100644 --- a/idea/testData/quickfix/implement/annotation.kt +++ b/idea/testData/quickfix/implement/annotation.kt @@ -1,4 +1,5 @@ // "Create subclass" "false" +// ACTION: Rename file to My.kt annotation class My(val x: Int) diff --git a/idea/testData/quickfix/implement/enum.kt b/idea/testData/quickfix/implement/enum.kt index 63fa37df843..58aed637ba4 100644 --- a/idea/testData/quickfix/implement/enum.kt +++ b/idea/testData/quickfix/implement/enum.kt @@ -1,6 +1,7 @@ // "Create subclass" "false" // ACTION: Create test // ACTION: Convert to sealed class +// ACTION: Rename file to My.kt enum class My { SINGLE { diff --git a/idea/testData/quickfix/implement/finalClass.kt b/idea/testData/quickfix/implement/finalClass.kt index b4d91eb74a6..e85a7cdbf9c 100644 --- a/idea/testData/quickfix/implement/finalClass.kt +++ b/idea/testData/quickfix/implement/finalClass.kt @@ -1,4 +1,5 @@ // "Create subclass" "false" // ACTION: Create test +// ACTION: Rename file to Base.kt class Base \ No newline at end of file diff --git a/idea/testData/quickfix/modifiers/noAbstractForObject.kt b/idea/testData/quickfix/modifiers/noAbstractForObject.kt index 373f5ced522..91e215f10ae 100644 --- a/idea/testData/quickfix/modifiers/noAbstractForObject.kt +++ b/idea/testData/quickfix/modifiers/noAbstractForObject.kt @@ -2,6 +2,7 @@ // ACTION: Create test // ACTION: Implement members // ACTION: Move 'Some' to separate file +// ACTION: Rename file to Some.kt // ERROR: Object 'Some' must be declared abstract or implement abstract member public abstract fun foo(): Unit defined in T interface T { fun foo() diff --git a/idea/testData/quickfix/removeUnused/importEnumValues.kt b/idea/testData/quickfix/removeUnused/importEnumValues.kt index ce4451dbe73..3348c4f6dae 100644 --- a/idea/testData/quickfix/removeUnused/importEnumValues.kt +++ b/idea/testData/quickfix/removeUnused/importEnumValues.kt @@ -1,6 +1,7 @@ // "Safe delete 'MyEnum'" "false" // ACTION: Create test // ACTION: Convert to sealed class +// ACTION: Rename file to MyEnum.kt import MyEnum.values diff --git a/idea/testData/quickfix/removeUnused/importObjectFun.kt b/idea/testData/quickfix/removeUnused/importObjectFun.kt index ca9d6ea576e..72e41b9a11f 100644 --- a/idea/testData/quickfix/removeUnused/importObjectFun.kt +++ b/idea/testData/quickfix/removeUnused/importObjectFun.kt @@ -1,5 +1,6 @@ // "Safe delete 'MyObj'" "false" // ACTION: Create test +// ACTION: Rename file to MyObj.kt import MyObj.foo diff --git a/idea/testData/quickfix/removeUnused/usedClassAsAlias.kt b/idea/testData/quickfix/removeUnused/usedClassAsAlias.kt index 8edabb624e2..6f602a11bb3 100644 --- a/idea/testData/quickfix/removeUnused/usedClassAsAlias.kt +++ b/idea/testData/quickfix/removeUnused/usedClassAsAlias.kt @@ -1,6 +1,7 @@ // "Safe delete 'Imported'" "false" // ACTION: Create test // ACTION: Move 'ImportedClass' to separate file +// ACTION: Rename file to ImportedClass.kt import ImportedClass as ClassAlias class ImportedClass diff --git a/idea/testData/quickfix/removeUnused/usedObjectAsAlias.kt b/idea/testData/quickfix/removeUnused/usedObjectAsAlias.kt index 88d13dbd3f5..14f43656234 100644 --- a/idea/testData/quickfix/removeUnused/usedObjectAsAlias.kt +++ b/idea/testData/quickfix/removeUnused/usedObjectAsAlias.kt @@ -1,6 +1,7 @@ // "Safe delete 'Imported'" "false" // ACTION: Create test // ACTION: Move 'Imported' to separate file +// ACTION: Rename file to Imported.kt import Imported as Alias object Imported diff --git a/idea/testData/quickfix/removeUnused/usedObjectAsAliasMulti.before.Main.kt b/idea/testData/quickfix/removeUnused/usedObjectAsAliasMulti.before.Main.kt index 506b2eb6698..652b97f3944 100644 --- a/idea/testData/quickfix/removeUnused/usedObjectAsAliasMulti.before.Main.kt +++ b/idea/testData/quickfix/removeUnused/usedObjectAsAliasMulti.before.Main.kt @@ -1,3 +1,4 @@ // "Safe delete 'Imported'" "false" // ACTION: Create test +// ACTION: Rename file to Imported.kt object Imported \ No newline at end of file diff --git a/idea/testData/quickfix/unusedSuppressAnnotation/notForJava.kt b/idea/testData/quickfix/unusedSuppressAnnotation/notForJava.kt index 543b33adf53..07e49b7298d 100644 --- a/idea/testData/quickfix/unusedSuppressAnnotation/notForJava.kt +++ b/idea/testData/quickfix/unusedSuppressAnnotation/notForJava.kt @@ -1,5 +1,6 @@ // "Suppress for declarations annotated by 'java.lang.SuppressWarnings'" "false" // ACTION: Safe delete 'Unused' // ACTION: Create test +// ACTION: Rename file to Unused.kt @java.lang.SuppressWarnings("") class Unused