Add quickfix for "A type annotation is required on a value parameter" #KT-12804 Fixed

This commit is contained in:
Kirill Rakhman
2016-08-20 16:02:51 +03:00
committed by Mikhail Glukhikh
parent 2dd4194a79
commit 1fbd6678a5
6 changed files with 101 additions and 0 deletions
@@ -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<KtParameter>(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)
}
}
}
@@ -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)
}
}
@@ -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<caret>)
@@ -0,0 +1,3 @@
// "Add type 'Int' to parameter 'bar'" "true"
class Foo(val bar = 10<caret>)
@@ -0,0 +1,3 @@
// "Add type 'Int' to parameter 'bar'" "true"
class Foo(val bar: Int = 10<caret>)
@@ -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)