Specify property type by accessor type (preliminary) #KT-9498 Fixed
This commit is contained in:
@@ -22,6 +22,7 @@ import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.psi.PsiDocumentManager
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.descriptors.CallableDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
|
||||
import org.jetbrains.kotlin.idea.util.*
|
||||
@@ -59,9 +60,19 @@ class SpecifyTypeExplicitlyIntention : SelfTargetingIntention<KtCallableDeclarat
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
private val PropertyDescriptor.getterType: KotlinType?
|
||||
get() = getter?.returnType?.let { if (it.isError) null else it }
|
||||
|
||||
private val PropertyDescriptor.setterType: KotlinType?
|
||||
get() = setter?.valueParameters?.firstOrNull()?.type?.let { if (it.isError) null else it }
|
||||
|
||||
fun getTypeForDeclaration(declaration: KtCallableDeclaration): KotlinType {
|
||||
val descriptor = declaration.analyze()[BindingContext.DECLARATION_TO_DESCRIPTOR, declaration]
|
||||
val type = (descriptor as? CallableDescriptor)?.returnType
|
||||
if (type != null && type.isError && descriptor is PropertyDescriptor) {
|
||||
return descriptor.getterType ?: descriptor.setterType ?: ErrorUtils.createErrorType("null type")
|
||||
}
|
||||
return type ?: ErrorUtils.createErrorType("null type")
|
||||
}
|
||||
|
||||
|
||||
@@ -184,6 +184,8 @@ class QuickFixRegistrar : QuickFixContributor {
|
||||
UNSAFE_INFIX_CALL.registerFactory(ReplaceInfixCallFix)
|
||||
|
||||
AMBIGUOUS_ANONYMOUS_TYPE_INFERRED.registerActions(SpecifyTypeExplicitlyFix())
|
||||
PROPERTY_WITH_NO_TYPE_NO_INITIALIZER.registerActions(SpecifyTypeExplicitlyFix())
|
||||
MUST_BE_INITIALIZED.registerActions(SpecifyTypeExplicitlyFix())
|
||||
|
||||
ELSE_MISPLACED_IN_WHEN.registerFactory(MoveWhenElseBranchFix)
|
||||
NO_ELSE_IN_WHEN.registerFactory(AddWhenElseBranchFix)
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
// "Specify type explicitly" "true"
|
||||
|
||||
val <caret>x
|
||||
get(): Int = 42
|
||||
@@ -0,0 +1,4 @@
|
||||
// "Specify type explicitly" "true"
|
||||
|
||||
val x: Int
|
||||
get(): Int = 42
|
||||
@@ -0,0 +1,12 @@
|
||||
// "Specify type explicitly" "true"
|
||||
|
||||
class My {
|
||||
|
||||
var yy = 0
|
||||
|
||||
var <caret>y
|
||||
get() = yy
|
||||
set(arg: Int) {
|
||||
yy = arg + 1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// "Specify type explicitly" "true"
|
||||
|
||||
class My {
|
||||
|
||||
var yy = 0
|
||||
|
||||
var y: Int
|
||||
get() = yy
|
||||
set(arg: Int) {
|
||||
yy = arg + 1
|
||||
}
|
||||
}
|
||||
@@ -6775,6 +6775,18 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("propertyWithGetterWithoutType.kt")
|
||||
public void testPropertyWithGetterWithoutType() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeAddition/propertyWithGetterWithoutType.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("propertyWithSetterWithoutType.kt")
|
||||
public void testPropertyWithSetterWithoutType() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeAddition/propertyWithSetterWithoutType.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("protectedFunWithoutReturnType.kt")
|
||||
public void testProtectedFunWithoutReturnType() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeAddition/protectedFunWithoutReturnType.kt");
|
||||
|
||||
Reference in New Issue
Block a user