From 52e9117e9d85a1477c91de1ed78b6244b185fcfe Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Wed, 11 Jan 2017 12:25:28 +0300 Subject: [PATCH] Extract Superclass/Interface: Allow extracting class with special name (and quotes) #KT-15353 Fixed --- ChangeLog.md | 1 + .../extractClass/ExtractSuperRefactoring.kt | 7 ++----- .../kotlin/idea/refactoring/pullUp/markingUtils.kt | 3 ++- .../refactoring/extractInterface/specialName.kt | 8 ++++++++ .../extractInterface/specialName.kt.after | 11 +++++++++++ .../refactoring/extractSuperclass/specialName.kt | 8 ++++++++ .../extractSuperclass/specialName.kt.after | 11 +++++++++++ .../introduce/ExtractionTestGenerated.java | 12 ++++++++++++ 8 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 idea/testData/refactoring/extractInterface/specialName.kt create mode 100644 idea/testData/refactoring/extractInterface/specialName.kt.after create mode 100644 idea/testData/refactoring/extractSuperclass/specialName.kt create mode 100644 idea/testData/refactoring/extractSuperclass/specialName.kt.after diff --git a/ChangeLog.md b/ChangeLog.md index cfdcc32a5a5..5e536b4e1db 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -476,6 +476,7 @@ These artifacts include extensions for the types available in the latter JDKs, s - [`KT-12704`](https://youtrack.jetbrains.com/issue/KT-12704), [`KT-15583`](https://youtrack.jetbrains.com/issue/KT-15583) Override/Implement Members: Support all nullability annotations respected by the Kotlin compiler - [`KT-15563`](https://youtrack.jetbrains.com/issue/KT-15563) Override Members: Allow overriding virtual synthetic members (e.g. equals(), hashCode(), toString(), etc.) in data classes - [`KT-15355`](https://youtrack.jetbrains.com/issue/KT-15355) Extract Interface: Disable "Make abstract" and assume it to be true for abstract members of an interface +- [`KT-15353`](https://youtrack.jetbrains.com/issue/KT-15353) Extract Superclass/Interface: Allow extracting class with special name (and quotes) #### Intention actions, inspections and quickfixes diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractClass/ExtractSuperRefactoring.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractClass/ExtractSuperRefactoring.kt index 90e93ecb096..132ed7db032 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractClass/ExtractSuperRefactoring.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractClass/ExtractSuperRefactoring.kt @@ -37,10 +37,7 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde import org.jetbrains.kotlin.idea.codeInsight.shorten.performDelayedShortening -import org.jetbrains.kotlin.idea.core.ShortenReferences -import org.jetbrains.kotlin.idea.core.copied -import org.jetbrains.kotlin.idea.core.getPackage -import org.jetbrains.kotlin.idea.core.replaced +import org.jetbrains.kotlin.idea.core.* import org.jetbrains.kotlin.idea.refactoring.introduce.insertDeclaration import org.jetbrains.kotlin.idea.refactoring.memberInfo.KotlinMemberInfo import org.jetbrains.kotlin.idea.refactoring.memberInfo.getChildrenToAnalyze @@ -206,7 +203,7 @@ class ExtractSuperRefactoring( private fun createClass(superClassEntry: KtSuperTypeListEntry?): KtClass { val targetParent = extractInfo.targetParent - val newClassName = extractInfo.newClassName + val newClassName = extractInfo.newClassName.quoteIfNeeded() val originalClass = extractInfo.originalClass val kind = if (extractInfo.isInterface) "interface" else "class" diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/pullUp/markingUtils.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/pullUp/markingUtils.kt index 130a346055e..38f3d8da606 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/pullUp/markingUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/pullUp/markingUtils.kt @@ -25,6 +25,7 @@ import org.jetbrains.kotlin.idea.imports.importableFqName import org.jetbrains.kotlin.idea.references.mainReference import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers import org.jetbrains.kotlin.idea.core.ShortenReferences +import org.jetbrains.kotlin.idea.core.quoteIfNeeded import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.allChildren @@ -113,7 +114,7 @@ fun applyMarking( substitutor: TypeSubstitutor, targetClassDescriptor: ClassDescriptor ) { val psiFactory = KtPsiFactory(declaration) - val targetThis = psiFactory.createExpression("this@${targetClassDescriptor.name.asString()}") + val targetThis = psiFactory.createExpression("this@${targetClassDescriptor.name.asString().quoteIfNeeded()}") val shorteningOptionsForThis = ShortenReferences.Options(removeThisLabels = true, removeThis = true) declaration.accept( diff --git a/idea/testData/refactoring/extractInterface/specialName.kt b/idea/testData/refactoring/extractInterface/specialName.kt new file mode 100644 index 00000000000..5e12df33bec --- /dev/null +++ b/idea/testData/refactoring/extractInterface/specialName.kt @@ -0,0 +1,8 @@ +// NAME: class +// SIBLING: +class A { + // INFO: {checked: "true"} + fun foo() { + + } +} \ No newline at end of file diff --git a/idea/testData/refactoring/extractInterface/specialName.kt.after b/idea/testData/refactoring/extractInterface/specialName.kt.after new file mode 100644 index 00000000000..8b6051a817f --- /dev/null +++ b/idea/testData/refactoring/extractInterface/specialName.kt.after @@ -0,0 +1,11 @@ +interface `class` { + // INFO: {checked: "true"} + fun foo() { + + } +} + +// NAME: class +// SIBLING: +class A : `class` { +} \ No newline at end of file diff --git a/idea/testData/refactoring/extractSuperclass/specialName.kt b/idea/testData/refactoring/extractSuperclass/specialName.kt new file mode 100644 index 00000000000..5e12df33bec --- /dev/null +++ b/idea/testData/refactoring/extractSuperclass/specialName.kt @@ -0,0 +1,8 @@ +// NAME: class +// SIBLING: +class A { + // INFO: {checked: "true"} + fun foo() { + + } +} \ No newline at end of file diff --git a/idea/testData/refactoring/extractSuperclass/specialName.kt.after b/idea/testData/refactoring/extractSuperclass/specialName.kt.after new file mode 100644 index 00000000000..fefb7ed5fa4 --- /dev/null +++ b/idea/testData/refactoring/extractSuperclass/specialName.kt.after @@ -0,0 +1,11 @@ +open class `class` { + // INFO: {checked: "true"} + fun foo() { + + } +} + +// NAME: class +// SIBLING: +class A : `class`() { +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/introduce/ExtractionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/refactoring/introduce/ExtractionTestGenerated.java index 3df91c85d25..0e2ffd7c1ac 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/introduce/ExtractionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/introduce/ExtractionTestGenerated.java @@ -4312,6 +4312,12 @@ public class ExtractionTestGenerated extends AbstractExtractionTest { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/extractSuperclass/replaceSuperclass.kt"); doExtractSuperclassTest(fileName); } + + @TestMetadata("specialName.kt") + public void testSpecialName() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/extractSuperclass/specialName.kt"); + doExtractSuperclassTest(fileName); + } } @TestMetadata("idea/testData/refactoring/extractInterface") @@ -4351,5 +4357,11 @@ public class ExtractionTestGenerated extends AbstractExtractionTest { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/extractInterface/annotation.kt"); doExtractInterfaceTest(fileName); } + + @TestMetadata("specialName.kt") + public void testSpecialName() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/extractInterface/specialName.kt"); + doExtractInterfaceTest(fileName); + } } }