AddJvmNameAnnotationFix: fix case with top-level functions

#KT-17209 Fixed
This commit is contained in:
Dmitry Gridin
2020-04-24 18:50:19 +07:00
parent f494b4ce11
commit c91089be59
4 changed files with 50 additions and 5 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -16,7 +16,6 @@ import org.jetbrains.kotlin.idea.util.addAnnotation
import org.jetbrains.kotlin.idea.util.findAnnotation
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
class AddJvmNameAnnotationFix(element: KtElement, private val jvmName: String) : KotlinQuickFixAction<KtElement>(element) {
override fun getText(): String = if (element is KtAnnotationEntry) {
@@ -50,9 +49,13 @@ class AddJvmNameAnnotationFix(element: KtElement, private val jvmName: String) :
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val function = diagnostic.psiElement as? KtNamedFunction ?: return null
val functionName = function.name ?: return null
val classOrObjectBody = function.containingClassOrObject?.body ?: return null
val nameValidator =
NewDeclarationNameValidator(classOrObjectBody, function, NewDeclarationNameValidator.Target.FUNCTIONS_AND_CLASSES)
val containingDeclaration = function.parent ?: return null
val nameValidator = NewDeclarationNameValidator(
containingDeclaration,
function,
NewDeclarationNameValidator.Target.FUNCTIONS_AND_CLASSES
)
val jvmName = KotlinNameSuggester.suggestNameByName(functionName, nameValidator)
return AddJvmNameAnnotationFix(function.findAnnotation(JVM_NAME_FQ_NAME) ?: function, jvmName)
}
+18
View File
@@ -0,0 +1,18 @@
// "Add '@JvmName' annotation" "true"
// WITH_RUNTIME
fun <caret>bar(foo: List<String>): String {
return "1"
}
fun bar(foo: List<Int>): String {
return "2"
}
fun bar1(foo: List<Int>): String {
return "3"
}
fun bar2(foo: List<Int>): String {
return "4"
}
@@ -0,0 +1,19 @@
// "Add '@JvmName' annotation" "true"
// WITH_RUNTIME
@JvmName("bar3")
fun <caret>bar(foo: List<String>): String {
return "1"
}
fun bar(foo: List<Int>): String {
return "2"
}
fun bar1(foo: List<Int>): String {
return "3"
}
fun bar2(foo: List<Int>): String {
return "4"
}
@@ -888,6 +888,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
public void testHasAnnotation2() throws Exception {
runTest("idea/testData/quickfix/addJvmNameAnnotation/hasAnnotation2.kt");
}
@TestMetadata("topLevel.kt")
public void testTopLevel() throws Exception {
runTest("idea/testData/quickfix/addJvmNameAnnotation/topLevel.kt");
}
}
@TestMetadata("idea/testData/quickfix/addNewLineAfterAnnotations")