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.KtDeclaration
|
||||||
import org.jetbrains.kotlin.psi.KtProperty
|
import org.jetbrains.kotlin.psi.KtProperty
|
||||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
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.isExtensionDeclaration
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||||
import org.jetbrains.kotlin.types.isError
|
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? {
|
override fun applicabilityRange(element: KtProperty): TextRange? {
|
||||||
val initializer = element.initializer
|
val initializer = element.initializer ?: return null
|
||||||
return if (initializer != null && element.getter == null && !element.isExtensionDeclaration() && !element.isLocal && !element.hasJvmFieldAnnotation())
|
val nameIdentifier = element.nameIdentifier ?: return null
|
||||||
initializer.textRange
|
return if (element.getter == null && !element.isExtensionDeclaration() && !element.isLocal && !element.hasJvmFieldAnnotation())
|
||||||
|
TextRange(nameIdentifier.startOffset, initializer.endOffset)
|
||||||
else
|
else
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
@@ -65,14 +69,12 @@ class ConvertPropertyInitializerToGetterIntention : SelfTargetingRangeIntention<
|
|||||||
|
|
||||||
if (setter != null) {
|
if (setter != null) {
|
||||||
property.addBefore(getter, setter)
|
property.addBefore(getter, setter)
|
||||||
}
|
} else if (property.isVar) {
|
||||||
else if (property.isVar) {
|
|
||||||
property.add(getter)
|
property.add(getter)
|
||||||
val notImplemented = KtPsiFactory(property).createExpression("TODO()")
|
val notImplemented = KtPsiFactory(property).createExpression("TODO()")
|
||||||
val notImplementedSetter = KtPsiFactory(property).createPropertySetter(notImplemented)
|
val notImplementedSetter = KtPsiFactory(property).createPropertySetter(notImplemented)
|
||||||
property.add(notImplementedSetter)
|
property.add(notImplementedSetter)
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
property.add(getter)
|
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 getter and setter
|
||||||
// ACTION: Add setter
|
// ACTION: Add setter
|
||||||
// ACTION: Specify type explicitly
|
// ACTION: Specify type explicitly
|
||||||
|
// ACTION: Convert property initializer to getter
|
||||||
import usedVar as used
|
import usedVar as used
|
||||||
|
|
||||||
var <caret>usedVar = 0
|
var <caret>usedVar = 0
|
||||||
|
|||||||
@@ -6415,6 +6415,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
runTest("idea/testData/intentions/convertPropertyInitializerToGetter/notInLambda.kt");
|
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")
|
@TestMetadata("propertyWithInitializerWithSetter.kt")
|
||||||
public void testPropertyWithInitializerWithSetter() throws Exception {
|
public void testPropertyWithInitializerWithSetter() throws Exception {
|
||||||
runTest("idea/testData/intentions/convertPropertyInitializerToGetter/propertyWithInitializerWithSetter.kt");
|
runTest("idea/testData/intentions/convertPropertyInitializerToGetter/propertyWithInitializerWithSetter.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user