diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/MakeTypeParameterReifiedAndFunctionInlineFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/MakeTypeParameterReifiedAndFunctionInlineFix.kt new file mode 100644 index 00000000000..a7d1883eb75 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/MakeTypeParameterReifiedAndFunctionInlineFix.kt @@ -0,0 +1,60 @@ +/* + * 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.openapi.editor.Editor +import com.intellij.openapi.project.Project +import org.jetbrains.kotlin.diagnostics.Diagnostic +import org.jetbrains.kotlin.diagnostics.Errors +import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor +import org.jetbrains.kotlin.lexer.KtTokens +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.psi.KtNamedFunction +import org.jetbrains.kotlin.psi.KtTypeParameter +import org.jetbrains.kotlin.psi.KtTypeReference +import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType + +class MakeTypeParameterReifiedAndFunctionInlineFix( + typeReference: KtTypeReference, + function: KtNamedFunction, + private val typeParameter: KtTypeParameter +) : KotlinQuickFixAction(typeReference) { + + private val inlineFix = AddInlineToFunctionWithReifiedFix(function) + + override fun getText() = "Make type parameter reified and function inline" + + override fun getFamilyName() = text + + override fun invoke(project: Project, editor: Editor?, file: KtFile) { + inlineFix.invoke(project, editor, file) + AddModifierFix(typeParameter, KtTokens.REIFIED_KEYWORD).invoke(project, editor, file) + } + + companion object : KotlinSingleIntentionActionFactory() { + override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction? { + val element = Errors.CANNOT_CHECK_FOR_ERASED.cast(diagnostic) + val typeReference = element.psiElement as? KtTypeReference ?: return null + val function = typeReference.getStrictParentOfType() ?: return null + val typeParameter = function.typeParameterList?.parameters?.firstOrNull { + it.descriptor == element.a.constructor.declarationDescriptor + } ?: return null + return MakeTypeParameterReifiedAndFunctionInlineFix(typeReference, function, typeParameter) + } + } + +} diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index 05471726eae..22d155675b7 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt @@ -459,6 +459,8 @@ class QuickFixRegistrar : QuickFixContributor { TYPE_PARAMETER_AS_REIFIED.registerFactory(AddReifiedToTypeParameterOfFunctionFix) + CANNOT_CHECK_FOR_ERASED.registerFactory(MakeTypeParameterReifiedAndFunctionInlineFix) + TOO_MANY_CHARACTERS_IN_CHARACTER_LITERAL.registerFactory(TooLongCharLiteralToStringFix) UNUSED_VALUE.registerFactory(RemoveUnusedValueFix) diff --git a/idea/testData/quickfix/MakeTypeParameterReifiedAndFunctionInline/noTypeParameter.kt b/idea/testData/quickfix/MakeTypeParameterReifiedAndFunctionInline/noTypeParameter.kt new file mode 100644 index 00000000000..70465923196 --- /dev/null +++ b/idea/testData/quickfix/MakeTypeParameterReifiedAndFunctionInline/noTypeParameter.kt @@ -0,0 +1,6 @@ +// "Make type parameter reified and function inline" "false" +// ACTION: Change type arguments to <*> +// ACTION: Convert to block body +// ACTION: Introduce local variable +// ERROR: Cannot check for instance of erased type: List +fun test(a: List) = a is List \ No newline at end of file diff --git a/idea/testData/quickfix/MakeTypeParameterReifiedAndFunctionInline/noTypeParameter2.kt b/idea/testData/quickfix/MakeTypeParameterReifiedAndFunctionInline/noTypeParameter2.kt new file mode 100644 index 00000000000..178f43dc687 --- /dev/null +++ b/idea/testData/quickfix/MakeTypeParameterReifiedAndFunctionInline/noTypeParameter2.kt @@ -0,0 +1,6 @@ +// "Make type parameter reified and function inline" "false" +// ACTION: Change type arguments to <*> +// ACTION: Convert to block body +// ACTION: Introduce local variable +// ERROR: Cannot check for instance of erased type: List +fun test(a: List) = a is List \ No newline at end of file diff --git a/idea/testData/quickfix/MakeTypeParameterReifiedAndFunctionInline/typeParameter.kt b/idea/testData/quickfix/MakeTypeParameterReifiedAndFunctionInline/typeParameter.kt new file mode 100644 index 00000000000..7c53208335a --- /dev/null +++ b/idea/testData/quickfix/MakeTypeParameterReifiedAndFunctionInline/typeParameter.kt @@ -0,0 +1,2 @@ +// "Make type parameter reified and function inline" "true" +fun test(a: String) = a is T \ No newline at end of file diff --git a/idea/testData/quickfix/MakeTypeParameterReifiedAndFunctionInline/typeParameter.kt.after b/idea/testData/quickfix/MakeTypeParameterReifiedAndFunctionInline/typeParameter.kt.after new file mode 100644 index 00000000000..df96043aee3 --- /dev/null +++ b/idea/testData/quickfix/MakeTypeParameterReifiedAndFunctionInline/typeParameter.kt.after @@ -0,0 +1,2 @@ +// "Make type parameter reified and function inline" "true" +inline fun test(a: String) = a is T \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index 89740fe89bc..000e5eb5142 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -6717,6 +6717,33 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { } } + @TestMetadata("idea/testData/quickfix/makeTypeParameterReifiedAndFunctionInline") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class MakeTypeParameterReifiedAndFunctionInline extends AbstractQuickFixTest { + public void testAllFilesPresentInMakeTypeParameterReifiedAndFunctionInline() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/makeTypeParameterReifiedAndFunctionInline"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("noTypeParameter.kt") + public void testNoTypeParameter() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/makeTypeParameterReifiedAndFunctionInline/noTypeParameter.kt"); + doTest(fileName); + } + + @TestMetadata("noTypeParameter2.kt") + public void testNoTypeParameter2() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/makeTypeParameterReifiedAndFunctionInline/noTypeParameter2.kt"); + doTest(fileName); + } + + @TestMetadata("typeParameter.kt") + public void testTypeParameter() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/makeTypeParameterReifiedAndFunctionInline/typeParameter.kt"); + doTest(fileName); + } + } + @TestMetadata("idea/testData/quickfix/memberVisibilityCanBePrivate") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)