Intentions: Implement "Convert enum to sealed class" intention

#KT-14245 In Progress
This commit is contained in:
Alexey Sedunov
2016-10-10 19:33:56 +03:00
parent 02e8d58acd
commit 2187a77646
18 changed files with 252 additions and 0 deletions
+1
View File
@@ -225,6 +225,7 @@ These artifacts include extensions for the types available in the latter JDKs, s
- [`KT-11525`](https://youtrack.jetbrains.com/issue/KT-11525) Implement "Create type parameter" quickfix
- [`KT-9931`](https://youtrack.jetbrains.com/issue/KT-9931) Implement "Remove unused assignment" quickfix
- [`KT-14245`](https://youtrack.jetbrains.com/issue/KT-14245) Implement "Convert enum to sealed class" intention
#### Refactorings
@@ -0,0 +1,9 @@
sealed class MyEnum(val s: String = "") {
fun foo() {
}
object FOO : MyEnum("FOO")
object BAR : MyEnum("BAR")
object DEFAULT : MyEnum()
}
@@ -0,0 +1,7 @@
enum class MyEnum(val s: String = "") {
FOO("FOO"), BAR("BAR"), DEFAULT();
fun foo() {
}
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention converts an enum class to a sealed class hierarchy with enum entries turned into objects
</body>
</html>
+5
View File
@@ -1396,6 +1396,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertEnumToSealedClassIntention</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,76 @@
/*
* 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.psi.PsiWhiteSpace
import com.intellij.psi.codeStyle.CodeStyleManager
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.allChildren
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.siblings
import org.jetbrains.kotlin.psi.psiUtil.startOffset
class ConvertEnumToSealedClassIntention : SelfTargetingRangeIntention<KtClass>(KtClass::class.java, "Convert to sealed class") {
override fun applicabilityRange(element: KtClass): TextRange? {
val nameIdentifier = element.nameIdentifier ?: return null
val enumKeyword = element.modifierList?.getModifier(KtTokens.ENUM_KEYWORD) ?: return null
return TextRange(enumKeyword.startOffset, nameIdentifier.endOffset)
}
override fun applyTo(element: KtClass, editor: Editor?) {
element.removeModifier(KtTokens.ENUM_KEYWORD)
element.addModifier(KtTokens.SEALED_KEYWORD)
val psiFactory = KtPsiFactory(element)
for (member in element.declarations) {
if (member !is KtEnumEntry) continue
val obj = psiFactory.createDeclaration<KtObjectDeclaration>("object ${member.name}")
val initializers = member.initializerList?.initializers ?: emptyList()
if (initializers.isNotEmpty()) {
initializers.forEach { obj.addSuperTypeListEntry(psiFactory.createSuperTypeCallEntry("${element.name}${it.text}")) }
}
else {
obj.addSuperTypeListEntry(psiFactory.createSuperTypeCallEntry("${element.name}()"))
}
member.getBody()?.let { body -> obj.add(body) }
member.delete()
element.addDeclaration(obj)
}
element.getBody()?.let { body ->
val semicolon = body
.allChildren
.takeWhile { it !is KtDeclaration }
.firstOrNull { it.node.elementType == KtTokens.SEMICOLON }
if (semicolon != null) {
val nonWhiteSibling = semicolon.siblings(forward = true, withItself = false).firstOrNull { it !is PsiWhiteSpace }
body.deleteChildRange(semicolon, nonWhiteSibling?.prevSibling ?: semicolon)
if (nonWhiteSibling != null) {
CodeStyleManager.getInstance(element.project).reformat(nonWhiteSibling.firstChild ?: nonWhiteSibling)
}
}
}
}
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.ConvertEnumToSealedClassIntention
@@ -0,0 +1,21 @@
// WITH_RUNTIME
enum class <caret>MyEnum(val s: String = "") {
FOO("FOO"), BAR("BAR"), DEFAULT();
fun foo() {
}
}
fun test(e: MyEnum) {
if (e == MyEnum.BAR) {
println()
}
val n = when (e) {
MyEnum.BAR -> 1
MyEnum.FOO -> 2
MyEnum.DEFAULT -> 0
}
}
@@ -0,0 +1,24 @@
// WITH_RUNTIME
sealed class <caret>MyEnum(val s: String = "") {
fun foo() {
}
object FOO : MyEnum("FOO")
object BAR : MyEnum("BAR")
object DEFAULT : MyEnum()
}
fun test(e: MyEnum) {
if (e == MyEnum.BAR) {
println()
}
val n = when (e) {
MyEnum.BAR -> 1
MyEnum.FOO -> 2
MyEnum.DEFAULT -> 0
}
}
@@ -0,0 +1,17 @@
// WITH_RUNTIME
enum class <caret>MyEnum(val s: String = "") {
FOO("FOO"), BAR("BAR"), DEFAULT()
}
fun test(e: MyEnum) {
if (e == MyEnum.BAR) {
println()
}
val n = when (e) {
MyEnum.BAR -> 1
MyEnum.FOO -> 2
MyEnum.DEFAULT -> 0
}
}
@@ -0,0 +1,19 @@
// WITH_RUNTIME
sealed class <caret>MyEnum(val s: String = "") {
object FOO : MyEnum("FOO")
object BAR : MyEnum("BAR")
object DEFAULT : MyEnum()
}
fun test(e: MyEnum) {
if (e == MyEnum.BAR) {
println()
}
val n = when (e) {
MyEnum.BAR -> 1
MyEnum.FOO -> 2
MyEnum.DEFAULT -> 0
}
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
enum class <caret>MyEnum(val s: String = "") {
;
fun foo() {
}
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
sealed class <caret>MyEnum(val s: String = "") {
fun foo() {
}
}
@@ -0,0 +1,5 @@
// IS_APPLICABLE: false
class <caret>A {
}
@@ -0,0 +1,5 @@
// IS_APPLICABLE: false
<caret>internal enum class MyEnum {
A, B, C
}
+1
View File
@@ -1,5 +1,6 @@
// "Create subclass" "false"
// ACTION: Create test
// ACTION: Convert to sealed class
enum class <caret>My {
SINGLE {
@@ -1,5 +1,6 @@
// "Safe delete 'MyEnum'" "false"
// ACTION: Create test
// ACTION: Convert to sealed class
import MyEnum.values
@@ -3569,6 +3569,45 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/convertEnumToSealedClass")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ConvertEnumToSealedClass extends AbstractIntentionTest {
public void testAllFilesPresentInConvertEnumToSealedClass() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/convertEnumToSealedClass"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("entriesAndMembers.kt")
public void testEntriesAndMembers() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertEnumToSealedClass/entriesAndMembers.kt");
doTest(fileName);
}
@TestMetadata("entriesOnly.kt")
public void testEntriesOnly() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertEnumToSealedClass/entriesOnly.kt");
doTest(fileName);
}
@TestMetadata("membersOnly.kt")
public void testMembersOnly() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertEnumToSealedClass/membersOnly.kt");
doTest(fileName);
}
@TestMetadata("notEnum.kt")
public void testNotEnum() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertEnumToSealedClass/notEnum.kt");
doTest(fileName);
}
@TestMetadata("outOfRange.kt")
public void testOutOfRange() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertEnumToSealedClass/outOfRange.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/convertForEachToForLoop")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)