Introduce Variable: Skip type in template if no resolvable/non-error types are available in the current context

#KT-10808 Fixed
This commit is contained in:
Alexey Sedunov
2016-02-04 15:36:45 +03:00
parent eacd50c4dc
commit 792d9c1ae2
6 changed files with 27 additions and 11 deletions
@@ -32,6 +32,7 @@ import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.utils.SmartSet
import org.jetbrains.kotlin.utils.addToStdlib.singletonList
import org.jetbrains.kotlin.utils.ifEmpty
class SpecifyTypeExplicitlyIntention : SelfTargetingIntention<KtCallableDeclaration>(KtCallableDeclaration::class.java, "Specify type explicitly"), LowPriorityAction {
override fun isApplicableTo(element: KtCallableDeclaration, caretOffset: Int): Boolean {
@@ -64,7 +65,7 @@ class SpecifyTypeExplicitlyIntention : SelfTargetingIntention<KtCallableDeclarat
return type ?: ErrorUtils.createErrorType("null type")
}
fun createTypeExpressionForTemplate(exprType: KotlinType, contextElement: KtElement): Expression {
fun createTypeExpressionForTemplate(exprType: KotlinType, contextElement: KtElement): Expression? {
val resolutionFacade = contextElement.getResolutionFacade()
val bindingContext = resolutionFacade.analyze(contextElement, BodyResolveMode.PARTIAL)
val scope = contextElement.getResolutionScope(bindingContext, resolutionFacade)
@@ -92,6 +93,7 @@ class SpecifyTypeExplicitlyIntention : SelfTargetingIntention<KtCallableDeclarat
override fun getArguments() = newArguments
}
}
.ifEmpty { return null }
return object : ChooseValueExpression<KotlinType>(types, types.first()) {
override fun getLookupString(element: KotlinType) = IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(element)
override fun getResult(element: KotlinType) = IdeDescriptorRenderers.SOURCE_CODE.renderType(element)
@@ -122,7 +124,7 @@ class SpecifyTypeExplicitlyIntention : SelfTargetingIntention<KtCallableDeclarat
assert(!exprType.isError) { "Unexpected error type, should have been checked before: " + declaration.getElementTextWithContext() + ", type = " + exprType }
val project = declaration.project
val expression = createTypeExpressionForTemplate(exprType, declaration)
val expression = createTypeExpressionForTemplate(exprType, declaration) ?: return
declaration.setType(KotlinBuiltIns.FQ_NAMES.any.asString())
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.idea.refactoring.introduce.introduceVariable;
import com.intellij.codeInsight.template.Expression;
import com.intellij.codeInsight.template.TemplateBuilderImpl;
import com.intellij.codeInsight.template.impl.TemplateManagerImpl;
import com.intellij.codeInsight.template.impl.TemplateState;
@@ -353,11 +354,9 @@ public class KotlinInplaceVariableIntroducer<D extends KtCallableDeclaration> ex
protected void addTypeReferenceVariable(TemplateBuilderImpl builder) {
KtTypeReference typeReference = myDeclaration.getTypeReference();
if (typeReference != null) {
builder.replaceElement(typeReference,
TYPE_REFERENCE_VARIABLE_NAME,
SpecifyTypeExplicitlyIntention.Companion.createTypeExpressionForTemplate(myExprType, myDeclaration),
false);
Expression expression = SpecifyTypeExplicitlyIntention.Companion.createTypeExpressionForTemplate(myExprType, myDeclaration);
if (typeReference != null && expression != null) {
builder.replaceElement(typeReference, TYPE_REFERENCE_VARIABLE_NAME, expression, false);
}
}
@@ -107,10 +107,8 @@ class KotlinVariableInplaceIntroducer(
override fun addAdditionalVariables(builder: TemplateBuilderImpl) {
addedVariable.typeReference?.let {
builder.replaceElement(it,
"TypeReferenceVariable",
SpecifyTypeExplicitlyIntention.createTypeExpressionForTemplate(expressionType!!, addedVariable),
false)
val expression = SpecifyTypeExplicitlyIntention.createTypeExpressionForTemplate(expressionType!!, addedVariable) ?: return@let
builder.replaceElement(it, "TypeReferenceVariable", expression, false)
}
}
@@ -0,0 +1,5 @@
fun main(args:Array<String>) {
val old = pref.getString("", "")
pref.edit().putString("", "").apply()
<selection>LocalBroadcastManager.getInstance(this)</selection>.sendBroadcast(Intent(""))
}
@@ -0,0 +1,6 @@
fun main(args:Array<String>) {
val old = pref.getString("", "")
pref.edit().putString("", "").apply()
val instance: Any = LocalBroadcastManager.getInstance(this)
instance.sendBroadcast(Intent(""))
}
@@ -217,6 +217,12 @@ public class ExtractionTestGenerated extends AbstractExtractionTest {
doIntroduceVariableTest(fileName);
}
@TestMetadata("kt10808.kt")
public void testKt10808() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/kt10808.kt");
doIntroduceVariableTest(fileName);
}
@TestMetadata("LoopRange.kt")
public void testLoopRange() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/LoopRange.kt");