From 1fbd6678a5bc45f8aa4754d1f81be236e92b5abc Mon Sep 17 00:00:00 2001 From: Kirill Rakhman Date: Sat, 20 Aug 2016 16:02:51 +0300 Subject: [PATCH] Add quickfix for "A type annotation is required on a value parameter" #KT-12804 Fixed --- .../AddTypeAnnotationToValueParameterFix.kt | 67 +++++++++++++++++++ .../kotlin/idea/quickfix/QuickFixRegistrar.kt | 2 + .../noDefaultValue.kt | 5 ++ .../simple.kt | 3 + .../simple.kt.after | 3 + .../idea/quickfix/QuickFixTestGenerated.java | 21 ++++++ 6 files changed, 101 insertions(+) create mode 100644 idea/src/org/jetbrains/kotlin/idea/quickfix/AddTypeAnnotationToValueParameterFix.kt create mode 100644 idea/testData/quickfix/addTypeAnnotationToValueParameter/noDefaultValue.kt create mode 100644 idea/testData/quickfix/addTypeAnnotationToValueParameter/simple.kt create mode 100644 idea/testData/quickfix/addTypeAnnotationToValueParameter/simple.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/AddTypeAnnotationToValueParameterFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/AddTypeAnnotationToValueParameterFix.kt new file mode 100644 index 00000000000..0fb3aa77a17 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/AddTypeAnnotationToValueParameterFix.kt @@ -0,0 +1,67 @@ +/* + * 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.diagnostics.Diagnostic +import org.jetbrains.kotlin.diagnostics.Errors +import org.jetbrains.kotlin.idea.caches.resolve.analyze +import org.jetbrains.kotlin.idea.core.ShortenReferences +import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.psi.KtParameter +import org.jetbrains.kotlin.psi.KtPsiFactory +import org.jetbrains.kotlin.resolve.calls.callUtil.getType +import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode + +class AddTypeAnnotationToValueParameterFix(element: KtParameter) : KotlinQuickFixAction(element) { + + val typeNameShort : String? + val typeName: String? + + init { + val defaultValue = element.defaultValue + val type = defaultValue?.getType(defaultValue.analyze(BodyResolveMode.PARTIAL)) + + typeNameShort = type?.let { IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(it) } + typeName = type?.let { IdeDescriptorRenderers.SOURCE_CODE.renderType(it) } + } + + override fun isAvailable(project: Project, editor: Editor?, file: PsiFile): Boolean { + return element.typeReference == null && typeNameShort != null + } + + override fun getFamilyName() = "Add type annotation" + override fun getText() = "Add type '$typeNameShort' to parameter '${element.name}'" + + override fun invoke(project: Project, editor: Editor?, file: KtFile) { + if (typeName != null) { + element.typeReference = KtPsiFactory(element).createType(typeName) + ShortenReferences.DEFAULT.process(element) + } + } + + companion object Factory : KotlinSingleIntentionActionFactory() { + override fun createAction(diagnostic: Diagnostic): AddTypeAnnotationToValueParameterFix? { + val element = Errors.VALUE_PARAMETER_WITH_NO_TYPE_ANNOTATION.cast(diagnostic).psiElement + if (element.defaultValue == null) return null + return AddTypeAnnotationToValueParameterFix(element) + } + } +} \ No newline at end of file diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index 5fae271190f..552c1a71858 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt @@ -404,5 +404,7 @@ class QuickFixRegistrar : QuickFixContributor { WRONG_LONG_SUFFIX.registerFactory(WrongLongSuffixFix) REIFIED_TYPE_PARAMETER_NO_INLINE.registerFactory(AddInlineToFunctionWithReifiedFix) + + VALUE_PARAMETER_WITH_NO_TYPE_ANNOTATION.registerFactory(AddTypeAnnotationToValueParameterFix) } } diff --git a/idea/testData/quickfix/addTypeAnnotationToValueParameter/noDefaultValue.kt b/idea/testData/quickfix/addTypeAnnotationToValueParameter/noDefaultValue.kt new file mode 100644 index 00000000000..3bd297d4c2e --- /dev/null +++ b/idea/testData/quickfix/addTypeAnnotationToValueParameter/noDefaultValue.kt @@ -0,0 +1,5 @@ +// "Add type 'Int' to parameter 'bar'" "false" +// ERROR: A type annotation is required on a value parameter +// ACTION: Create test + +class Foo(val bar) \ No newline at end of file diff --git a/idea/testData/quickfix/addTypeAnnotationToValueParameter/simple.kt b/idea/testData/quickfix/addTypeAnnotationToValueParameter/simple.kt new file mode 100644 index 00000000000..28faaa6b0e3 --- /dev/null +++ b/idea/testData/quickfix/addTypeAnnotationToValueParameter/simple.kt @@ -0,0 +1,3 @@ +// "Add type 'Int' to parameter 'bar'" "true" + +class Foo(val bar = 10) \ No newline at end of file diff --git a/idea/testData/quickfix/addTypeAnnotationToValueParameter/simple.kt.after b/idea/testData/quickfix/addTypeAnnotationToValueParameter/simple.kt.after new file mode 100644 index 00000000000..78d210dbf92 --- /dev/null +++ b/idea/testData/quickfix/addTypeAnnotationToValueParameter/simple.kt.after @@ -0,0 +1,3 @@ +// "Add type 'Int' to parameter 'bar'" "true" + +class Foo(val bar: Int = 10) \ 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 8247d06b1be..e0883f38d1b 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -572,6 +572,27 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { } } + @TestMetadata("idea/testData/quickfix/addTypeAnnotationToValueParameter") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class AddTypeAnnotationToValueParameter extends AbstractQuickFixTest { + public void testAllFilesPresentInAddTypeAnnotationToValueParameter() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/addTypeAnnotationToValueParameter"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); + } + + @TestMetadata("noDefaultValue.kt") + public void testNoDefaultValue() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addTypeAnnotationToValueParameter/noDefaultValue.kt"); + doTest(fileName); + } + + @TestMetadata("simple.kt") + public void testSimple() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addTypeAnnotationToValueParameter/simple.kt"); + doTest(fileName); + } + } + @TestMetadata("idea/testData/quickfix/addValVar") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)