Minor: refactoring InsertExplicitTypeArgumentIntention

This commit is contained in:
Dmitry Gridin
2019-03-06 18:01:30 +03:00
parent 6ae5e91930
commit 212e573c26
@@ -20,8 +20,8 @@ import com.intellij.codeInsight.intention.LowPriorityAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
import org.jetbrains.kotlin.idea.core.ShortenReferences
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
import org.jetbrains.kotlin.psi.KtCallElement
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtPsiFactory
@@ -31,16 +31,17 @@ import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.types.ErrorUtils
class InsertExplicitTypeArgumentsIntention : SelfTargetingRangeIntention<KtCallExpression>(KtCallExpression::class.java, "Add explicit type arguments"), LowPriorityAction {
class InsertExplicitTypeArgumentsIntention :
SelfTargetingRangeIntention<KtCallExpression>(KtCallExpression::class.java, "Add explicit type arguments"), LowPriorityAction {
override fun applicabilityRange(element: KtCallExpression): TextRange? {
return if (isApplicableTo(element, element.analyze())) element.calleeExpression!!.textRange else null
return if (isApplicableTo(element, element.analyze())) element.calleeExpression?.textRange else null
}
override fun applyTo(element: KtCallExpression, editor: Editor?) = applyTo(element)
companion object {
fun isApplicableTo(element: KtCallElement, bindingContext: BindingContext = element.analyze(BodyResolveMode.PARTIAL)): Boolean {
if (!element.typeArguments.isEmpty()) return false
if (element.typeArguments.isNotEmpty()) return false
if (element.calleeExpression == null) return false
val resolvedCall = element.getResolvedCall(bindingContext) ?: return false
@@ -49,9 +50,9 @@ class InsertExplicitTypeArgumentsIntention : SelfTargetingRangeIntention<KtCallE
}
fun applyTo(element: KtCallElement, shortenReferences: Boolean = true) {
val argumentList = createTypeArguments(element, element.analyze())!!
val argumentList = createTypeArguments(element, element.analyze()) ?: return
val callee = element.calleeExpression!!
val callee = element.calleeExpression ?: return
val newArgumentList = element.addAfter(argumentList, callee) as KtTypeArgumentList
if (shortenReferences) {
@@ -65,7 +66,9 @@ class InsertExplicitTypeArgumentsIntention : SelfTargetingRangeIntention<KtCallE
val args = resolvedCall.typeArguments
val types = resolvedCall.candidateDescriptor.typeParameters
val text = types.joinToString(", ", "<", ">") { IdeDescriptorRenderers.SOURCE_CODE.renderType(args[it]!!) }
val text = types.joinToString(", ", "<", ">") {
IdeDescriptorRenderers.SOURCE_CODE.renderType(args.getValue(it))
}
return KtPsiFactory(element).createTypeArguments(text)
}