From b012f22edcaaf7d07841eddeb4e115b469a1cac3 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Fri, 29 May 2015 19:04:59 +0300 Subject: [PATCH] DeprecatedCallableAddReplaceWithIntention: fixed for string templates in expression --- ...precatedCallableAddReplaceWithIntention.kt | 20 +++++++++++++++++-- .../StringLiteral.kt | 2 +- .../StringLiteral.kt.after | 4 ++-- .../StringTemplate.kt | 6 ++++++ .../StringTemplate.kt.after | 6 ++++++ .../intentions/IntentionTestGenerated.java | 6 ++++++ 6 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 idea/testData/intentions/deprecatedCallableAddReplaceWith/StringTemplate.kt create mode 100644 idea/testData/intentions/deprecatedCallableAddReplaceWith/StringTemplate.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/DeprecatedCallableAddReplaceWithIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/DeprecatedCallableAddReplaceWithIntention.kt index a71469dfb9d..ccb406ce7b7 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/DeprecatedCallableAddReplaceWithIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/DeprecatedCallableAddReplaceWithIntention.kt @@ -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(\"") diff --git a/idea/testData/intentions/deprecatedCallableAddReplaceWith/StringLiteral.kt b/idea/testData/intentions/deprecatedCallableAddReplaceWith/StringLiteral.kt index d40b396d2f5..e101ce3c45b 100644 --- a/idea/testData/intentions/deprecatedCallableAddReplaceWith/StringLiteral.kt +++ b/idea/testData/intentions/deprecatedCallableAddReplaceWith/StringLiteral.kt @@ -1,6 +1,6 @@ @deprecated("") fun foo(p: Int) { - bar("\"$p\"\n1\r2\t3") + bar("\"\"\n1\r2\t3") } fun bar(s: String){} \ No newline at end of file diff --git a/idea/testData/intentions/deprecatedCallableAddReplaceWith/StringLiteral.kt.after b/idea/testData/intentions/deprecatedCallableAddReplaceWith/StringLiteral.kt.after index 75e2ff5c9b1..52ff363888d 100644 --- a/idea/testData/intentions/deprecatedCallableAddReplaceWith/StringLiteral.kt.after +++ b/idea/testData/intentions/deprecatedCallableAddReplaceWith/StringLiteral.kt.after @@ -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){} \ No newline at end of file diff --git a/idea/testData/intentions/deprecatedCallableAddReplaceWith/StringTemplate.kt b/idea/testData/intentions/deprecatedCallableAddReplaceWith/StringTemplate.kt new file mode 100644 index 00000000000..aa38e560b3a --- /dev/null +++ b/idea/testData/intentions/deprecatedCallableAddReplaceWith/StringTemplate.kt @@ -0,0 +1,6 @@ +@deprecated("") +fun foo(p: Int) { + bar("$p ${p + 1} $0") +} + +fun bar(s: String){} \ No newline at end of file diff --git a/idea/testData/intentions/deprecatedCallableAddReplaceWith/StringTemplate.kt.after b/idea/testData/intentions/deprecatedCallableAddReplaceWith/StringTemplate.kt.after new file mode 100644 index 00000000000..6efc0f9fd5a --- /dev/null +++ b/idea/testData/intentions/deprecatedCallableAddReplaceWith/StringTemplate.kt.after @@ -0,0 +1,6 @@ +@deprecated("", ReplaceWith("bar(\"\$p \${p + 1} $0\")")) +fun foo(p: Int) { + bar("$p ${p + 1} $0") +} + +fun bar(s: String){} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 6aa0ad18581..3c99872f24d 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -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");