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:
@@ -32,6 +32,7 @@ import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
|||||||
import org.jetbrains.kotlin.types.*
|
import org.jetbrains.kotlin.types.*
|
||||||
import org.jetbrains.kotlin.utils.SmartSet
|
import org.jetbrains.kotlin.utils.SmartSet
|
||||||
import org.jetbrains.kotlin.utils.addToStdlib.singletonList
|
import org.jetbrains.kotlin.utils.addToStdlib.singletonList
|
||||||
|
import org.jetbrains.kotlin.utils.ifEmpty
|
||||||
|
|
||||||
class SpecifyTypeExplicitlyIntention : SelfTargetingIntention<KtCallableDeclaration>(KtCallableDeclaration::class.java, "Specify type explicitly"), LowPriorityAction {
|
class SpecifyTypeExplicitlyIntention : SelfTargetingIntention<KtCallableDeclaration>(KtCallableDeclaration::class.java, "Specify type explicitly"), LowPriorityAction {
|
||||||
override fun isApplicableTo(element: KtCallableDeclaration, caretOffset: Int): Boolean {
|
override fun isApplicableTo(element: KtCallableDeclaration, caretOffset: Int): Boolean {
|
||||||
@@ -64,7 +65,7 @@ class SpecifyTypeExplicitlyIntention : SelfTargetingIntention<KtCallableDeclarat
|
|||||||
return type ?: ErrorUtils.createErrorType("null type")
|
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 resolutionFacade = contextElement.getResolutionFacade()
|
||||||
val bindingContext = resolutionFacade.analyze(contextElement, BodyResolveMode.PARTIAL)
|
val bindingContext = resolutionFacade.analyze(contextElement, BodyResolveMode.PARTIAL)
|
||||||
val scope = contextElement.getResolutionScope(bindingContext, resolutionFacade)
|
val scope = contextElement.getResolutionScope(bindingContext, resolutionFacade)
|
||||||
@@ -92,6 +93,7 @@ class SpecifyTypeExplicitlyIntention : SelfTargetingIntention<KtCallableDeclarat
|
|||||||
override fun getArguments() = newArguments
|
override fun getArguments() = newArguments
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.ifEmpty { return null }
|
||||||
return object : ChooseValueExpression<KotlinType>(types, types.first()) {
|
return object : ChooseValueExpression<KotlinType>(types, types.first()) {
|
||||||
override fun getLookupString(element: KotlinType) = IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(element)
|
override fun getLookupString(element: KotlinType) = IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(element)
|
||||||
override fun getResult(element: KotlinType) = IdeDescriptorRenderers.SOURCE_CODE.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 }
|
assert(!exprType.isError) { "Unexpected error type, should have been checked before: " + declaration.getElementTextWithContext() + ", type = " + exprType }
|
||||||
|
|
||||||
val project = declaration.project
|
val project = declaration.project
|
||||||
val expression = createTypeExpressionForTemplate(exprType, declaration)
|
val expression = createTypeExpressionForTemplate(exprType, declaration) ?: return
|
||||||
|
|
||||||
declaration.setType(KotlinBuiltIns.FQ_NAMES.any.asString())
|
declaration.setType(KotlinBuiltIns.FQ_NAMES.any.asString())
|
||||||
|
|
||||||
|
|||||||
+4
-5
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.idea.refactoring.introduce.introduceVariable;
|
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.TemplateBuilderImpl;
|
||||||
import com.intellij.codeInsight.template.impl.TemplateManagerImpl;
|
import com.intellij.codeInsight.template.impl.TemplateManagerImpl;
|
||||||
import com.intellij.codeInsight.template.impl.TemplateState;
|
import com.intellij.codeInsight.template.impl.TemplateState;
|
||||||
@@ -353,11 +354,9 @@ public class KotlinInplaceVariableIntroducer<D extends KtCallableDeclaration> ex
|
|||||||
|
|
||||||
protected void addTypeReferenceVariable(TemplateBuilderImpl builder) {
|
protected void addTypeReferenceVariable(TemplateBuilderImpl builder) {
|
||||||
KtTypeReference typeReference = myDeclaration.getTypeReference();
|
KtTypeReference typeReference = myDeclaration.getTypeReference();
|
||||||
if (typeReference != null) {
|
Expression expression = SpecifyTypeExplicitlyIntention.Companion.createTypeExpressionForTemplate(myExprType, myDeclaration);
|
||||||
builder.replaceElement(typeReference,
|
if (typeReference != null && expression != null) {
|
||||||
TYPE_REFERENCE_VARIABLE_NAME,
|
builder.replaceElement(typeReference, TYPE_REFERENCE_VARIABLE_NAME, expression, false);
|
||||||
SpecifyTypeExplicitlyIntention.Companion.createTypeExpressionForTemplate(myExprType, myDeclaration),
|
|
||||||
false);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-4
@@ -107,10 +107,8 @@ class KotlinVariableInplaceIntroducer(
|
|||||||
|
|
||||||
override fun addAdditionalVariables(builder: TemplateBuilderImpl) {
|
override fun addAdditionalVariables(builder: TemplateBuilderImpl) {
|
||||||
addedVariable.typeReference?.let {
|
addedVariable.typeReference?.let {
|
||||||
builder.replaceElement(it,
|
val expression = SpecifyTypeExplicitlyIntention.createTypeExpressionForTemplate(expressionType!!, addedVariable) ?: return@let
|
||||||
"TypeReferenceVariable",
|
builder.replaceElement(it, "TypeReferenceVariable", expression, false)
|
||||||
SpecifyTypeExplicitlyIntention.createTypeExpressionForTemplate(expressionType!!, addedVariable),
|
|
||||||
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(""))
|
||||||
|
}
|
||||||
+6
@@ -217,6 +217,12 @@ public class ExtractionTestGenerated extends AbstractExtractionTest {
|
|||||||
doIntroduceVariableTest(fileName);
|
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")
|
@TestMetadata("LoopRange.kt")
|
||||||
public void testLoopRange() throws Exception {
|
public void testLoopRange() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/LoopRange.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/LoopRange.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user