AddJvmNameAnnotationFix: fix case with top-level functions
#KT-17209 Fixed
This commit is contained in:
@@ -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.
|
* 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.idea.util.findAnnotation
|
||||||
import org.jetbrains.kotlin.name.FqName
|
import org.jetbrains.kotlin.name.FqName
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
|
|
||||||
|
|
||||||
class AddJvmNameAnnotationFix(element: KtElement, private val jvmName: String) : KotlinQuickFixAction<KtElement>(element) {
|
class AddJvmNameAnnotationFix(element: KtElement, private val jvmName: String) : KotlinQuickFixAction<KtElement>(element) {
|
||||||
override fun getText(): String = if (element is KtAnnotationEntry) {
|
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? {
|
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
||||||
val function = diagnostic.psiElement as? KtNamedFunction ?: return null
|
val function = diagnostic.psiElement as? KtNamedFunction ?: return null
|
||||||
val functionName = function.name ?: return null
|
val functionName = function.name ?: return null
|
||||||
val classOrObjectBody = function.containingClassOrObject?.body ?: return null
|
val containingDeclaration = function.parent ?: return null
|
||||||
val nameValidator =
|
val nameValidator = NewDeclarationNameValidator(
|
||||||
NewDeclarationNameValidator(classOrObjectBody, function, NewDeclarationNameValidator.Target.FUNCTIONS_AND_CLASSES)
|
containingDeclaration,
|
||||||
|
function,
|
||||||
|
NewDeclarationNameValidator.Target.FUNCTIONS_AND_CLASSES
|
||||||
|
)
|
||||||
|
|
||||||
val jvmName = KotlinNameSuggester.suggestNameByName(functionName, nameValidator)
|
val jvmName = KotlinNameSuggester.suggestNameByName(functionName, nameValidator)
|
||||||
return AddJvmNameAnnotationFix(function.findAnnotation(JVM_NAME_FQ_NAME) ?: function, jvmName)
|
return AddJvmNameAnnotationFix(function.findAnnotation(JVM_NAME_FQ_NAME) ?: function, jvmName)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 {
|
public void testHasAnnotation2() throws Exception {
|
||||||
runTest("idea/testData/quickfix/addJvmNameAnnotation/hasAnnotation2.kt");
|
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")
|
@TestMetadata("idea/testData/quickfix/addNewLineAfterAnnotations")
|
||||||
|
|||||||
Reference in New Issue
Block a user