Add 'Add JvmName annotation' quick fix for CONFLICTING_JVM_DECLARATIONS

#KT-17209
This commit is contained in:
Toshiaki Kameyama
2019-09-09 17:46:19 +09:00
committed by Dmitry Gridin
parent 9bc51be287
commit f494b4ce11
11 changed files with 224 additions and 1 deletions
@@ -2206,4 +2206,5 @@ kotlin.script.definitions.model.name.pattern.extension=Pattern/Extension
kotlin.script.definitions.model.name.name=Name
codestyle.name.kotlin=Kotlin
add.missing.class.keyword=Add missing 'class' keyword
fix.move.typealias.to.top.level=Move typealias to top level
fix.move.typealias.to.top.level=Move typealias to top level
fix.change.jvm.name=Change JVM name
@@ -0,0 +1,60 @@
/*
* Copyright 2010-2019 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.
*/
package org.jetbrains.kotlin.idea.quickfix
import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.idea.core.KotlinNameSuggester
import org.jetbrains.kotlin.idea.core.NewDeclarationNameValidator
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) {
KotlinBundle.message("fix.change.jvm.name")
} else {
KotlinBundle.message("fix.add.annotation.text.self", JVM_NAME_FQ_NAME.shortName())
}
override fun getFamilyName(): String = text
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
val element = element ?: return
when (element) {
is KtAnnotationEntry -> {
val argList = element.valueArgumentList
val newArgList = KtPsiFactory(element).createCallArguments("(\"$jvmName\")")
if (argList != null) {
argList.replace(newArgList)
} else {
element.addAfter(newArgList, element.lastChild)
}
}
is KtFunction ->
element.addAnnotation(JVM_NAME_FQ_NAME, annotationInnerText = "\"$jvmName\"")
}
}
companion object : KotlinSingleIntentionActionFactory() {
private val JVM_NAME_FQ_NAME = FqName("kotlin.jvm.JvmName")
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 jvmName = KotlinNameSuggester.suggestNameByName(functionName, nameValidator)
return AddJvmNameAnnotationFix(function.findAnnotation(JVM_NAME_FQ_NAME) ?: function, jvmName)
}
}
}
@@ -646,5 +646,7 @@ class QuickFixRegistrar : QuickFixContributor {
FUN_INTERFACE_WRONG_COUNT_OF_ABSTRACT_MEMBERS.registerFactory(RemoveModifierFix.createRemoveFunFromInterfaceFactory())
TOPLEVEL_TYPEALIASES_ONLY.registerFactory(MoveTypeAliasToTopLevelFix)
CONFLICTING_JVM_DECLARATIONS.registerFactory(AddJvmNameAnnotationFix)
}
}
+19
View File
@@ -0,0 +1,19 @@
// "Add '@JvmName' annotation" "true"
// WITH_RUNTIME
class Foo {
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,20 @@
// "Add '@JvmName' annotation" "true"
// WITH_RUNTIME
class Foo {
@JvmName("bar3")
fun 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,20 @@
// "Change JVM name" "true"
// WITH_RUNTIME
class Foo {
@JvmName("bar1")
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,20 @@
// "Change JVM name" "true"
// WITH_RUNTIME
class Foo {
@JvmName("bar3")
fun 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,20 @@
// "Change JVM name" "true"
// WITH_RUNTIME
class Foo {
@JvmName
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,20 @@
// "Change JVM name" "true"
// WITH_RUNTIME
class Foo {
@JvmName("bar3")
fun 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"
}
}
@@ -207,6 +207,19 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
}
}
@TestMetadata("idea/testData/quickfix/addJvmNameAnnotation")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class AddJvmNameAnnotation extends AbstractQuickFixMultiFileTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTestWithExtraFile, this, testDataFilePath);
}
public void testAllFilesPresentInAddJvmNameAnnotation() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/quickfix/addJvmNameAnnotation"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), null, true);
}
}
@TestMetadata("idea/testData/quickfix/addNewLineAfterAnnotations")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -862,6 +862,34 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
}
}
@TestMetadata("idea/testData/quickfix/addJvmNameAnnotation")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class AddJvmNameAnnotation extends AbstractQuickFixTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInAddJvmNameAnnotation() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/quickfix/addJvmNameAnnotation"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), null, true);
}
@TestMetadata("basic.kt")
public void testBasic() throws Exception {
runTest("idea/testData/quickfix/addJvmNameAnnotation/basic.kt");
}
@TestMetadata("hasAnnotation.kt")
public void testHasAnnotation() throws Exception {
runTest("idea/testData/quickfix/addJvmNameAnnotation/hasAnnotation.kt");
}
@TestMetadata("hasAnnotation2.kt")
public void testHasAnnotation2() throws Exception {
runTest("idea/testData/quickfix/addJvmNameAnnotation/hasAnnotation2.kt");
}
}
@TestMetadata("idea/testData/quickfix/addNewLineAfterAnnotations")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)