Convert property initializer to getter: suggest on property name
#KT-29344 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
20aa8ebdb0
commit
87dc1a035b
+11
-9
@@ -26,15 +26,19 @@ import org.jetbrains.kotlin.idea.util.hasJvmFieldAnnotation
|
||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||
import org.jetbrains.kotlin.psi.KtProperty
|
||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||
import org.jetbrains.kotlin.psi.psiUtil.isExtensionDeclaration
|
||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
import org.jetbrains.kotlin.types.isError
|
||||
|
||||
class ConvertPropertyInitializerToGetterIntention : SelfTargetingRangeIntention<KtProperty>(KtProperty::class.java, "Convert property initializer to getter") {
|
||||
|
||||
class ConvertPropertyInitializerToGetterIntention : SelfTargetingRangeIntention<KtProperty>(
|
||||
KtProperty::class.java, "Convert property initializer to getter"
|
||||
) {
|
||||
override fun applicabilityRange(element: KtProperty): TextRange? {
|
||||
val initializer = element.initializer
|
||||
return if (initializer != null && element.getter == null && !element.isExtensionDeclaration() && !element.isLocal && !element.hasJvmFieldAnnotation())
|
||||
initializer.textRange
|
||||
val initializer = element.initializer ?: return null
|
||||
val nameIdentifier = element.nameIdentifier ?: return null
|
||||
return if (element.getter == null && !element.isExtensionDeclaration() && !element.isLocal && !element.hasJvmFieldAnnotation())
|
||||
TextRange(nameIdentifier.startOffset, initializer.endOffset)
|
||||
else
|
||||
null
|
||||
}
|
||||
@@ -65,14 +69,12 @@ class ConvertPropertyInitializerToGetterIntention : SelfTargetingRangeIntention<
|
||||
|
||||
if (setter != null) {
|
||||
property.addBefore(getter, setter)
|
||||
}
|
||||
else if (property.isVar) {
|
||||
} else if (property.isVar) {
|
||||
property.add(getter)
|
||||
val notImplemented = KtPsiFactory(property).createExpression("TODO()")
|
||||
val notImplementedSetter = KtPsiFactory(property).createPropertySetter(notImplemented)
|
||||
property.add(notImplementedSetter)
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
property.add(getter)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
val <caret>abc = true
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
val abc: Boolean
|
||||
get() = true
|
||||
@@ -3,6 +3,7 @@
|
||||
// ACTION: Add getter and setter
|
||||
// ACTION: Add setter
|
||||
// ACTION: Specify type explicitly
|
||||
// ACTION: Convert property initializer to getter
|
||||
import usedVar as used
|
||||
|
||||
var <caret>usedVar = 0
|
||||
|
||||
@@ -6415,6 +6415,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
runTest("idea/testData/intentions/convertPropertyInitializerToGetter/notInLambda.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("onPropertyName.kt")
|
||||
public void testOnPropertyName() throws Exception {
|
||||
runTest("idea/testData/intentions/convertPropertyInitializerToGetter/onPropertyName.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("propertyWithInitializerWithSetter.kt")
|
||||
public void testPropertyWithInitializerWithSetter() throws Exception {
|
||||
runTest("idea/testData/intentions/convertPropertyInitializerToGetter/propertyWithInitializerWithSetter.kt");
|
||||
|
||||
Reference in New Issue
Block a user