"To ordinary string literal": don't add unnecessary escapes to characters in template expression

#KT-36406 Fixed
This commit is contained in:
Toshiaki Kameyama
2020-02-06 23:13:14 +09:00
committed by Yan Zhulanow
parent bf3e739edf
commit 81d01a8f8e
8 changed files with 63 additions and 6 deletions
@@ -58,10 +58,16 @@ class ToOrdinaryStringLiteralIntention : SelfTargetingOffsetIndependentIntention
editor?.caretModel?.moveToOffset(offset)
}
private fun String.escape(): String = StringUtil.convertLineSeparators(
replace("\\", "\\\\").replace("\"", "\\\""),
"\\n"
)
private fun String.escape(escapeLineSeparators: Boolean = true): String {
var text = this
text = text.replace("\\", "\\\\")
text = text.replace("\"", "\\\"")
return if (escapeLineSeparators) text.escapeLineSeparators() else text
}
private fun String.escapeLineSeparators(): String {
return StringUtil.convertLineSeparators(this, "\\n")
}
private fun getTrimIndentCall(
element: KtStringTemplateExpression,
@@ -82,9 +88,11 @@ class ToOrdinaryStringLiteralIntention : SelfTargetingOffsetIndependentIntention
}
val stringTemplateText = entries
.joinToString(separator = "") { it.text }
.joinToString(separator = "") {
if (it is KtLiteralStringTemplateEntry) it.text.escape(escapeLineSeparators = false) else it.text
}
.let { if (marginPrefix != null) it.trimMargin(marginPrefix) else it.trimIndent() }
.escape()
.escapeLineSeparators()
return TrimIndentCall(qualifiedExpression, stringTemplateText)
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun test(list: List<Int>) {
val s = <caret>"""foo ${list.joinToString(", ")} bar"""
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun test(list: List<Int>) {
val s = "foo ${list.joinToString(", ")} bar"
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
data class Column(val name: String)
fun test(columns: List<Column>) {
val sql = <caret>"""
SELECT ${columns.joinToString(", ")} FROM foo
""".trimIndent()
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
data class Column(val name: String)
fun test(columns: List<Column>) {
val sql = "SELECT ${columns.joinToString(", ")} FROM foo"
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
fun test(list: List<Int>) {
val s = <caret>"""
"\foo
${list.joinToString(",")}
"\bar
""".trimIndent()
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun test(list: List<Int>) {
val s = "\"\\foo\n${list.joinToString(",")}\n\"\\bar"
}
@@ -16168,6 +16168,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/toOrdinaryStringLiteral/quotesAndSlashes.kt");
}
@TestMetadata("quotesInExpression.kt")
public void testQuotesInExpression() throws Exception {
runTest("idea/testData/intentions/toOrdinaryStringLiteral/quotesInExpression.kt");
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("idea/testData/intentions/toOrdinaryStringLiteral/simple.kt");
@@ -16178,6 +16183,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/toOrdinaryStringLiteral/trimIndent1.kt");
}
@TestMetadata("trimIndent10.kt")
public void testTrimIndent10() throws Exception {
runTest("idea/testData/intentions/toOrdinaryStringLiteral/trimIndent10.kt");
}
@TestMetadata("trimIndent11.kt")
public void testTrimIndent11() throws Exception {
runTest("idea/testData/intentions/toOrdinaryStringLiteral/trimIndent11.kt");
}
@TestMetadata("trimIndent2.kt")
public void testTrimIndent2() throws Exception {
runTest("idea/testData/intentions/toOrdinaryStringLiteral/trimIndent2.kt");