Introduce intention 'expr.unsafeCast<Type>()' <- -> 'expr as Type'
So #KT-16382 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
085c7d30ed
commit
e35ec8ee08
+3
@@ -0,0 +1,3 @@
|
||||
fun test(foo: dynamic) {
|
||||
val s = <spot>foo as String</spot>
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun test(foo: dynamic) {
|
||||
val s = <spot>foo.unsafeCast<String>()</spot>
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention converts unsafeCast() call to unsafe cast.
|
||||
</body>
|
||||
</html>
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun test(foo: dynamic) {
|
||||
val s = <spot>foo.unsafeCast<String>()</spot>
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun test(foo: dynamic) {
|
||||
val s = <spot>foo as String</spot>
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention converts unsafe cast to unsafeCast() call.
|
||||
</body>
|
||||
</html>
|
||||
@@ -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"
|
||||
|
||||
+41
@@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
+44
@@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.intentions.ConvertUnsafeCastCallToUnsafeCastIntention
|
||||
@@ -0,0 +1,4 @@
|
||||
// JS
|
||||
fun test(foo: dynamic) {
|
||||
val s = <caret>foo.unsafeCast<String>()
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// JS
|
||||
fun test(foo: dynamic) {
|
||||
val s = <caret>foo as String
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// JS
|
||||
// IS_APPLICABLE: false
|
||||
fun test(foo: Any?) {
|
||||
val s = <caret>foo?.unsafeCast<String>()
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.intentions.ConvertUnsafeCastToUnsafeCastCallIntention
|
||||
@@ -0,0 +1,4 @@
|
||||
// JS
|
||||
fun test(foo: dynamic) {
|
||||
val s = <caret>foo as String
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// JS
|
||||
fun test(foo: dynamic) {
|
||||
val s = <caret>foo.unsafeCast<String>()
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// JS
|
||||
// IS_APPLICABLE: false
|
||||
fun test(foo: Any) {
|
||||
val s = <caret>foo as? String
|
||||
}
|
||||
@@ -7141,6 +7141,48 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/convertUnsafeCastCallToUnsafeCast")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class ConvertUnsafeCastCallToUnsafeCast extends AbstractIntentionTest {
|
||||
public void testAllFilesPresentInConvertUnsafeCastCallToUnsafeCast() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/convertUnsafeCastCallToUnsafeCast"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("call.kt")
|
||||
public void testCall() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertUnsafeCastCallToUnsafeCast/call.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("safeCall.kt")
|
||||
public void testSafeCall() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertUnsafeCastCallToUnsafeCast/safeCall.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/convertUnsafeCastToUnsafeCastCall")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class ConvertUnsafeCastToUnsafeCastCall extends AbstractIntentionTest {
|
||||
public void testAllFilesPresentInConvertUnsafeCastToUnsafeCastCall() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/convertUnsafeCastToUnsafeCastCall"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("cast.kt")
|
||||
public void testCast() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertUnsafeCastToUnsafeCastCall/cast.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("safeCast.kt")
|
||||
public void testSafeCast() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertUnsafeCastToUnsafeCastCall/safeCast.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/copyConcatenatedStringToClipboard")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user