AddConstModifierIntention: handle more edge cases #KT-13173 Fixed
(cherry picked from commit 4755596)
This commit is contained in:
committed by
Mikhail Glukhikh
parent
adbb07f30b
commit
5948b69679
@@ -31,7 +31,10 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
|||||||
import org.jetbrains.kotlin.idea.intentions.SelfTargetingIntention
|
import org.jetbrains.kotlin.idea.intentions.SelfTargetingIntention
|
||||||
import org.jetbrains.kotlin.idea.search.allScope
|
import org.jetbrains.kotlin.idea.search.allScope
|
||||||
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
|
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
|
||||||
|
import org.jetbrains.kotlin.idea.util.findAnnotation
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
|
import org.jetbrains.kotlin.name.FqName
|
||||||
|
import org.jetbrains.kotlin.psi.KtAnnotationEntry
|
||||||
import org.jetbrains.kotlin.psi.KtFile
|
import org.jetbrains.kotlin.psi.KtFile
|
||||||
import org.jetbrains.kotlin.psi.KtProperty
|
import org.jetbrains.kotlin.psi.KtProperty
|
||||||
import org.jetbrains.kotlin.psi.KtReferenceExpression
|
import org.jetbrains.kotlin.psi.KtReferenceExpression
|
||||||
@@ -47,9 +50,12 @@ class AddConstModifierFix(val property: KtProperty) : AddModifierFix(property, K
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
private val removeAnnotations = listOf(FqName("kotlin.jvm.JvmStatic"), FqName("kotlin.jvm.JvmField"))
|
||||||
|
|
||||||
fun addConstModifier(property: KtProperty) {
|
fun addConstModifier(property: KtProperty) {
|
||||||
replaceReferencesToGetterByReferenceToField(property)
|
replaceReferencesToGetterByReferenceToField(property)
|
||||||
property.addModifier(KtTokens.CONST_KEYWORD)
|
property.addModifier(KtTokens.CONST_KEYWORD)
|
||||||
|
removeAnnotations.mapNotNull { property.findAnnotation(it) }.forEach(KtAnnotationEntry::delete)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -66,7 +72,8 @@ class AddConstModifierIntention : SelfTargetingIntention<KtProperty>(KtProperty:
|
|||||||
companion object {
|
companion object {
|
||||||
fun isApplicableTo(element: KtProperty): Boolean {
|
fun isApplicableTo(element: KtProperty): Boolean {
|
||||||
if (element.isLocal || element.isVar || element.hasDelegate() || element.initializer == null
|
if (element.isLocal || element.isVar || element.hasDelegate() || element.initializer == null
|
||||||
|| element.getter?.hasBody() == true || element.receiverTypeReference != null) {
|
|| element.getter?.hasBody() == true || element.receiverTypeReference != null
|
||||||
|
|| element.hasModifier(KtTokens.CONST_KEYWORD) || element.hasModifier(KtTokens.OVERRIDE_KEYWORD)) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
val propertyDescriptor = element.descriptor as? VariableDescriptor ?: return false
|
val propertyDescriptor = element.descriptor as? VariableDescriptor ?: return false
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
// IS_APPLICABLE: FALSE
|
||||||
|
|
||||||
|
const val <caret>a = ""
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// IS_APPLICABLE: FALSE
|
||||||
|
|
||||||
|
interface I {
|
||||||
|
val a: String
|
||||||
|
}
|
||||||
|
object O: I {
|
||||||
|
override val <caret>a = ""
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
object O {
|
||||||
|
@JvmField val <caret>a = 1
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
object O {
|
||||||
|
const val <caret>a = 1
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
object O {
|
||||||
|
@JvmStatic val <caret>a = 1
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
object O {
|
||||||
|
const val <caret>a = 1
|
||||||
|
}
|
||||||
@@ -106,6 +106,30 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/addConstModifier"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/addConstModifier"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("inapplicableToConst.kt")
|
||||||
|
public void testInapplicableToConst() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addConstModifier/inapplicableToConst.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("inapplicableToOverride.kt")
|
||||||
|
public void testInapplicableToOverride() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addConstModifier/inapplicableToOverride.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("removeJvmField.kt")
|
||||||
|
public void testRemoveJvmField() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addConstModifier/removeJvmField.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("removeJvmStatic.kt")
|
||||||
|
public void testRemoveJvmStatic() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addConstModifier/removeJvmStatic.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("simple.kt")
|
@TestMetadata("simple.kt")
|
||||||
public void testSimple() throws Exception {
|
public void testSimple() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addConstModifier/simple.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addConstModifier/simple.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user