From e62c8a0860b61275b37569eda2bb6c2b614fae16 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Thu, 24 Mar 2016 18:55:42 +0300 Subject: [PATCH] Unescape single quote when converting to string template --- .../idea/intentions/ConvertToStringTemplateIntention.kt | 6 +++++- .../convertToStringTemplate/unescapeSingleQuote.kt | 3 +++ .../convertToStringTemplate/unescapeSingleQuote.kt.after | 3 +++ .../kotlin/idea/intentions/IntentionTestGenerated.java | 6 ++++++ 4 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 idea/testData/intentions/convertToStringTemplate/unescapeSingleQuote.kt create mode 100644 idea/testData/intentions/convertToStringTemplate/unescapeSingleQuote.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToStringTemplateIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToStringTemplateIntention.kt index a3988770a71..701f561ee56 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToStringTemplateIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToStringTemplateIntention.kt @@ -100,7 +100,11 @@ class ConvertToStringTemplateIntention : SelfTargetingOffsetIndependentIntention if (KotlinBuiltIns.isChar(type)) { val value = expressionText.removePrefix("'").removeSuffix("'") - return if (value == "\"") "\\\"" else value + return when (value) { // escape double quote and unescape single one + "\"" -> "\\\"" + "\\'" -> "'" + else -> value + } } val constant = ConstantExpressionEvaluator.getConstant(expression, bindingContext) diff --git a/idea/testData/intentions/convertToStringTemplate/unescapeSingleQuote.kt b/idea/testData/intentions/convertToStringTemplate/unescapeSingleQuote.kt new file mode 100644 index 00000000000..2116ba75daf --- /dev/null +++ b/idea/testData/intentions/convertToStringTemplate/unescapeSingleQuote.kt @@ -0,0 +1,3 @@ +fun test(p: String): String { + return p + '\'' +} \ No newline at end of file diff --git a/idea/testData/intentions/convertToStringTemplate/unescapeSingleQuote.kt.after b/idea/testData/intentions/convertToStringTemplate/unescapeSingleQuote.kt.after new file mode 100644 index 00000000000..3ec0e930b96 --- /dev/null +++ b/idea/testData/intentions/convertToStringTemplate/unescapeSingleQuote.kt.after @@ -0,0 +1,3 @@ +fun test(p: String): String { + return "$p'" +} \ 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 e201527ae0d..2852acce5b1 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -4807,6 +4807,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("unescapeSingleQuote.kt") + public void testUnescapeSingleQuote() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToStringTemplate/unescapeSingleQuote.kt"); + doTest(fileName); + } + } @TestMetadata("idea/testData/intentions/declarations")