Introduce intention 'expr.unsafeCast<Type>()' <- -> 'expr as Type'

So #KT-16382 Fixed
This commit is contained in:
Toshiaki Kameyama
2018-02-07 08:07:50 +03:00
committed by Mikhail Glukhikh
parent 085c7d30ed
commit e35ec8ee08
18 changed files with 187 additions and 0 deletions
+10
View File
@@ -1539,6 +1539,16 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertUnsafeCastCallToUnsafeCastIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertUnsafeCastToUnsafeCastCallIntention</className>
<category>Kotlin</category>
</intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
displayName="Object literal can be converted to lambda"
groupPath="Kotlin"
@@ -0,0 +1,41 @@
/*
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.project.platform
import org.jetbrains.kotlin.js.resolve.JsPlatform
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
class ConvertUnsafeCastCallToUnsafeCastIntention : SelfTargetingIntention<KtDotQualifiedExpression>(
KtDotQualifiedExpression::class.java, "Convert to unsafe cast"
) {
override fun isApplicableTo(element: KtDotQualifiedExpression, caretOffset: Int): Boolean {
if (element.platform != JsPlatform) return false
if ((element.selectorExpression as? KtCallExpression)?.calleeExpression?.text != "unsafeCast") return false
val context = element.analyze(BodyResolveMode.PARTIAL)
val fqName = element.getResolvedCall(context)?.resultingDescriptor?.fqNameOrNull()?.asString() ?: return false
if (fqName != "kotlin.js.unsafeCast") return false
val type = element.callExpression?.typeArguments?.singleOrNull() ?: return false
text = "Convert to '${element.receiverExpression.text} as ${type.text}'"
return true
}
override fun applyTo(element: KtDotQualifiedExpression, editor: Editor?) {
val type = element.callExpression?.typeArguments?.singleOrNull() ?: return
val newExpression = KtPsiFactory(element).createExpressionByPattern("$0 as $1", element.receiverExpression, type.text)
element.replace(newExpression)
}
}
@@ -0,0 +1,44 @@
/*
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.project.platform
import org.jetbrains.kotlin.js.resolve.JsPlatform
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtBinaryExpressionWithTypeRHS
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.createExpressionByPattern
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.types.TypeUtils
class ConvertUnsafeCastToUnsafeCastCallIntention : SelfTargetingIntention<KtBinaryExpressionWithTypeRHS>(
KtBinaryExpressionWithTypeRHS::class.java, "Convert to unsafeCast() call"
) {
override fun isApplicableTo(element: KtBinaryExpressionWithTypeRHS, caretOffset: Int): Boolean {
if (element.platform != JsPlatform) return false
if (element.operationReference.getReferencedNameElementType() != KtTokens.AS_KEYWORD) return false
val right = element.right ?: return false
val context = right.analyze(BodyResolveMode.PARTIAL)
val type = context[BindingContext.TYPE, right] ?: return false
if (TypeUtils.isNullableType(type)) return false
text = "Convert to '${element.left.text}.unsafeCast<${right.text}>()'"
return true
}
override fun applyTo(element: KtBinaryExpressionWithTypeRHS, editor: Editor?) {
val right = element.right ?: return
val newExpression = KtPsiFactory(element).createExpressionByPattern("$0.unsafeCast<$1>()", element.left, right)
element.replace(newExpression)
}
}