Fixed a quick fix for enum entry delimiters: situation with following comments. Three new tests. isAvailable().

This commit is contained in:
Mikhail Glukhikh
2015-05-18 17:17:07 +03:00
parent 6342b98592
commit 3585c45f70
8 changed files with 75 additions and 2 deletions
@@ -19,8 +19,7 @@ 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.PsiElement
import com.intellij.psi.PsiErrorElement
import com.intellij.psi.*
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.idea.quickfix.quickfixUtil.createIntentionFactory
import org.jetbrains.kotlin.idea.quickfix.quickfixUtil.createIntentionForFirstParentOfType
@@ -41,6 +40,9 @@ class DeprecatedEnumEntryDelimiterSyntaxFix(element: JetEnumEntry): JetIntention
override fun invoke(project: Project, editor: Editor?, file: JetFile?) = insertLackingCommaSemicolon(element)
override fun isAvailable(project: Project, editor: Editor?, file: PsiFile): Boolean
= super.isAvailable(project, editor, file) && DeclarationsChecker.enumEntryUsesDeprecatedOrNoDelimiter(element)
companion object : JetSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): IntentionAction? =
diagnostic.createIntentionForFirstParentOfType(::DeprecatedEnumEntryDelimiterSyntaxFix)
@@ -62,6 +64,7 @@ class DeprecatedEnumEntryDelimiterSyntaxFix(element: JetEnumEntry): JetIntention
for ((entryIndex, entry) in entries.withIndex()) {
var next = entry.getNextSiblingIgnoringWhitespaceAndComments()
var nextType = next?.getNode()?.getElementType()
var added = false
if (entryIndex < entries.size() - 1) {
if (next is PsiErrorElement && next.getFirstChild()?.getNode()?.getElementType() == JetTokens.SEMICOLON) {
// Fix for syntax error like ENUM_ENTRY1; ENUM_ENTRY2; ENUM_ENTRY3
@@ -70,6 +73,7 @@ class DeprecatedEnumEntryDelimiterSyntaxFix(element: JetEnumEntry): JetIntention
else if (nextType != JetTokens.COMMA) {
// Classic case like ENUM_ENTRY1 ENUM_ENTRY2
body.addAfter(psiFactory.createComma(), entry)
added = true
}
}
else {
@@ -80,6 +84,21 @@ class DeprecatedEnumEntryDelimiterSyntaxFix(element: JetEnumEntry): JetIntention
else if (nextType != JetTokens.SEMICOLON && nextType != JetTokens.RBRACE) {
// ENUM_ENTRY_LAST fun foo()
body.addAfter(psiFactory.createSemicolon(), entry)
added = true
}
}
// Specific situation: ENTRY // comment ==> ENTRY, // comment, not ENTRY // comment,
if (added) {
val last = entry.getLastChild()
var curr = last
while (curr is PsiComment || curr is PsiWhiteSpace) {
// After comma / semicolon
val prev = curr.getPrevSibling()
body.addAfter(curr, entry.getNextSibling())
curr = prev
}
if (curr !== last) {
entry.deleteChildRange(curr.getNextSibling()!!, last)
}
}
}
@@ -0,0 +1,6 @@
// "Insert lacking comma(s) / semicolon(s)" "true"
enum class MyEnum {
FIRST<caret> /* The first one */
SECOND
}
@@ -0,0 +1,6 @@
// "Insert lacking comma(s) / semicolon(s)" "true"
enum class MyEnum {
FIRST, /* The first one */
SECOND
}
@@ -0,0 +1,6 @@
// "Insert lacking comma(s) / semicolon(s)" "true"
enum class MyEnum {
FIRST<caret> // The first one
SECOND
}
@@ -0,0 +1,6 @@
// "Insert lacking comma(s) / semicolon(s)" "true"
enum class MyEnum {
FIRST, // The first one
SECOND
}
@@ -0,0 +1,6 @@
// "Insert lacking comma(s) / semicolon(s)" "true"
enum class MyEnum {
FIRST<caret> /* The first one */ // It's really important
SECOND
}
@@ -0,0 +1,6 @@
// "Insert lacking comma(s) / semicolon(s)" "true"
enum class MyEnum {
FIRST, /* The first one */ // It's really important
SECOND
}
@@ -3144,6 +3144,24 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName);
}
@TestMetadata("noCommaWithBracketComment.kt")
public void testNoCommaWithBracketComment() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/enumDelimiter/noCommaWithBracketComment.kt");
doTest(fileName);
}
@TestMetadata("noCommaWithComment.kt")
public void testNoCommaWithComment() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/enumDelimiter/noCommaWithComment.kt");
doTest(fileName);
}
@TestMetadata("noCommaWithTwoComments.kt")
public void testNoCommaWithTwoComments() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/enumDelimiter/noCommaWithTwoComments.kt");
doTest(fileName);
}
@TestMetadata("noDelimiter.kt")
public void testNoDelimiter() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/enumDelimiter/noDelimiter.kt");