KT-18978 Intention Move to class body generates incorrect code for vararg val/var (#1188)
Intention Move to class body generates incorrect code for vararg val/var #KT-18978 Fixed
This commit is contained in:
committed by
Vyacheslav Gerasimov
parent
a59021a25e
commit
fe599463ac
@@ -21,6 +21,7 @@ import com.intellij.psi.util.PsiTreeUtil
|
||||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
|
||||
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.isPropertyParameter
|
||||
import org.jetbrains.kotlin.resolve.AnnotationChecker
|
||||
@@ -42,6 +43,7 @@ class MovePropertyToClassBodyIntention : SelfTargetingIntention<KtParameter>(KtP
|
||||
val firstProperty = parentClass.getProperties().firstOrNull()
|
||||
parentClass.addDeclarationBefore(propertyDeclaration, firstProperty).apply {
|
||||
val propertyModifierList = element.modifierList?.copy() as? KtModifierList
|
||||
propertyModifierList?.getModifier(KtTokens.VARARG_KEYWORD)?.delete()
|
||||
propertyModifierList?.let { modifierList?.replace(it) ?: addBefore(it, firstChild) }
|
||||
modifierList?.annotationEntries?.forEach {
|
||||
if (!it.isAppliedToProperty()) {
|
||||
@@ -59,12 +61,14 @@ class MovePropertyToClassBodyIntention : SelfTargetingIntention<KtParameter>(KtP
|
||||
?.takeIf { it.isNotEmpty() }
|
||||
?.joinToString(separator = " ") { it.textWithoutUseSite() }
|
||||
|
||||
val hasVararg = element.hasModifier(KtTokens.VARARG_KEYWORD)
|
||||
if (parameterAnnotationsText != null) {
|
||||
element.modifierList?.replace(KtPsiFactory(element).createModifierList(parameterAnnotationsText))
|
||||
}
|
||||
else {
|
||||
element.modifierList?.delete()
|
||||
}
|
||||
if (hasVararg) element.addModifier(KtTokens.VARARG_KEYWORD)
|
||||
}
|
||||
|
||||
fun KtAnnotationEntry.isAppliedToProperty(): Boolean {
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
class TestClass(vararg var <caret>words: String)
|
||||
@@ -0,0 +1,3 @@
|
||||
class TestClass(vararg words: String) {
|
||||
var words = words
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
@Target(AnnotationTarget.VALUE_PARAMETER)
|
||||
annotation class ParameterAnnotation(val a: Int = 0)
|
||||
|
||||
class TestClass(private vararg @ParameterAnnotation(42) val <caret>words: String = arrayOf())
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
@Target(AnnotationTarget.VALUE_PARAMETER)
|
||||
annotation class ParameterAnnotation(val a: Int = 0)
|
||||
|
||||
class TestClass(vararg @ParameterAnnotation(42) words: String = arrayOf()) {
|
||||
private val words = words
|
||||
}
|
||||
@@ -11362,6 +11362,18 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/movePropertyToClassBody/simple.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("vararg.kt")
|
||||
public void testVararg() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/movePropertyToClassBody/vararg.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("varargWithAnnotation.kt")
|
||||
public void testVarargWithAnnotation() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/movePropertyToClassBody/varargWithAnnotation.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/movePropertyToConstructor")
|
||||
|
||||
Reference in New Issue
Block a user