Specify type: escape with backticks if need

#KT-29677 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-02-05 12:51:54 +09:00
committed by Mikhail Glukhikh
parent 1a818970c3
commit 240ff08069
4 changed files with 23 additions and 1 deletions
@@ -33,12 +33,14 @@ import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.core.ShortenReferences
import org.jetbrains.kotlin.idea.core.setType
import org.jetbrains.kotlin.idea.core.unquote
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
import org.jetbrains.kotlin.idea.util.application.runWriteAction
import org.jetbrains.kotlin.idea.util.getResolutionScope
import org.jetbrains.kotlin.idea.util.getResolvableApproximations
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.*
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.typeUtil.makeNotNullable
@@ -183,7 +185,18 @@ class SpecifyTypeExplicitlyIntention : SelfTargetingRangeIntention<KtCallableDec
items: List<KotlinType>, defaultItem: KotlinType
) : ChooseValueExpression<KotlinType>(items, defaultItem) {
override fun getLookupString(element: KotlinType) = IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_NO_ANNOTATIONS.renderType(element)
override fun getResult(element: KotlinType) = IdeDescriptorRenderers.SOURCE_CODE.renderType(element)
override fun getResult(element: KotlinType): String {
val renderType = IdeDescriptorRenderers.SOURCE_CODE.renderType(element)
val descriptor = element.constructor.declarationDescriptor
if (descriptor?.name?.asString() == renderType) {
val className = (DescriptorToSourceUtils.descriptorToDeclaration(descriptor) as? KtClass)?.nameIdentifier?.text
if (className != null && className != className.unquote()) {
return className
}
}
return renderType
}
}
fun addTypeAnnotation(editor: Editor?, declaration: KtCallableDeclaration, exprType: KotlinType) {
@@ -0,0 +1,2 @@
class `1`
val x<caret> = `1`()
@@ -0,0 +1,2 @@
class `1`
val x: `1` = `1`()
@@ -16216,6 +16216,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/specifyTypeExplicitly/anonymousObject.kt");
}
@TestMetadata("backticked.kt")
public void testBackticked() throws Exception {
runTest("idea/testData/intentions/specifyTypeExplicitly/backticked.kt");
}
@TestMetadata("badCaretPosition.kt")
public void testBadCaretPosition() throws Exception {
runTest("idea/testData/intentions/specifyTypeExplicitly/badCaretPosition.kt");