Fixed a quick fix for enum entry delimiters: situation with following comments. Three new tests. isAvailable().
This commit is contained in:
+21
-2
@@ -19,8 +19,7 @@ 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.PsiElement
|
import com.intellij.psi.*
|
||||||
import com.intellij.psi.PsiErrorElement
|
|
||||||
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
|
||||||
import org.jetbrains.kotlin.idea.quickfix.quickfixUtil.createIntentionForFirstParentOfType
|
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 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() {
|
companion object : JetSingleIntentionActionFactory() {
|
||||||
override fun createAction(diagnostic: Diagnostic): IntentionAction? =
|
override fun createAction(diagnostic: Diagnostic): IntentionAction? =
|
||||||
diagnostic.createIntentionForFirstParentOfType(::DeprecatedEnumEntryDelimiterSyntaxFix)
|
diagnostic.createIntentionForFirstParentOfType(::DeprecatedEnumEntryDelimiterSyntaxFix)
|
||||||
@@ -62,6 +64,7 @@ class DeprecatedEnumEntryDelimiterSyntaxFix(element: JetEnumEntry): JetIntention
|
|||||||
for ((entryIndex, entry) in entries.withIndex()) {
|
for ((entryIndex, entry) in entries.withIndex()) {
|
||||||
var next = entry.getNextSiblingIgnoringWhitespaceAndComments()
|
var next = entry.getNextSiblingIgnoringWhitespaceAndComments()
|
||||||
var nextType = next?.getNode()?.getElementType()
|
var nextType = next?.getNode()?.getElementType()
|
||||||
|
var added = false
|
||||||
if (entryIndex < entries.size() - 1) {
|
if (entryIndex < entries.size() - 1) {
|
||||||
if (next is PsiErrorElement && next.getFirstChild()?.getNode()?.getElementType() == JetTokens.SEMICOLON) {
|
if (next is PsiErrorElement && next.getFirstChild()?.getNode()?.getElementType() == JetTokens.SEMICOLON) {
|
||||||
// Fix for syntax error like ENUM_ENTRY1; ENUM_ENTRY2; ENUM_ENTRY3
|
// 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) {
|
else if (nextType != JetTokens.COMMA) {
|
||||||
// Classic case like ENUM_ENTRY1 ENUM_ENTRY2
|
// Classic case like ENUM_ENTRY1 ENUM_ENTRY2
|
||||||
body.addAfter(psiFactory.createComma(), entry)
|
body.addAfter(psiFactory.createComma(), entry)
|
||||||
|
added = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -80,6 +84,21 @@ class DeprecatedEnumEntryDelimiterSyntaxFix(element: JetEnumEntry): JetIntention
|
|||||||
else if (nextType != JetTokens.SEMICOLON && nextType != JetTokens.RBRACE) {
|
else if (nextType != JetTokens.SEMICOLON && nextType != JetTokens.RBRACE) {
|
||||||
// ENUM_ENTRY_LAST fun foo()
|
// ENUM_ENTRY_LAST fun foo()
|
||||||
body.addAfter(psiFactory.createSemicolon(), entry)
|
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);
|
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")
|
@TestMetadata("noDelimiter.kt")
|
||||||
public void testNoDelimiter() throws Exception {
|
public void testNoDelimiter() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/enumDelimiter/noDelimiter.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/enumDelimiter/noDelimiter.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user