diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/DeprecatedEnumEntrySuperConstructorSyntaxFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/DeprecatedEnumEntrySuperConstructorSyntaxFix.kt index ed6540c833b..c906db972c1 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/DeprecatedEnumEntrySuperConstructorSyntaxFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/DeprecatedEnumEntrySuperConstructorSyntaxFix.kt @@ -19,7 +19,11 @@ package org.jetbrains.kotlin.idea.quickfix import com.intellij.codeInsight.intention.IntentionAction import com.intellij.openapi.editor.Editor import com.intellij.openapi.project.Project +import com.intellij.psi.PsiComment 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.diagnostics.Diagnostic 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.psiUtil.getChildOfType +import org.jetbrains.kotlin.psi.psiUtil.getNextSiblingIgnoringWhitespaceAndComments import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes 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 isAvailable(project: Project, editor: Editor?, file: PsiFile): Boolean + = super.isAvailable(project, editor, file) && DeclarationsChecker.enumEntryUsesDeprecatedSuperConstructor(element) + companion object: JetSingleIntentionActionFactory() { override fun createAction(diagnostic: Diagnostic): IntentionAction? = diagnostic.createIntentionForFirstParentOfType(::DeprecatedEnumEntrySuperConstructorSyntaxFix) @@ -61,8 +69,9 @@ class DeprecatedEnumEntrySuperConstructorSyntaxFix(element: JetEnumEntry): JetIn private fun changeConstructorToShort(entry: JetEnumEntry) { val list = entry.getInitializerList()!! transformInitializerList(list) - // Delete everything between name and initializer (colon with whitespaces) - entry.deleteChildRange(entry.getFirstChild().getNextSibling(), list.getPrevSibling()) + // Delete everything between name identifier and initializer (colon with whitespaces) + val name = entry.getNameAsDeclaration() + entry.deleteChildRange(name.getNextSibling()!!, list.getPrevSibling()!!) } } } \ No newline at end of file diff --git a/idea/testData/quickfix/migration/enumConstructor/precedingAnnotation.kt b/idea/testData/quickfix/migration/enumConstructor/precedingAnnotation.kt new file mode 100644 index 00000000000..f819cecf540 --- /dev/null +++ b/idea/testData/quickfix/migration/enumConstructor/precedingAnnotation.kt @@ -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), + @My @Your SECOND: MyEnum(2), + @Your @His THIRD: MyEnum(3) +} \ No newline at end of file diff --git a/idea/testData/quickfix/migration/enumConstructor/precedingAnnotation.kt.after b/idea/testData/quickfix/migration/enumConstructor/precedingAnnotation.kt.after new file mode 100644 index 00000000000..38a0d3c5e09 --- /dev/null +++ b/idea/testData/quickfix/migration/enumConstructor/precedingAnnotation.kt.after @@ -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) +} \ No newline at end of file diff --git a/idea/testData/quickfix/migration/enumConstructor/precedingComment.kt b/idea/testData/quickfix/migration/enumConstructor/precedingComment.kt new file mode 100644 index 00000000000..6a4008170d3 --- /dev/null +++ b/idea/testData/quickfix/migration/enumConstructor/precedingComment.kt @@ -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), + // The second + SECOND: MyEnum(2), + // The third + THIRD: MyEnum(3) +} \ No newline at end of file diff --git a/idea/testData/quickfix/migration/enumConstructor/precedingComment.kt.after b/idea/testData/quickfix/migration/enumConstructor/precedingComment.kt.after new file mode 100644 index 00000000000..53995112965 --- /dev/null +++ b/idea/testData/quickfix/migration/enumConstructor/precedingComment.kt.after @@ -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) +} \ No newline at end of file diff --git a/idea/testData/quickfix/migration/enumConstructor/precedingDocComment.kt b/idea/testData/quickfix/migration/enumConstructor/precedingDocComment.kt new file mode 100644 index 00000000000..9ab019aac42 --- /dev/null +++ b/idea/testData/quickfix/migration/enumConstructor/precedingDocComment.kt @@ -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), + /** + * The third + */ + THIRD: MyEnum(3) +} \ No newline at end of file diff --git a/idea/testData/quickfix/migration/enumConstructor/precedingDocComment.kt.after b/idea/testData/quickfix/migration/enumConstructor/precedingDocComment.kt.after new file mode 100644 index 00000000000..6c42d247267 --- /dev/null +++ b/idea/testData/quickfix/migration/enumConstructor/precedingDocComment.kt.after @@ -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) +} \ No newline at end of file diff --git a/idea/testData/quickfix/migration/enumConstructor/precedingMultilineComment.kt b/idea/testData/quickfix/migration/enumConstructor/precedingMultilineComment.kt new file mode 100644 index 00000000000..6ab0c0492f4 --- /dev/null +++ b/idea/testData/quickfix/migration/enumConstructor/precedingMultilineComment.kt @@ -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) +} \ No newline at end of file diff --git a/idea/testData/quickfix/migration/enumConstructor/precedingMultilineComment.kt.after b/idea/testData/quickfix/migration/enumConstructor/precedingMultilineComment.kt.after new file mode 100644 index 00000000000..1f33aa8a525 --- /dev/null +++ b/idea/testData/quickfix/migration/enumConstructor/precedingMultilineComment.kt.after @@ -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) +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index 04748de3e34..12cd742a65e 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -3051,6 +3051,30 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { 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") public void testSingleEntry() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/enumConstructor/singleEntry.kt");