Add quickfix for replacing deprecated annotations
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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.codeInsight.intention.IntentionAction
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiWhiteSpace
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.idea.quickfix.quickfixUtil.createIntentionFactory
|
||||
import org.jetbrains.kotlin.idea.quickfix.quickfixUtil.createIntentionForFirstParentOfType
|
||||
import org.jetbrains.kotlin.psi.JetAnnotation
|
||||
import org.jetbrains.kotlin.psi.JetFile
|
||||
import org.jetbrains.kotlin.psi.JetPsiFactory
|
||||
|
||||
public class DeprecatedAnnotationSyntaxFix(element: JetAnnotation) : JetIntentionAction<JetAnnotation>(element) {
|
||||
override fun getFamilyName(): String = "Replace with '@' annotations"
|
||||
override fun getText(): String = "Replace with '@' annotations"
|
||||
|
||||
override fun invoke(project: Project, editor: Editor?, file: JetFile?) = replaceWithAtAnnotationEntries(element)
|
||||
|
||||
companion object : JetSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic): IntentionAction? =
|
||||
diagnostic.createIntentionForFirstParentOfType(::DeprecatedAnnotationSyntaxFix)
|
||||
|
||||
public fun createWholeProjectFixFactory(): JetSingleIntentionActionFactory = createIntentionFactory {
|
||||
JetWholeProjectForEachElementOfTypeFix.createByPredicate<JetAnnotation>(
|
||||
predicate = { it.isDeprecated() },
|
||||
taskProcessor = { replaceWithAtAnnotationEntries(it) },
|
||||
modalTitle = "Replacing deprecated annotations syntax",
|
||||
name = "Replace with '@' annotations in whole project",
|
||||
familyName = "Replace with '@' annotations in whole project"
|
||||
)
|
||||
}
|
||||
|
||||
private fun replaceWithAtAnnotationEntries(annotation: JetAnnotation) {
|
||||
val psiFactory = JetPsiFactory(annotation)
|
||||
|
||||
val parent = annotation.getParent()
|
||||
var prevElement: PsiElement = annotation
|
||||
|
||||
for (entry in annotation.getEntries()) {
|
||||
val newEntry = psiFactory.createAnnotationEntry("@" + entry.getText())
|
||||
val added = parent.addAfter(newEntry, prevElement)
|
||||
|
||||
if (prevElement != annotation) {
|
||||
parent.addBefore(psiFactory.createWhiteSpace(), added)
|
||||
}
|
||||
|
||||
prevElement = added
|
||||
}
|
||||
|
||||
annotation.delete()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -321,5 +321,8 @@ public class QuickFixRegistrar {
|
||||
|
||||
QuickFixes.factories.put(JAVA_LANG_CLASS_PARAMETER_IN_ANNOTATION, ReplaceJavaClassAsAnnotationParameterFix.Companion);
|
||||
QuickFixes.factories.put(JAVA_LANG_CLASS_PARAMETER_IN_ANNOTATION, ReplaceJavaClassAsAnnotationParameterFix.Companion.createWholeProjectFixFactory());
|
||||
|
||||
QuickFixes.factories.put(DEPRECATED_ANNOTATION_SYNTAX, DeprecatedAnnotationSyntaxFix.Companion);
|
||||
QuickFixes.factories.put(DEPRECATED_ANNOTATION_SYNTAX, DeprecatedAnnotationSyntaxFix.Companion.createWholeProjectFixFactory());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Replace with '@' annotations" "true"
|
||||
|
||||
annotation class Ann(val x: Int = 1)
|
||||
|
||||
@Ann @Ann(2) class MyClass
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Replace with '@' annotations" "true"
|
||||
|
||||
annotation class Ann(val x: Int = 1)
|
||||
|
||||
[Ann Ann(2)]<caret> class MyClass
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
[file: Ann]
|
||||
|
||||
@Ann class B [Ann(1)]()
|
||||
@@ -0,0 +1,31 @@
|
||||
// "Replace with '@' annotations in whole project" "true"
|
||||
|
||||
annotation class Ann(val x: Int = 1)
|
||||
|
||||
@Ann @Ann(2) class MyClass {
|
||||
@Ann(3) @Ann /* abc */fun foo(x: @Ann Int) {
|
||||
@Ann class Local {
|
||||
@Ann init {}
|
||||
|
||||
private @Ann @Ann(4) fun foo() {}
|
||||
}
|
||||
|
||||
@Ann var x = 1
|
||||
|
||||
1+ @Ann(5) 2
|
||||
|
||||
3+ @Ann(55)4
|
||||
|
||||
5+ @Ann @Ann(6)/* cde */7
|
||||
|
||||
label@@Ann(7) @Ann for (i in 1..100) {}
|
||||
}
|
||||
}
|
||||
@Ann(1) @Ann
|
||||
class Q1
|
||||
|
||||
@Ann(2)
|
||||
|
||||
@Ann(3)
|
||||
|
||||
fun bar() {}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
// "Replace with '@' annotations in whole project" "true"
|
||||
|
||||
annotation class Ann(val x: Int = 1)
|
||||
|
||||
[Ann Ann(2)]<caret> class MyClass {
|
||||
[Ann(3) Ann]/* abc */fun foo(x: [Ann] Int) {
|
||||
[Ann] class Local {
|
||||
[Ann] init {}
|
||||
|
||||
private[Ann Ann(4)]fun foo() {}
|
||||
}
|
||||
|
||||
[Ann]var x = 1
|
||||
|
||||
1+[Ann(5)] 2
|
||||
|
||||
3+[Ann(55)]4
|
||||
|
||||
5+[Ann Ann(6)]/* cde */7
|
||||
|
||||
label@[Ann(7) Ann] for (i in 1..100) {}
|
||||
}
|
||||
}
|
||||
[Ann(1) Ann]
|
||||
class Q1
|
||||
|
||||
[Ann(2)]
|
||||
|
||||
[Ann(3)]
|
||||
|
||||
fun bar() {}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
[file: Ann]
|
||||
|
||||
[Ann] class B [Ann(1)]()
|
||||
@@ -852,6 +852,21 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/migration"), Pattern.compile("^(\\w+)\\.before\\.Main\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/migration/bracketsAnnotations")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class BracketsAnnotations extends AbstractQuickFixMultiFileTest {
|
||||
public void testAllFilesPresentInBracketsAnnotations() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/migration/bracketsAnnotations"), Pattern.compile("^(\\w+)\\.before\\.Main\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("manyFilesMuitliple.before.Main.kt")
|
||||
public void testManyFilesMuitliple() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/bracketsAnnotations/manyFilesMuitliple.before.Main.kt");
|
||||
doTestWithExtraFile(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/migration/lambdaSyntax")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
@@ -3016,6 +3016,21 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/migration"), Pattern.compile("^before(\\w+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/migration/bracketsAnnotations")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class BracketsAnnotations extends AbstractQuickFixTest {
|
||||
public void testAllFilesPresentInBracketsAnnotations() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/migration/bracketsAnnotations"), Pattern.compile("^before(\\w+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("beforeBasic.kt")
|
||||
public void testBasic() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/bracketsAnnotations/beforeBasic.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/migration/lambdaSyntax")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user