Add "Rename class to containing file name" intention
#KT-25262 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
39016594b1
commit
e340af51df
@@ -1732,6 +1732,11 @@
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.kotlin.idea.intentions.RenameClassToContainingFileNameIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<lang.inspectionSuppressor language="kotlin" implementationClass="org.jetbrains.kotlin.idea.inspections.KotlinInspectionSuppressor"/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
|
||||
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
// Bar.kt
|
||||
|
||||
class <spot>Bar</spot> {
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
// Bar.kt
|
||||
|
||||
class <spot>Foo</spot> {
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention renames the class to containing file name.
|
||||
</body>
|
||||
</html>
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
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.refactoring.RefactoringSettings
|
||||
import com.intellij.refactoring.rename.RenameProcessor
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.psi.KtClassOrObject
|
||||
import org.jetbrains.kotlin.renderer.render
|
||||
|
||||
class RenameClassToContainingFileNameIntention : SelfTargetingRangeIntention<KtClassOrObject>(
|
||||
KtClassOrObject::class.java, "Rename class to containing file name"
|
||||
) {
|
||||
override fun startInWriteAction() = false
|
||||
|
||||
override fun applicabilityRange(element: KtClassOrObject): TextRange? {
|
||||
if (!element.isTopLevel()) return null
|
||||
val fileName = FileUtil.getNameWithoutExtension(element.containingKtFile.name)
|
||||
if (fileName == element.name
|
||||
|| fileName[0].isLowerCase()
|
||||
|| !Name.isValidIdentifier(fileName)
|
||||
|| Name.identifier(fileName).render() != fileName
|
||||
|| element.containingKtFile.declarations.any { it is KtClassOrObject && it.name == fileName }
|
||||
) return null
|
||||
text = "Rename class to $fileName"
|
||||
return element.nameIdentifier?.textRange
|
||||
}
|
||||
|
||||
override fun applyTo(element: KtClassOrObject, editor: Editor?) {
|
||||
val file = element.containingKtFile
|
||||
val fileName = FileUtil.getNameWithoutExtension(element.containingKtFile.name)
|
||||
RenameProcessor(file.project, element, fileName, false, false).run()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.intentions.RenameClassToContainingFileNameIntention
|
||||
@@ -0,0 +1 @@
|
||||
class Foo<caret>
|
||||
@@ -0,0 +1 @@
|
||||
class Basic
|
||||
@@ -0,0 +1 @@
|
||||
object Foo<caret>
|
||||
@@ -0,0 +1 @@
|
||||
object Basic2
|
||||
@@ -0,0 +1,4 @@
|
||||
// IS_APPLICABLE: false
|
||||
class Foo<caret>
|
||||
|
||||
class HasSameNameClass
|
||||
@@ -0,0 +1,4 @@
|
||||
// IS_APPLICABLE: false
|
||||
class Foo<caret>
|
||||
|
||||
object HasSameNameObject
|
||||
@@ -0,0 +1,2 @@
|
||||
// IS_APPLICABLE: false
|
||||
class SameName<caret>
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
// IS_APPLICABLE: false
|
||||
class Foo<caret>
|
||||
@@ -14837,6 +14837,49 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/renameClassToContainingFileName")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class RenameClassToContainingFileName extends AbstractIntentionTest {
|
||||
private void runTest(String testDataFilePath) throws Exception {
|
||||
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInRenameClassToContainingFileName() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/renameClassToContainingFileName"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("Basic.kt")
|
||||
public void testBasic() throws Exception {
|
||||
runTest("idea/testData/intentions/renameClassToContainingFileName/Basic.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("Basic2.kt")
|
||||
public void testBasic2() throws Exception {
|
||||
runTest("idea/testData/intentions/renameClassToContainingFileName/Basic2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("HasSameNameClass.kt")
|
||||
public void testHasSameNameClass() throws Exception {
|
||||
runTest("idea/testData/intentions/renameClassToContainingFileName/HasSameNameClass.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("HasSameNameObject.kt")
|
||||
public void testHasSameNameObject() throws Exception {
|
||||
runTest("idea/testData/intentions/renameClassToContainingFileName/HasSameNameObject.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("SameName.kt")
|
||||
public void testSameName() throws Exception {
|
||||
runTest("idea/testData/intentions/renameClassToContainingFileName/SameName.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("startWithLowerCase.kt")
|
||||
public void testStartWithLowerCase() throws Exception {
|
||||
runTest("idea/testData/intentions/renameClassToContainingFileName/startWithLowerCase.kt");
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/replaceAddWithPlusAssign")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user