Better replacement for annotations with parameters in ReplaceWIth pattern but still lot of safety pre-checks to be added

This commit is contained in:
Valentin Kipyatkov
2015-10-09 22:48:21 +03:00
parent 4290601807
commit 28c950ffbe
4 changed files with 36 additions and 4 deletions
@@ -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!!
@@ -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
@@ -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");