"Convert to sealed class" intention: don't suggest when no "class" keyword
#KT-32365 Fixed
This commit is contained in:
committed by
Dmitry Gridin
parent
9786564d75
commit
1f89c0f730
@@ -52,6 +52,8 @@ open class KtClass : KtClassOrObject {
|
||||
override fun getCompanionObjects(): List<KtObjectDeclaration> = getBody()?.allCompanionObjects.orEmpty()
|
||||
|
||||
fun getClassOrInterfaceKeyword(): PsiElement? = findChildByType(TokenSet.create(KtTokens.CLASS_KEYWORD, KtTokens.INTERFACE_KEYWORD))
|
||||
|
||||
fun getClassKeyword(): PsiElement? = findChildByType(KtTokens.CLASS_KEYWORD)
|
||||
}
|
||||
|
||||
fun KtClass.createPrimaryConstructorIfAbsent(): KtPrimaryConstructor {
|
||||
|
||||
@@ -31,6 +31,7 @@ import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
|
||||
class ConvertEnumToSealedClassIntention : SelfTargetingRangeIntention<KtClass>(KtClass::class.java, "Convert to sealed class") {
|
||||
override fun applicabilityRange(element: KtClass): TextRange? {
|
||||
if (element.getClassKeyword() == null) return null
|
||||
val nameIdentifier = element.nameIdentifier ?: return null
|
||||
val enumKeyword = element.modifierList?.getModifier(KtTokens.ENUM_KEYWORD) ?: return null
|
||||
return TextRange(enumKeyword.startOffset, nameIdentifier.endOffset)
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
// IS_APPLICABLE: false
|
||||
enum<caret> Foo {
|
||||
A, B, C
|
||||
}
|
||||
@@ -4275,6 +4275,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
runTest("idea/testData/intentions/convertEnumToSealedClass/membersOnly.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("noClassKeyword.kt")
|
||||
public void testNoClassKeyword() throws Exception {
|
||||
runTest("idea/testData/intentions/convertEnumToSealedClass/noClassKeyword.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("notEnum.kt")
|
||||
public void testNotEnum() throws Exception {
|
||||
runTest("idea/testData/intentions/convertEnumToSealedClass/notEnum.kt");
|
||||
|
||||
Reference in New Issue
Block a user