Add quickfix for changing primitive cast to primitive conversion method
Fixes #KT-15564
This commit is contained in:
committed by
Mikhail Glukhikh
parent
2b54f1ac72
commit
a0491d5258
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2016 JetBrains s.r.o.
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -464,6 +464,8 @@ class QuickFixRegistrar : QuickFixContributor {
|
||||
|
||||
HEADER_WITHOUT_IMPLEMENTATION.registerFactory(CreateHeaderImplementationFix)
|
||||
|
||||
CAST_NEVER_SUCCEEDS.registerFactory(ReplacePrimitiveCastWithNumberConversionFix)
|
||||
|
||||
ErrorsJs.WRONG_EXTERNAL_DECLARATION.registerFactory(MigrateExternalExtensionFix)
|
||||
}
|
||||
}
|
||||
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.quickfix
|
||||
|
||||
import com.intellij.codeInsight.intention.IntentionAction
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.diagnostics.Errors.CAST_NEVER_SUCCEEDS
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.core.replaced
|
||||
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
|
||||
import org.jetbrains.kotlin.resolve.typeBinding.createTypeBinding
|
||||
import org.jetbrains.kotlin.types.typeUtil.isPrimitiveNumberType
|
||||
|
||||
class ReplacePrimitiveCastWithNumberConversionFix(
|
||||
element: KtBinaryExpressionWithTypeRHS,
|
||||
private val targetShortType: String
|
||||
) : KotlinQuickFixAction<KtBinaryExpressionWithTypeRHS>(element) {
|
||||
|
||||
override fun getText() = "Replace cast with call to 'to$targetShortType()'"
|
||||
override fun getFamilyName() = "Replace cast with primitive conversion method"
|
||||
|
||||
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||
val element = element ?: return
|
||||
val factory = KtPsiFactory(element)
|
||||
val replaced = element.replaced(factory.createExpressionByPattern("$0.to$1()", element.left, targetShortType))
|
||||
|
||||
editor?.caretModel?.moveToOffset(replaced.endOffset)
|
||||
}
|
||||
|
||||
companion object Factory : KotlinSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
||||
val element = CAST_NEVER_SUCCEEDS.cast(diagnostic).psiElement as? KtOperationReferenceExpression ?: return null
|
||||
val binaryExpression = element.parent as? KtBinaryExpressionWithTypeRHS ?: return null
|
||||
|
||||
val context = binaryExpression.analyze()
|
||||
|
||||
val expressionType = binaryExpression.left.getType(context) ?: return null
|
||||
if (!expressionType.isPrimitiveNumberType()) return null
|
||||
|
||||
val castType = binaryExpression.right?.createTypeBinding(context)?.type ?: return null
|
||||
if (!castType.isPrimitiveNumberType()) return null
|
||||
|
||||
return ReplacePrimitiveCastWithNumberConversionFix(binaryExpression, SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(castType))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Replace cast with call to 'toInt()'" "false"
|
||||
// WARNING: Cast can never succeed
|
||||
|
||||
fun foo() {
|
||||
val a = true as<caret> Int
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Replace cast with call to 'toShort()'" "true"
|
||||
|
||||
fun foo(c: Char) {
|
||||
val a = c as<caret> Short
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Replace cast with call to 'toShort()'" "true"
|
||||
|
||||
fun foo(c: Char) {
|
||||
val a = c.toShort()<caret>
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Replace cast with call to 'toByte()'" "true"
|
||||
|
||||
fun foo() {
|
||||
val a = 1 as<caret> Byte
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Replace cast with call to 'toByte()'" "true"
|
||||
|
||||
fun foo() {
|
||||
val a = 1.toByte()<caret>
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Replace cast with call to 'toFloat()'" "true"
|
||||
|
||||
fun foo() {
|
||||
val a = 1L as<caret> Float
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Replace cast with call to 'toFloat()'" "true"
|
||||
|
||||
fun foo() {
|
||||
val a = 1L.toFloat()<caret>
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2016 JetBrains s.r.o.
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -7363,6 +7363,39 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/primitiveCastToConversion")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class PrimitiveCastToConversion extends AbstractQuickFixTest {
|
||||
public void testAllFilesPresentInPrimitiveCastToConversion() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/primitiveCastToConversion"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("boolean.kt")
|
||||
public void testBoolean() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/primitiveCastToConversion/boolean.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("charToShort.kt")
|
||||
public void testCharToShort() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/primitiveCastToConversion/charToShort.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("intToByte.kt")
|
||||
public void testIntToByte() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/primitiveCastToConversion/intToByte.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("longToFloat.kt")
|
||||
public void testLongToFloat() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/primitiveCastToConversion/longToFloat.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/properties")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user