Fix memory leak from SpecifyTypeExplicitlyIntention
This commit is contained in:
@@ -25,16 +25,18 @@ import com.intellij.codeInsight.template.impl.TemplateManagerImpl
|
|||||||
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil
|
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil
|
||||||
|
|
||||||
//TODO: move it somewhere else and reuse
|
//TODO: move it somewhere else and reuse
|
||||||
abstract class ChooseValueExpression<T : Any>(
|
abstract class ChooseValueExpression<in T : Any>(
|
||||||
lookupItems: Collection<T>,
|
lookupItems: Collection<T>,
|
||||||
protected val defaultItem: T,
|
defaultItem: T,
|
||||||
private val advertisementText: String? = null
|
private val advertisementText: String? = null
|
||||||
) : Expression() {
|
) : Expression() {
|
||||||
|
|
||||||
protected abstract fun getLookupString(element: T): String
|
protected abstract fun getLookupString(element: T): String
|
||||||
protected abstract fun getResult(element: T): String
|
protected abstract fun getResult(element: T): String
|
||||||
|
|
||||||
protected val lookupItems: Array<LookupElement> = lookupItems.map { suggestion ->
|
@Suppress("LeakingThis")
|
||||||
|
private val defaultItemString = getLookupString(defaultItem)
|
||||||
|
|
||||||
|
private val lookupItems: Array<LookupElement> = lookupItems.map { suggestion ->
|
||||||
LookupElementBuilder.create(suggestion, getLookupString(suggestion)).withInsertHandler { context, item ->
|
LookupElementBuilder.create(suggestion, getLookupString(suggestion)).withInsertHandler { context, item ->
|
||||||
val topLevelEditor = InjectedLanguageUtil.getTopLevelEditor(context.editor)
|
val topLevelEditor = InjectedLanguageUtil.getTopLevelEditor(context.editor)
|
||||||
val templateState = TemplateManagerImpl.getTemplateState(topLevelEditor)
|
val templateState = TemplateManagerImpl.getTemplateState(topLevelEditor)
|
||||||
@@ -52,7 +54,7 @@ abstract class ChooseValueExpression<T : Any>(
|
|||||||
|
|
||||||
override fun calculateQuickResult(context: ExpressionContext) = calculateResult(context)
|
override fun calculateQuickResult(context: ExpressionContext) = calculateResult(context)
|
||||||
|
|
||||||
override fun calculateResult(context: ExpressionContext) = TextResult(getLookupString(defaultItem))
|
override fun calculateResult(context: ExpressionContext) = TextResult(defaultItemString)
|
||||||
|
|
||||||
override fun getAdvertisingText() = advertisementText
|
override fun getAdvertisingText() = advertisementText
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -132,10 +132,14 @@ class SpecifyTypeExplicitlyIntention :
|
|||||||
}
|
}
|
||||||
}.ifEmpty { return null }
|
}.ifEmpty { return null }
|
||||||
|
|
||||||
return object : ChooseValueExpression<KotlinType>(types, types.first()) {
|
return TypeChooseValueExpression(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)
|
|
||||||
}
|
// Explicit class is used because of KT-20460
|
||||||
|
private class TypeChooseValueExpression(items: List<KotlinType>, defaultItem: KotlinType) :
|
||||||
|
ChooseValueExpression<KotlinType>(items, defaultItem) {
|
||||||
|
override fun getLookupString(element: KotlinType) = IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(element)
|
||||||
|
override fun getResult(element: KotlinType) = IdeDescriptorRenderers.SOURCE_CODE.renderType(element)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun addTypeAnnotation(editor: Editor?, declaration: KtCallableDeclaration, exprType: KotlinType) {
|
fun addTypeAnnotation(editor: Editor?, declaration: KtCallableDeclaration, exprType: KotlinType) {
|
||||||
|
|||||||
Reference in New Issue
Block a user