From 2b2b9372637dded95277777400430c878c865ebd Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Tue, 14 Jun 2016 14:44:58 +0300 Subject: [PATCH] Specify type explicitly intention: now derived from self targeting range intention (cherry picked from commit b09b5a9) --- .../SpecifyTypeExplicitlyIntention.kt | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/SpecifyTypeExplicitlyIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/SpecifyTypeExplicitlyIntention.kt index feb1a1f6f6f..a5757cb74d2 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/SpecifyTypeExplicitlyIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/SpecifyTypeExplicitlyIntention.kt @@ -19,6 +19,7 @@ package org.jetbrains.kotlin.idea.intentions import com.intellij.codeInsight.intention.LowPriorityAction import com.intellij.codeInsight.template.* import com.intellij.openapi.editor.Editor +import com.intellij.openapi.util.TextRange import com.intellij.psi.PsiDocumentManager import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.CallableDescriptor @@ -32,7 +33,9 @@ import org.jetbrains.kotlin.idea.util.approximateWithResolvableType import org.jetbrains.kotlin.idea.util.getResolutionScope import org.jetbrains.kotlin.idea.util.isResolvableInScope import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.endOffset import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext +import org.jetbrains.kotlin.psi.psiUtil.startOffset import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode import org.jetbrains.kotlin.types.* @@ -40,23 +43,29 @@ import org.jetbrains.kotlin.utils.SmartSet import org.jetbrains.kotlin.utils.addToStdlib.singletonList import org.jetbrains.kotlin.utils.ifEmpty -class SpecifyTypeExplicitlyIntention : SelfTargetingIntention(KtCallableDeclaration::class.java, "Specify type explicitly"), LowPriorityAction { - override fun isApplicableTo(element: KtCallableDeclaration, caretOffset: Int): Boolean { - if (element.containingFile is KtCodeFragment) return false - if (element is KtFunctionLiteral) return false // TODO: should JetFunctionLiteral be JetCallableDeclaration at all? - if (element is KtConstructor<*>) return false - if (element.typeReference != null) return false +class SpecifyTypeExplicitlyIntention : + SelfTargetingRangeIntention(KtCallableDeclaration::class.java, "Specify type explicitly"), + LowPriorityAction { - if (getTypeForDeclaration(element).isError) return false + override fun applicabilityRange(element: KtCallableDeclaration): TextRange? { + if (element.containingFile is KtCodeFragment) return null + if (element is KtFunctionLiteral) return null // TODO: should KtFunctionLiteral be KtCallableDeclaration at all? + if (element is KtConstructor<*>) return null + if (element.typeReference != null) return null - val initializer = (element as? KtWithExpressionInitializer)?.initializer - if (initializer != null && initializer.textRange.containsOffset(caretOffset)) return false + if (getTypeForDeclaration(element).isError) return null - if (element is KtNamedFunction && element.hasBlockBody()) return false + if (element is KtNamedFunction && element.hasBlockBody()) return null text = if (element is KtFunction) "Specify return type explicitly" else "Specify type explicitly" - return true + val initializer = (element as? KtWithExpressionInitializer)?.initializer + return if (initializer != null) { + TextRange(element.startOffset, initializer.startOffset - 1) + } + else { + TextRange(element.startOffset, element.endOffset) + } } override fun applyTo(element: KtCallableDeclaration, editor: Editor?) {