Insert explicit property type while converting accessor to block body
So #KT-20417 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
31f5c105f3
commit
1db365bb59
@@ -19,8 +19,10 @@ package org.jetbrains.kotlin.idea.intentions
|
||||
import com.intellij.codeInsight.intention.LowPriorityAction
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.descriptors.CallableDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||
import org.jetbrains.kotlin.idea.core.setType
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
@@ -81,7 +83,15 @@ class ConvertToBlockBodyIntention : SelfTargetingIntention<KtDeclarationWithBody
|
||||
generateBody(!returnType.isUnit() && !returnType.isNothing())
|
||||
}
|
||||
|
||||
is KtPropertyAccessor -> generateBody(declaration.isGetter)
|
||||
is KtPropertyAccessor -> {
|
||||
val parent = declaration.parent
|
||||
if (parent is KtProperty && parent.typeReference == null) {
|
||||
val descriptor = parent.resolveToDescriptorIfAny()
|
||||
(descriptor as? CallableDescriptor)?.returnType?.let { parent.setType(it) }
|
||||
}
|
||||
|
||||
generateBody(declaration.isGetter)
|
||||
}
|
||||
|
||||
else -> throw RuntimeException("Unknown declaration type: $declaration")
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
val foo <caret>get() = "abc"
|
||||
@@ -0,0 +1,4 @@
|
||||
val foo: String
|
||||
get() {
|
||||
return "abc"
|
||||
}
|
||||
@@ -6479,6 +6479,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("getterTypeInferred.kt")
|
||||
public void testGetterTypeInferred() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToBlockBody/getterTypeInferred.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("getterWithThrow.kt")
|
||||
public void testGetterWithThrow() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToBlockBody/getterWithThrow.kt");
|
||||
|
||||
Reference in New Issue
Block a user