Implement quick fix for converting primitive types
#KT-10476 Fixed
This commit is contained in:
committed by
Dmitry Jemerov
parent
b328f37495
commit
48310d66da
@@ -55,6 +55,7 @@ fun KotlinType.isUnit(): Boolean = KotlinBuiltIns.isUnit(this)
|
||||
fun KotlinType.isAnyOrNullableAny(): Boolean = KotlinBuiltIns.isAnyOrNullableAny(this)
|
||||
fun KotlinType.isNullableAny(): Boolean = KotlinBuiltIns.isNullableAny(this)
|
||||
fun KotlinType.isBoolean(): Boolean = KotlinBuiltIns.isBoolean(this)
|
||||
fun KotlinType.isPrimitiveNumberType(): Boolean = KotlinBuiltIns.isPrimitiveType(this) && !isBoolean()
|
||||
fun KotlinType.isBooleanOrNullableBoolean(): Boolean = KotlinBuiltIns.isBooleanOrNullableBoolean(this)
|
||||
|
||||
fun KotlinType.isTypeParameter(): Boolean = TypeUtils.isTypeParameter(this)
|
||||
@@ -71,6 +72,7 @@ fun KotlinType.isSubtypeOf(superType: KotlinType): Boolean = KotlinTypeChecker.D
|
||||
|
||||
fun KotlinType.cannotBeReified(): Boolean =
|
||||
KotlinBuiltIns.isNothingOrNullableNothing(this) || this.isDynamic() || this.isCaptured()
|
||||
|
||||
fun KotlinType.unsafeAsReifiedArgument(): Boolean = arguments.any { !it.isStarProjection }
|
||||
|
||||
fun TypeProjection.substitute(doSubstitute: (KotlinType) -> KotlinType): TypeProjection {
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2010-2016 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.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiFile
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.core.replaced
|
||||
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
|
||||
import org.jetbrains.kotlin.psi.KtExpression
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||
import org.jetbrains.kotlin.psi.createExpressionByPattern
|
||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
import org.jetbrains.kotlin.types.typeUtil.isPrimitiveNumberType
|
||||
|
||||
class NumberConversionFix(element: KtExpression, private val type: KotlinType) : KotlinQuickFixAction<KtExpression>(element) {
|
||||
|
||||
override fun isAvailable(project: Project, editor: Editor?, file: PsiFile): Boolean {
|
||||
if (!super.isAvailable(project, editor, file)) return false
|
||||
|
||||
val expressionType = element.analyze(BodyResolveMode.PARTIAL).getType(element) ?: return false
|
||||
return expressionType != type && expressionType.isPrimitiveNumberType() && type.isPrimitiveNumberType()
|
||||
}
|
||||
|
||||
override fun getFamilyName() = "Insert number conversion"
|
||||
override fun getText() = "Convert expression to '${IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(type)}'"
|
||||
|
||||
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||
val renderedType = IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(type)
|
||||
val expressionToInsert = KtPsiFactory(file).createExpressionByPattern("$0.to$1()", element, renderedType)
|
||||
val newExpression = element.replaced(expressionToInsert)
|
||||
editor?.caretModel?.moveToOffset(newExpression.endOffset)
|
||||
}
|
||||
}
|
||||
@@ -93,6 +93,10 @@ public class QuickFixFactoryForTypeMismatchError extends KotlinIntentionActionsF
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
if (TypeUtilsKt.isPrimitiveNumberType(expressionType) && TypeUtilsKt.isPrimitiveNumberType(expectedType)) {
|
||||
actions.add(new NumberConversionFix(expression, expectedType));
|
||||
}
|
||||
|
||||
// We don't want to cast a cast or type-asserted expression:
|
||||
if (!(expression instanceof KtBinaryExpressionWithTypeRHS) && !(expression.getParent() instanceof KtBinaryExpressionWithTypeRHS)) {
|
||||
actions.add(new CastExpressionFix(expression, expectedType));
|
||||
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// "Convert expression to 'Float'" "true"
|
||||
fun foo() {
|
||||
bar(1 + 3L<caret>)
|
||||
}
|
||||
|
||||
fun bar(l: Float) {
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// "Convert expression to 'Float'" "true"
|
||||
fun foo() {
|
||||
bar((1 + 3L).toFloat()<caret>)
|
||||
}
|
||||
|
||||
fun bar(l: Float) {
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// "Convert expression to 'Int'" "true"
|
||||
fun foo() {
|
||||
bar(1L<caret>)
|
||||
}
|
||||
|
||||
fun bar(l: Int) {
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// "Convert expression to 'Int'" "true"
|
||||
fun foo() {
|
||||
bar(1L.toInt()<caret>)
|
||||
}
|
||||
|
||||
fun bar(l: Int) {
|
||||
}
|
||||
@@ -7444,6 +7444,27 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/typeMismatch/numberConversion")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class NumberConversion extends AbstractQuickFixTest {
|
||||
public void testAllFilesPresentInNumberConversion() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/typeMismatch/numberConversion"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("convertBinaryExpression.kt")
|
||||
public void testConvertBinaryExpression() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/numberConversion/convertBinaryExpression.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("convertLiteral.kt")
|
||||
public void testConvertLiteral() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/numberConversion/convertLiteral.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/typeMismatch/parameterTypeMismatch")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user