Intentions: Implement intention which rename file according to the top-level class name

#KT-15068 Fixed
This commit is contained in:
Alexey Sedunov
2016-12-21 13:41:33 +03:00
parent c7aefa2842
commit 6b57a3e338
16 changed files with 88 additions and 0 deletions
+10
View File
@@ -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
@@ -0,0 +1,5 @@
// Foo.kt
class <spot>Foo</spot> {
}
@@ -0,0 +1,5 @@
// bar.kt
class <spot>Foo</spot> {
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention renames file according to the top-level class name.
</body>
</html>
+5
View File
@@ -1479,6 +1479,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.RenameFileToMatchClassIntention</className>
<category>Kotlin</category>
</intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
displayName="Object literal can be converted to lambda"
groupName="Kotlin"
@@ -0,0 +1,48 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.util.TextRange
import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.util.io.FileUtilRt
import com.intellij.refactoring.RefactoringSettings
import com.intellij.refactoring.rename.RenameProcessor
import org.jetbrains.kotlin.psi.KtClassOrObject
class RenameFileToMatchClassIntention : SelfTargetingRangeIntention<KtClassOrObject>(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()
}
}
+1
View File
@@ -1,4 +1,5 @@
// "Create subclass" "false"
// ACTION: Rename file to My.kt
annotation class <caret>My(val x: Int)
+1
View File
@@ -1,6 +1,7 @@
// "Create subclass" "false"
// ACTION: Create test
// ACTION: Convert to sealed class
// ACTION: Rename file to My.kt
enum class <caret>My {
SINGLE {
+1
View File
@@ -1,4 +1,5 @@
// "Create subclass" "false"
// ACTION: Create test
// ACTION: Rename file to Base.kt
class <caret>Base
@@ -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()
@@ -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
@@ -1,5 +1,6 @@
// "Safe delete 'MyObj'" "false"
// ACTION: Create test
// ACTION: Rename file to MyObj.kt
import MyObj.foo
@@ -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 <caret>ImportedClass
@@ -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 <caret>Imported
@@ -1,3 +1,4 @@
// "Safe delete 'Imported'" "false"
// ACTION: Create test
// ACTION: Rename file to Imported.kt
object <caret>Imported
@@ -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 <caret>Unused