DeprecatedCallableAddReplaceWithIntention: fixed for string templates in expression

This commit is contained in:
Valentin Kipyatkov
2015-05-29 19:04:59 +03:00
parent 07cb0a0b08
commit b012f22edc
6 changed files with 39 additions and 5 deletions
@@ -62,11 +62,27 @@ public class DeprecatedCallableAddReplaceWithIntention : JetSelfTargetingRangeIn
val annotationEntry = element.deprecatedAnnotationWithNoReplaceWith()!!
val psiFactory = JetPsiFactory(element)
val escapedText = replaceWith.expression
var escapedText = replaceWith.expression
.replace("\\", "\\\\")
.replace("\"", "\\\"")
//TODO: escape $!
// escape '$' if it's followed by a letter or '{'
if (escapedText.contains('$')) {
escapedText = StringBuilder {
var i = 0
val length = escapedText.length()
while (i < length) {
val c = escapedText[i++]
if (c == '$' && i < length) {
val c1 = escapedText[i]
if (c1.isJavaIdentifierStart() || c1 == '{') {
append('\\')
}
}
append(c)
}
}.toString()
}
val argumentText = StringBuilder {
append("kotlin.ReplaceWith(\"")
@@ -1,6 +1,6 @@
<caret>@deprecated("")
fun foo(p: Int) {
bar("\"$p\"\n1\r2\t3")
bar("\"\"\n1\r2\t3")
}
fun bar(s: String){}
@@ -1,6 +1,6 @@
@deprecated("", ReplaceWith("bar(\"\\\"$p\\\"\\n1\\r2\\t3\")"))
@deprecated("", ReplaceWith("bar(\"\\\"\\\"\\n1\\r2\\t3\")"))
fun foo(p: Int) {
bar("\"$p\"\n1\r2\t3")
bar("\"\"\n1\r2\t3")
}
fun bar(s: String){}
@@ -0,0 +1,6 @@
<caret>@deprecated("")
fun foo(p: Int) {
bar("$p ${p + 1} $0")
}
fun bar(s: String){}
@@ -0,0 +1,6 @@
@deprecated("", ReplaceWith("bar(\"\$p \${p + 1} $0\")"))
fun foo(p: Int) {
bar("$p ${p + 1} $0")
}
fun bar(s: String){}
@@ -4588,6 +4588,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("StringTemplate.kt")
public void testStringTemplate() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/deprecatedCallableAddReplaceWith/StringTemplate.kt");
doTest(fileName);
}
@TestMetadata("TwoStatements.kt")
public void testTwoStatements() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/deprecatedCallableAddReplaceWith/TwoStatements.kt");