Fixed a quick fix for enum entry short super constructor syntax (situation with a preceding comment / annotation). Four extra tests. isAvailable().

This commit is contained in:
Mikhail Glukhikh
2015-05-18 15:41:08 +03:00
parent 62e9c31987
commit 6342b98592
10 changed files with 135 additions and 2 deletions
@@ -19,7 +19,11 @@ package org.jetbrains.kotlin.idea.quickfix
import com.intellij.codeInsight.intention.IntentionAction import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project import com.intellij.openapi.project.Project
import com.intellij.psi.PsiComment
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiWhiteSpace
import org.jetbrains.annotations.NotNull
import org.jetbrains.kotlin.JetNodeTypes import org.jetbrains.kotlin.JetNodeTypes
import org.jetbrains.kotlin.diagnostics.Diagnostic import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.idea.quickfix.quickfixUtil.createIntentionFactory import org.jetbrains.kotlin.idea.quickfix.quickfixUtil.createIntentionFactory
@@ -28,6 +32,7 @@ import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi import org.jetbrains.kotlin.psi
import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
import org.jetbrains.kotlin.psi.psiUtil.getNextSiblingIgnoringWhitespaceAndComments
import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes
import org.jetbrains.kotlin.resolve.DeclarationsChecker import org.jetbrains.kotlin.resolve.DeclarationsChecker
@@ -38,6 +43,9 @@ class DeprecatedEnumEntrySuperConstructorSyntaxFix(element: JetEnumEntry): JetIn
override fun invoke(project: Project, editor: Editor?, file: JetFile?) = changeConstructorToShort(element) override fun invoke(project: Project, editor: Editor?, file: JetFile?) = changeConstructorToShort(element)
override fun isAvailable(project: Project, editor: Editor?, file: PsiFile): Boolean
= super.isAvailable(project, editor, file) && DeclarationsChecker.enumEntryUsesDeprecatedSuperConstructor(element)
companion object: JetSingleIntentionActionFactory() { companion object: JetSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): IntentionAction? = override fun createAction(diagnostic: Diagnostic): IntentionAction? =
diagnostic.createIntentionForFirstParentOfType(::DeprecatedEnumEntrySuperConstructorSyntaxFix) diagnostic.createIntentionForFirstParentOfType(::DeprecatedEnumEntrySuperConstructorSyntaxFix)
@@ -61,8 +69,9 @@ class DeprecatedEnumEntrySuperConstructorSyntaxFix(element: JetEnumEntry): JetIn
private fun changeConstructorToShort(entry: JetEnumEntry) { private fun changeConstructorToShort(entry: JetEnumEntry) {
val list = entry.getInitializerList()!! val list = entry.getInitializerList()!!
transformInitializerList(list) transformInitializerList(list)
// Delete everything between name and initializer (colon with whitespaces) // Delete everything between name identifier and initializer (colon with whitespaces)
entry.deleteChildRange(entry.getFirstChild().getNextSibling(), list.getPrevSibling()) val name = entry.getNameAsDeclaration()
entry.deleteChildRange(name.getNextSibling()!!, list.getPrevSibling()!!)
} }
} }
} }
@@ -0,0 +1,11 @@
// "Change to short enum entry super constructor in the whole project" "true"
annotation class My
annotation class Your
annotation class His
enum class MyEnum(val i: Int) {
@My FIRST: MyEnum(1)<caret>,
@My @Your SECOND: MyEnum(2),
@Your @His THIRD: MyEnum(3)
}
@@ -0,0 +1,11 @@
// "Change to short enum entry super constructor in the whole project" "true"
annotation class My
annotation class Your
annotation class His
enum class MyEnum(val i: Int) {
@My FIRST(1),
@My @Your SECOND(2),
@Your @His THIRD(3)
}
@@ -0,0 +1,10 @@
// "Change to short enum entry super constructor in the whole project" "true"
enum class MyEnum(val i: Int) {
// The first
FIRST: MyEnum(1)<caret>,
// The second
SECOND: MyEnum(2),
// The third
THIRD: MyEnum(3)
}
@@ -0,0 +1,10 @@
// "Change to short enum entry super constructor in the whole project" "true"
enum class MyEnum(val i: Int) {
// The first
FIRST(1),
// The second
SECOND(2),
// The third
THIRD(3)
}
@@ -0,0 +1,16 @@
// "Change to short enum entry super constructor in the whole project" "true"
enum class MyEnum(val i: Int) {
/**
* The first
*/
FIRST: MyEnum(1),
/**
* The second
*/
SECOND: MyEnum(2)<caret>,
/**
* The third
*/
THIRD: MyEnum(3)
}
@@ -0,0 +1,16 @@
// "Change to short enum entry super constructor in the whole project" "true"
enum class MyEnum(val i: Int) {
/**
* The first
*/
FIRST(1),
/**
* The second
*/
SECOND(2),
/**
* The third
*/
THIRD(3)
}
@@ -0,0 +1,13 @@
// "Change to short enum entry super constructor in the whole project" "true"
enum class MyEnum(val i: Int) {
// The
// first
FIRST: MyEnum(1),
// The
// second
SECOND: MyEnum(2),
// The
// third
THIRD: MyEnum(3)<caret>
}
@@ -0,0 +1,13 @@
// "Change to short enum entry super constructor in the whole project" "true"
enum class MyEnum(val i: Int) {
// The
// first
FIRST(1),
// The
// second
SECOND(2),
// The
// third
THIRD(3)
}
@@ -3051,6 +3051,30 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName); doTest(fileName);
} }
@TestMetadata("precedingAnnotation.kt")
public void testPrecedingAnnotation() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/enumConstructor/precedingAnnotation.kt");
doTest(fileName);
}
@TestMetadata("precedingComment.kt")
public void testPrecedingComment() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/enumConstructor/precedingComment.kt");
doTest(fileName);
}
@TestMetadata("precedingDocComment.kt")
public void testPrecedingDocComment() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/enumConstructor/precedingDocComment.kt");
doTest(fileName);
}
@TestMetadata("precedingMultilineComment.kt")
public void testPrecedingMultilineComment() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/enumConstructor/precedingMultilineComment.kt");
doTest(fileName);
}
@TestMetadata("singleEntry.kt") @TestMetadata("singleEntry.kt")
public void testSingleEntry() throws Exception { public void testSingleEntry() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/enumConstructor/singleEntry.kt"); String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/enumConstructor/singleEntry.kt");