Support generic type parameters in 'Specify return type explicitly'
So #KT-18074 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
0af3c6542d
commit
8f9b680fc6
@@ -23,7 +23,9 @@ import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.psi.PsiDocumentManager
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.descriptors.CallableDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||
@@ -41,7 +43,6 @@ import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.types.*
|
||||
import org.jetbrains.kotlin.types.typeUtil.containsTypeAliases
|
||||
import org.jetbrains.kotlin.utils.ifEmpty
|
||||
|
||||
class SpecifyTypeExplicitlyIntention :
|
||||
@@ -114,7 +115,17 @@ class SpecifyTypeExplicitlyIntention :
|
||||
val resolutionFacade = contextElement.getResolutionFacade()
|
||||
val bindingContext = resolutionFacade.analyze(contextElement, BodyResolveMode.PARTIAL)
|
||||
val scope = contextElement.getResolutionScope(bindingContext, resolutionFacade)
|
||||
val types = with (exprType.getResolvableApproximations(scope, true).toList()) {
|
||||
|
||||
var checkTypeParameters = true
|
||||
val descriptor = exprType.constructor.declarationDescriptor
|
||||
if (descriptor != null && descriptor is TypeParameterDescriptor) {
|
||||
val owner = descriptor.containingDeclaration
|
||||
if (owner is FunctionDescriptor && owner.typeParameters.contains(descriptor)) {
|
||||
checkTypeParameters = false
|
||||
}
|
||||
}
|
||||
|
||||
val types = with (exprType.getResolvableApproximations(scope, checkTypeParameters).toList()) {
|
||||
when {
|
||||
exprType.isNullabilityFlexible() -> flatMap {
|
||||
listOf(TypeUtils.makeNotNullable(it), TypeUtils.makeNullable(it))
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
fun <T> generic1(): T = null!!
|
||||
|
||||
class My<T> {
|
||||
fun <caret>foo() = generic1<T>()
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun <T> generic1(): T = null!!
|
||||
|
||||
class My<T> {
|
||||
fun foo(): T = generic1<T>()
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fun <T> generic1(): T? = null
|
||||
fun <T> <caret>foo() = generic1<T>()
|
||||
@@ -0,0 +1,2 @@
|
||||
fun <T> generic1(): T? = null
|
||||
fun <T> foo(): T? = generic1<T>()
|
||||
@@ -14609,6 +14609,18 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("genericClass.kt")
|
||||
public void testGenericClass() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/specifyTypeExplicitly/genericClass.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("genericFunction.kt")
|
||||
public void testGenericFunction() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/specifyTypeExplicitly/genericFunction.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("lambdaParam.kt")
|
||||
public void testLambdaParam() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/specifyTypeExplicitly/lambdaParam.kt");
|
||||
|
||||
Reference in New Issue
Block a user