Add 'Change to val' quickfix for delegates without setValue

So #KT-13688 Fixed
This commit is contained in:
Toshiaki Kameyama
2018-06-01 05:21:10 +03:00
committed by Mikhail Glukhikh
parent 3292137acb
commit 2427406a8f
7 changed files with 81 additions and 3 deletions
@@ -23,18 +23,20 @@ import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.DiagnosticFactory1
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.lexer.KtToken
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.createCallable.CreatePropertyDelegateAccessorsActionFactory
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.util.OperatorNameConventions
class ChangeVariableMutabilityFix(
element: KtValVarKeywordOwner,
private val makeVar: Boolean
private val makeVar: Boolean,
private val actionText: String? = null
) : KotlinQuickFixAction<KtValVarKeywordOwner>(element) {
override fun getText() = if (makeVar) "Make variable mutable" else "Make variable immutable"
override fun getText() = actionText ?: if (makeVar) "Make variable mutable" else "Make variable immutable"
override fun getFamilyName(): String = text
@@ -108,5 +110,15 @@ class ChangeVariableMutabilityFix(
return ChangeVariableMutabilityFix(property, makeVar = false)
}
}
val DELEGATED_PROPERTY_VAL_FACTORY = object : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val element = Errors.DELEGATE_SPECIAL_FUNCTION_MISSING.cast(diagnostic).psiElement
val property = element.getStrictParentOfType<KtProperty>() ?: return null
val info = CreatePropertyDelegateAccessorsActionFactory.extractFixData(property, diagnostic).singleOrNull() ?: return null
if (info.name != OperatorNameConventions.SET_VALUE.asString()) return null
return ChangeVariableMutabilityFix(property, makeVar = false, actionText = "Change to val")
}
}
}
}
@@ -386,6 +386,7 @@ class QuickFixRegistrar : QuickFixContributor {
CreateDataClassPropertyFromDestructuringActionFactory
)
DELEGATE_SPECIAL_FUNCTION_MISSING.registerFactory(ChangeVariableMutabilityFix.DELEGATED_PROPERTY_VAL_FACTORY)
DELEGATE_SPECIAL_FUNCTION_MISSING.registerFactory(CreatePropertyDelegateAccessorsActionFactory)
DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE.registerFactory(CreatePropertyDelegateAccessorsActionFactory)
@@ -0,0 +1,12 @@
// "Change to val" "true"
import kotlin.reflect.KProperty
fun test() {
var foo: String by <caret>Delegate()
}
class Delegate {
operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
return ""
}
}
@@ -0,0 +1,12 @@
// "Change to val" "true"
import kotlin.reflect.KProperty
fun test() {
val foo: String by <caret>Delegate()
}
class Delegate {
operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
return ""
}
}
@@ -0,0 +1,14 @@
// "Change to val" "false"
// ACTION: Create extension function 'Delegate.getValue'
// ACTION: Create member function 'Delegate.getValue'
// ERROR: Missing 'getValue(Nothing?, KProperty<*>)' method on delegate of type 'Delegate'
import kotlin.reflect.KProperty
fun test() {
var foo: String by <caret>Delegate()
}
class Delegate {
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
}
}
@@ -0,0 +1,12 @@
// "Change to val" "false"
// ACTION: Create extension function 'Delegate.getValue', function 'Delegate.setValue'
// ACTION: Create member function 'Delegate.getValue', function 'Delegate.setValue'
// ERROR: Missing 'getValue(Nothing?, KProperty<*>)' method on delegate of type 'Delegate'
// ERROR: Missing 'setValue(Nothing?, KProperty<*>, String)' method on delegate of type 'Delegate'
import kotlin.reflect.KProperty
fun test() {
var foo: String by <caret>Delegate()
}
class Delegate
@@ -12085,6 +12085,21 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
runTest("idea/testData/quickfix/variables/changeMutability/canBeVal/const.kt");
}
@TestMetadata("delegatedProperty.kt")
public void testDelegatedProperty() throws Exception {
runTest("idea/testData/quickfix/variables/changeMutability/canBeVal/delegatedProperty.kt");
}
@TestMetadata("delegatedProperty2.kt")
public void testDelegatedProperty2() throws Exception {
runTest("idea/testData/quickfix/variables/changeMutability/canBeVal/delegatedProperty2.kt");
}
@TestMetadata("delegatedProperty3.kt")
public void testDelegatedProperty3() throws Exception {
runTest("idea/testData/quickfix/variables/changeMutability/canBeVal/delegatedProperty3.kt");
}
@TestMetadata("multiVariable.kt")
public void testMultiVariable() throws Exception {
runTest("idea/testData/quickfix/variables/changeMutability/canBeVal/multiVariable.kt");