Specify type explicitly intention: now derived from self targeting range intention

(cherry picked from commit b09b5a9)
This commit is contained in:
Mikhail Glukhikh
2016-06-14 14:44:58 +03:00
committed by Mikhail Glukhikh
parent 5c4b6ba8b0
commit 2b2b937263
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.idea.intentions
import com.intellij.codeInsight.intention.LowPriorityAction import com.intellij.codeInsight.intention.LowPriorityAction
import com.intellij.codeInsight.template.* import com.intellij.codeInsight.template.*
import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.Editor
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiDocumentManager import com.intellij.psi.PsiDocumentManager
import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.CallableDescriptor 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.getResolutionScope
import org.jetbrains.kotlin.idea.util.isResolvableInScope import org.jetbrains.kotlin.idea.util.isResolvableInScope
import org.jetbrains.kotlin.psi.* 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.getElementTextWithContext
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.types.* 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.addToStdlib.singletonList
import org.jetbrains.kotlin.utils.ifEmpty import org.jetbrains.kotlin.utils.ifEmpty
class SpecifyTypeExplicitlyIntention : SelfTargetingIntention<KtCallableDeclaration>(KtCallableDeclaration::class.java, "Specify type explicitly"), LowPriorityAction { class SpecifyTypeExplicitlyIntention :
override fun isApplicableTo(element: KtCallableDeclaration, caretOffset: Int): Boolean { SelfTargetingRangeIntention<KtCallableDeclaration>(KtCallableDeclaration::class.java, "Specify type explicitly"),
if (element.containingFile is KtCodeFragment) return false LowPriorityAction {
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
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 (getTypeForDeclaration(element).isError) return null
if (initializer != null && initializer.textRange.containsOffset(caretOffset)) return false
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" 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?) { override fun applyTo(element: KtCallableDeclaration, editor: Editor?) {