Better replacement for annotations with parameters in ReplaceWIth pattern but still lot of safety pre-checks to be added
This commit is contained in:
+10
-4
@@ -153,7 +153,14 @@ private fun performCallReplacement(
|
||||
|
||||
commentSaver.restore(resultRange)
|
||||
|
||||
return resultRange.last as JetElement
|
||||
var resultElement = resultRange.last as JetElement
|
||||
|
||||
if (resultElement is JetAnnotationEntry) { // unwrap "@Dummy(...)"
|
||||
val text = resultElement.valueArguments.single().getArgumentExpression()!!.text
|
||||
resultElement = resultElement.replaced(JetPsiFactory(project).createAnnotationEntry("@" + text)) //TODO: what if it does not work?
|
||||
}
|
||||
|
||||
return resultElement
|
||||
}
|
||||
|
||||
private fun JetElement.replaceCallElement(generatedExpression: JetExpression): JetElement {
|
||||
@@ -161,9 +168,8 @@ private fun JetElement.replaceCallElement(generatedExpression: JetExpression): J
|
||||
is JetExpression -> return replace(generatedExpression) as JetExpression
|
||||
|
||||
is JetAnnotationEntry -> {
|
||||
//TODO: bad techinique!!
|
||||
val annotationEntry = JetPsiFactory(project).createAnnotationEntry("@" + generatedExpression.text)
|
||||
return replace(annotationEntry) as JetElement
|
||||
val dummyAnnotation = createByPattern("@Dummy($0)", generatedExpression) { JetPsiFactory(project).createAnnotationEntry(it) }
|
||||
return replace(dummyAnnotation) as JetElement
|
||||
}
|
||||
|
||||
else -> throw UnsupportedOperationException() //TODO: check it before!!
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Replace with 'Bar(p, "")'" "true"
|
||||
|
||||
package test
|
||||
|
||||
@Deprecated("Replace with bar", ReplaceWith("Bar(p, \"\")"))
|
||||
annotation class Foo(val p: Int)
|
||||
|
||||
annotation class Bar(val p: Int, val s: String)
|
||||
|
||||
@Foo<caret>(p = 1) class C
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Replace with 'Bar(p, "")'" "true"
|
||||
|
||||
package test
|
||||
|
||||
@Deprecated("Replace with bar", ReplaceWith("Bar(p, \"\")"))
|
||||
annotation class Foo(val p: Int)
|
||||
|
||||
annotation class Bar(val p: Int, val s: String)
|
||||
|
||||
@Bar(p = 1, s = "") class C
|
||||
@@ -3435,6 +3435,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("annotationKeepNamedArgs.kt")
|
||||
public void testAnnotationKeepNamedArgs() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/deprecatedSymbolUsage/classUsages/annotationKeepNamedArgs.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("constructorUsage1.kt")
|
||||
public void testConstructorUsage1() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/deprecatedSymbolUsage/classUsages/constructorUsage1.kt");
|
||||
|
||||
Reference in New Issue
Block a user