Skip to content

Commit 1fbd667

Browse files
cypressiousmglukhikh
authored andcommitted
Add quickfix for "A type annotation is required on a value parameter" #KT-12804 Fixed
1 parent 2dd4194 commit 1fbd667

File tree

6 files changed

+101
-0
lines changed

6 files changed

+101
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2010-2016 JetBrains s.r.o.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.jetbrains.kotlin.idea.quickfix
18+
19+
import com.intellij.openapi.editor.Editor
20+
import com.intellij.openapi.project.Project
21+
import com.intellij.psi.PsiFile
22+
import org.jetbrains.kotlin.diagnostics.Diagnostic
23+
import org.jetbrains.kotlin.diagnostics.Errors
24+
import org.jetbrains.kotlin.idea.caches.resolve.analyze
25+
import org.jetbrains.kotlin.idea.core.ShortenReferences
26+
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
27+
import org.jetbrains.kotlin.psi.KtFile
28+
import org.jetbrains.kotlin.psi.KtParameter
29+
import org.jetbrains.kotlin.psi.KtPsiFactory
30+
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
31+
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
32+
33+
class AddTypeAnnotationToValueParameterFix(element: KtParameter) : KotlinQuickFixAction<KtParameter>(element) {
34+
35+
val typeNameShort : String?
36+
val typeName: String?
37+
38+
init {
39+
val defaultValue = element.defaultValue
40+
val type = defaultValue?.getType(defaultValue.analyze(BodyResolveMode.PARTIAL))
41+
42+
typeNameShort = type?.let { IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(it) }
43+
typeName = type?.let { IdeDescriptorRenderers.SOURCE_CODE.renderType(it) }
44+
}
45+
46+
override fun isAvailable(project: Project, editor: Editor?, file: PsiFile): Boolean {
47+
return element.typeReference == null && typeNameShort != null
48+
}
49+
50+
override fun getFamilyName() = "Add type annotation"
51+
override fun getText() = "Add type '$typeNameShort' to parameter '${element.name}'"
52+
53+
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
54+
if (typeName != null) {
55+
element.typeReference = KtPsiFactory(element).createType(typeName)
56+
ShortenReferences.DEFAULT.process(element)
57+
}
58+
}
59+
60+
companion object Factory : KotlinSingleIntentionActionFactory() {
61+
override fun createAction(diagnostic: Diagnostic): AddTypeAnnotationToValueParameterFix? {
62+
val element = Errors.VALUE_PARAMETER_WITH_NO_TYPE_ANNOTATION.cast(diagnostic).psiElement
63+
if (element.defaultValue == null) return null
64+
return AddTypeAnnotationToValueParameterFix(element)
65+
}
66+
}
67+
}

idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,5 +404,7 @@ class QuickFixRegistrar : QuickFixContributor {
404404
WRONG_LONG_SUFFIX.registerFactory(WrongLongSuffixFix)
405405

406406
REIFIED_TYPE_PARAMETER_NO_INLINE.registerFactory(AddInlineToFunctionWithReifiedFix)
407+
408+
VALUE_PARAMETER_WITH_NO_TYPE_ANNOTATION.registerFactory(AddTypeAnnotationToValueParameterFix)
407409
}
408410
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// "Add type 'Int' to parameter 'bar'" "false"
2+
// ERROR: A type annotation is required on a value parameter
3+
// ACTION: Create test
4+
5+
class Foo(val bar<caret>)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// "Add type 'Int' to parameter 'bar'" "true"
2+
3+
class Foo(val bar = 10<caret>)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// "Add type 'Int' to parameter 'bar'" "true"
2+
3+
class Foo(val bar: Int = 10<caret>)

idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,27 @@ public void testUnqualifiedMapOneArg() throws Exception {
572572
}
573573
}
574574

575+
@TestMetadata("idea/testData/quickfix/addTypeAnnotationToValueParameter")
576+
@TestDataPath("$PROJECT_ROOT")
577+
@RunWith(JUnit3RunnerWithInners.class)
578+
public static class AddTypeAnnotationToValueParameter extends AbstractQuickFixTest {
579+
public void testAllFilesPresentInAddTypeAnnotationToValueParameter() throws Exception {
580+
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/addTypeAnnotationToValueParameter"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
581+
}
582+
583+
@TestMetadata("noDefaultValue.kt")
584+
public void testNoDefaultValue() throws Exception {
585+
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addTypeAnnotationToValueParameter/noDefaultValue.kt");
586+
doTest(fileName);
587+
}
588+
589+
@TestMetadata("simple.kt")
590+
public void testSimple() throws Exception {
591+
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addTypeAnnotationToValueParameter/simple.kt");
592+
doTest(fileName);
593+
}
594+
}
595+
575596
@TestMetadata("idea/testData/quickfix/addValVar")
576597
@TestDataPath("$PROJECT_ROOT")
577598
@RunWith(JUnit3RunnerWithInners.class)

0 commit comments

Comments
 (0)