"Create abstract function" quick fix: don't suggest it if super classes are not writable

#KT-40215 Fixed
This commit is contained in:
Toshiaki Kameyama
2020-07-12 16:07:06 +09:00
committed by Roman Golyshev
parent 0c832e30bf
commit 80e4e7939d
3 changed files with 23 additions and 1 deletions
@@ -148,7 +148,10 @@ sealed class CreateCallableFromCallActionFactory<E : KtExpression>(
receiverTypeInfo = TypeInfo(receiverType, Variance.IN_VARIANCE).ofThis()
}
if (!receiverType.isAbstract() && TypeUtils.getAllSupertypes(receiverType).all { !it.isAbstract() }) return null
val project = originalExpression.project
if (!receiverType.isAbstract() && TypeUtils.getAllSupertypes(receiverType).none { it.isAbstract() && it.canRefactor(project) }) {
return null
}
return mainCallable.copy(
receiverTypeInfo = receiverTypeInfo,
@@ -157,6 +160,11 @@ sealed class CreateCallableFromCallActionFactory<E : KtExpression>(
)
}
private fun KotlinType.canRefactor(project: Project): Boolean {
val declarationDescriptor = constructor.declarationDescriptor ?: return false
return DescriptorToSourceUtilsIde.getAnyDeclaration(project, declarationDescriptor)?.canRefactor() == true
}
protected fun getCallableWithReceiverInsideExtension(
mainCallable: CallableInfo,
originalExpression: KtExpression,
@@ -0,0 +1,9 @@
// "Create abstract function 'bar'" "false"
// ACTION: Create function 'bar'
// ACTION: Rename reference
// ERROR: Unresolved reference: bar
class Foo : Runnable {
override fun run() {
<caret>bar()
}
}
@@ -4478,6 +4478,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
runTest("idea/testData/quickfix/createFromUsage/createFunction/call/abstract/notAbstractClass.kt");
}
@TestMetadata("notWritableAbstractSuperclass.kt")
public void testNotWritableAbstractSuperclass() throws Exception {
runTest("idea/testData/quickfix/createFromUsage/createFunction/call/abstract/notWritableAbstractSuperclass.kt");
}
@TestMetadata("otherExplicitReceiver.kt")
public void testOtherExplicitReceiver() throws Exception {
runTest("idea/testData/quickfix/createFromUsage/createFunction/call/abstract/otherExplicitReceiver.kt");