From 52d72ab920f97875e84ede9439e78c393607fa36 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Fri, 6 Apr 2018 06:07:32 +0300 Subject: [PATCH] Fix AE exception in "Convert too long character literal to string" So #KT-23608 Fixed --- .../kotlin/idea/quickfix/TooLongCharLiteralToStringFix.kt | 7 ++++--- .../ecapedDoubleQuotesShouldNotBeEscaped.kt | 5 +++++ .../ecapedDoubleQuotesShouldNotBeEscaped.kt.after | 5 +++++ .../kotlin/idea/quickfix/QuickFixTestGenerated.java | 6 ++++++ 4 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 idea/testData/quickfix/tooLongCharLiteralToString/ecapedDoubleQuotesShouldNotBeEscaped.kt create mode 100644 idea/testData/quickfix/tooLongCharLiteralToString/ecapedDoubleQuotesShouldNotBeEscaped.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/TooLongCharLiteralToStringFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/TooLongCharLiteralToStringFix.kt index 7053feb43e8..377dd32beb3 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/TooLongCharLiteralToStringFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/TooLongCharLiteralToStringFix.kt @@ -10,7 +10,7 @@ import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.KtPsiFactory class TooLongCharLiteralToStringFix( - element: KtConstantExpression + element: KtConstantExpression ) : KotlinQuickFixAction(element) { override fun getText(): String = "Convert too long character literal to string" @@ -22,8 +22,9 @@ class TooLongCharLiteralToStringFix( } val newStringContent = text - .slice(1..text.length - 2) - .replace("\"", "\\\"") + .slice(1..text.length - 2) + .replace("\\\"", "\"") + .replace("\"", "\\\"") val newElement = KtPsiFactory(element).createStringTemplate(newStringContent) element.replace(newElement) diff --git a/idea/testData/quickfix/tooLongCharLiteralToString/ecapedDoubleQuotesShouldNotBeEscaped.kt b/idea/testData/quickfix/tooLongCharLiteralToString/ecapedDoubleQuotesShouldNotBeEscaped.kt new file mode 100644 index 00000000000..e4550bb1751 --- /dev/null +++ b/idea/testData/quickfix/tooLongCharLiteralToString/ecapedDoubleQuotesShouldNotBeEscaped.kt @@ -0,0 +1,5 @@ +// "Convert too long character literal to string" "true" + +fun foo() { + 'foo\"bar' +} \ No newline at end of file diff --git a/idea/testData/quickfix/tooLongCharLiteralToString/ecapedDoubleQuotesShouldNotBeEscaped.kt.after b/idea/testData/quickfix/tooLongCharLiteralToString/ecapedDoubleQuotesShouldNotBeEscaped.kt.after new file mode 100644 index 00000000000..ae09326af5d --- /dev/null +++ b/idea/testData/quickfix/tooLongCharLiteralToString/ecapedDoubleQuotesShouldNotBeEscaped.kt.after @@ -0,0 +1,5 @@ +// "Convert too long character literal to string" "true" + +fun foo() { + "foo\"bar" +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index 4477a797e22..cd8255e116f 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -11297,6 +11297,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/tooLongCharLiteralToString/doubleQuotesShouldBeEscaped.kt"); doTest(fileName); } + + @TestMetadata("ecapedDoubleQuotesShouldNotBeEscaped.kt") + public void testEcapedDoubleQuotesShouldNotBeEscaped() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/tooLongCharLiteralToString/ecapedDoubleQuotesShouldNotBeEscaped.kt"); + doTest(fileName); + } } @TestMetadata("idea/testData/quickfix/typeAddition")