"Create enum constant" quick fix: handle trailing comman correctly
#KT-36461 Fixed #KT-36462 Fixed
This commit is contained in:
committed by
Yan Zhulanow
parent
0bd2643788
commit
4973d3c359
@@ -25,7 +25,6 @@ import org.jetbrains.kotlin.lexer.KtTokens
|
|||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.allChildren
|
import org.jetbrains.kotlin.psi.psiUtil.allChildren
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.nextSiblingOfSameType
|
import org.jetbrains.kotlin.psi.psiUtil.nextSiblingOfSameType
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.prevSiblingOfSameType
|
|
||||||
import org.jetbrains.kotlin.utils.addToStdlib.lastIsInstanceOrNull
|
import org.jetbrains.kotlin.utils.addToStdlib.lastIsInstanceOrNull
|
||||||
|
|
||||||
private class Visitor(var range: TextRange) : KtTreeVisitorVoid() {
|
private class Visitor(var range: TextRange) : KtTreeVisitorVoid() {
|
||||||
@@ -49,17 +48,6 @@ private class Visitor(var range: TextRange) : KtTreeVisitorVoid() {
|
|||||||
declaration.add(comma)
|
declaration.add(comma)
|
||||||
delta += comma.textLength
|
delta += comma.textLength
|
||||||
}
|
}
|
||||||
|
|
||||||
val prevEntry = declaration.prevSiblingOfSameType()
|
|
||||||
if (prevEntry != null && !prevEntry.containsToken(KtTokens.COMMA)) {
|
|
||||||
val semicolon = prevEntry.allChildren.firstOrNull { it.node?.elementType == KtTokens.SEMICOLON }
|
|
||||||
if (semicolon != null) {
|
|
||||||
semicolon.delete()
|
|
||||||
declaration.add(psiFactory.createSemicolon())
|
|
||||||
}
|
|
||||||
prevEntry.add(comma)
|
|
||||||
delta += comma.textLength
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
val lastEntry = klass.declarations.lastIsInstanceOrNull<KtEnumEntry>()
|
val lastEntry = klass.declarations.lastIsInstanceOrNull<KtEnumEntry>()
|
||||||
if (lastEntry != null &&
|
if (lastEntry != null &&
|
||||||
|
|||||||
+18
-3
@@ -1162,9 +1162,24 @@ internal fun <D : KtNamedDeclaration> placeDeclarationInContainer(
|
|||||||
when (declaration) {
|
when (declaration) {
|
||||||
is KtEnumEntry -> {
|
is KtEnumEntry -> {
|
||||||
val prevEnumEntry = declarationInPlace.siblings(forward = false, withItself = false).firstIsInstanceOrNull<KtEnumEntry>()
|
val prevEnumEntry = declarationInPlace.siblings(forward = false, withItself = false).firstIsInstanceOrNull<KtEnumEntry>()
|
||||||
if ((prevEnumEntry?.prevSibling as? PsiWhiteSpace)?.text?.contains('\n') == true) {
|
if (prevEnumEntry != null) {
|
||||||
val parent = declarationInPlace.parent
|
if ((prevEnumEntry.prevSibling as? PsiWhiteSpace)?.text?.contains('\n') == true) {
|
||||||
parent.addBefore(psiFactory.createNewLine(), declarationInPlace)
|
declarationInPlace.parent.addBefore(psiFactory.createNewLine(), declarationInPlace)
|
||||||
|
}
|
||||||
|
val comma = psiFactory.createComma()
|
||||||
|
if (prevEnumEntry.allChildren.any { it.node.elementType == KtTokens.COMMA }) {
|
||||||
|
declarationInPlace.add(comma)
|
||||||
|
} else {
|
||||||
|
prevEnumEntry.add(comma)
|
||||||
|
}
|
||||||
|
val semicolon = prevEnumEntry.allChildren.firstOrNull { it.node?.elementType == KtTokens.SEMICOLON }
|
||||||
|
if (semicolon != null) {
|
||||||
|
(semicolon.prevSibling as? PsiWhiteSpace)?.text?.let {
|
||||||
|
declarationInPlace.add(psiFactory.createWhiteSpace(it))
|
||||||
|
}
|
||||||
|
declarationInPlace.add(psiFactory.createSemicolon())
|
||||||
|
semicolon.delete()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
!is KtPrimaryConstructor -> {
|
!is KtPrimaryConstructor -> {
|
||||||
|
|||||||
Vendored
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
// "Create enum constant 'PUBLIC'" "true"
|
// "Create enum constant 'PUBLIC'" "true"
|
||||||
enum class TopicState {
|
enum class TopicState {
|
||||||
PRIVATE,
|
PRIVATE,
|
||||||
<caret>PUBLIC
|
<caret>PUBLIC,
|
||||||
}
|
}
|
||||||
|
|
||||||
fun foo() {
|
fun foo() {
|
||||||
|
|||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
// "Create enum constant 'C'" "true"
|
||||||
|
enum class Bar {
|
||||||
|
A,
|
||||||
|
B,
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
Bar.C<caret>
|
||||||
|
}
|
||||||
+11
@@ -0,0 +1,11 @@
|
|||||||
|
// "Create enum constant 'C'" "true"
|
||||||
|
enum class Bar {
|
||||||
|
A,
|
||||||
|
B,
|
||||||
|
<caret>C,
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
Bar.C
|
||||||
|
}
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
// "Create enum constant 'C'" "true"
|
||||||
|
enum class Baz {
|
||||||
|
A,
|
||||||
|
B, ;
|
||||||
|
}
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
Baz.C<caret>
|
||||||
|
}
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
// "Create enum constant 'C'" "true"
|
||||||
|
enum class Baz {
|
||||||
|
A,
|
||||||
|
B,
|
||||||
|
<caret>C, ;
|
||||||
|
}
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
Baz.C
|
||||||
|
}
|
||||||
@@ -3531,6 +3531,16 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
|||||||
runTest("idea/testData/quickfix/createFromUsage/createClass/referenceExpression/enumEntryHasComma.kt");
|
runTest("idea/testData/quickfix/createFromUsage/createClass/referenceExpression/enumEntryHasComma.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("enumEntryHasCommaAndSemicolon.kt")
|
||||||
|
public void testEnumEntryHasCommaAndSemicolon() throws Exception {
|
||||||
|
runTest("idea/testData/quickfix/createFromUsage/createClass/referenceExpression/enumEntryHasCommaAndSemicolon.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("enumEntryHasCommaAndSemicolon2.kt")
|
||||||
|
public void testEnumEntryHasCommaAndSemicolon2() throws Exception {
|
||||||
|
runTest("idea/testData/quickfix/createFromUsage/createClass/referenceExpression/enumEntryHasCommaAndSemicolon2.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("enumEntryHasNoLineBreak.kt")
|
@TestMetadata("enumEntryHasNoLineBreak.kt")
|
||||||
public void testEnumEntryHasNoLineBreak() throws Exception {
|
public void testEnumEntryHasNoLineBreak() throws Exception {
|
||||||
runTest("idea/testData/quickfix/createFromUsage/createClass/referenceExpression/enumEntryHasNoLineBreak.kt");
|
runTest("idea/testData/quickfix/createFromUsage/createClass/referenceExpression/enumEntryHasNoLineBreak.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user