From 0100b7005fb3b6512c021f4bdc2b6b6066c38839 Mon Sep 17 00:00:00 2001 From: Pavel Kirpichenkov Date: Fri, 11 Oct 2024 13:08:04 +0300 Subject: [PATCH] [Kotlin] Disable 'convert to raw string' for multi-dollar strings KTIJ-30275 GitOrigin-RevId: 4f382fd5a5607727be6f125265e3c2b56edec96a --- .../codeinsights/impl/base/StringTemplateUtils.kt | 9 +++++---- .../intentions/tests/K2IntentionTestGenerated.java | 10 ++++++++++ .../idea/intentions/K1IntentionTestGenerated.java | 10 ++++++++++ .../convertToRawStringTemplate/KTIJ-30275.kt | 13 +++++++++++++ .../intentions/toRawStringLiteral/KTIJ-30275.kt | 10 ++++++++++ 5 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 plugins/kotlin/idea/tests/testData/intentions/convertToRawStringTemplate/KTIJ-30275.kt create mode 100644 plugins/kotlin/idea/tests/testData/intentions/toRawStringLiteral/KTIJ-30275.kt diff --git a/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeinsights/impl/base/StringTemplateUtils.kt b/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeinsights/impl/base/StringTemplateUtils.kt index 95a4ce90b435..783bf2157dee 100644 --- a/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeinsights/impl/base/StringTemplateUtils.kt +++ b/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeinsights/impl/base/StringTemplateUtils.kt @@ -16,11 +16,12 @@ import org.jetbrains.kotlin.idea.codeinsight.utils.isToString import org.jetbrains.kotlin.idea.references.mainReference import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.isSingleQuoted private const val TRIPLE_DOUBLE_QUOTE = "\"\"\"" /** - * Recursively visits all operands of binary [expression] with plus and, + * Recursively visits all operands of binary expression with plus and, * returns true if all operands do not have a new line. Otherwise, returns false. */ fun KtExpression.containNoNewLine(): Boolean { @@ -180,7 +181,7 @@ private fun foldOperandsOfBinaryExpression(left: KtExpression?, right: String, f context(KaSession) fun buildStringTemplateForBinaryExpression(expression: KtBinaryExpression): KtStringTemplateExpression { val rightText = buildStringTemplateForExpression(expression.right, forceBraces = false, nextText = null) - return foldOperandsOfBinaryExpression(expression.left, rightText, KtPsiFactory(expression)) + return foldOperandsOfBinaryExpression(expression.left, rightText, KtPsiFactory(expression.project)) } context(KaSession) @@ -223,8 +224,8 @@ fun KtStringTemplateExpression.canBeConvertedToStringLiteral(): Boolean { // the replacement may make things even worse, suppress the action return false } - val text = text - if (text.startsWith("\"\"\"")) return false // already raw + if (!isSingleQuoted()) return false // already raw + if (interpolationPrefix != null) return false // unsupported val escapeEntries = entries.filterIsInstance() for (entry in escapeEntries) { diff --git a/plugins/kotlin/code-insight/intentions-k2/tests/test/org/jetbrains/kotlin/idea/k2/intentions/tests/K2IntentionTestGenerated.java b/plugins/kotlin/code-insight/intentions-k2/tests/test/org/jetbrains/kotlin/idea/k2/intentions/tests/K2IntentionTestGenerated.java index 686285a9fc3b..14b9904b6728 100644 --- a/plugins/kotlin/code-insight/intentions-k2/tests/test/org/jetbrains/kotlin/idea/k2/intentions/tests/K2IntentionTestGenerated.java +++ b/plugins/kotlin/code-insight/intentions-k2/tests/test/org/jetbrains/kotlin/idea/k2/intentions/tests/K2IntentionTestGenerated.java @@ -5193,6 +5193,11 @@ public abstract class K2IntentionTestGenerated extends AbstractK2IntentionTest { runTest("../../../idea/tests/testData/intentions/convertToRawStringTemplate/consecutiveBreaks.kt"); } + @TestMetadata("KTIJ-30275.kt") + public void testKTIJ_30275() throws Exception { + runTest("../../../idea/tests/testData/intentions/convertToRawStringTemplate/KTIJ-30275.kt"); + } + @TestMetadata("multiDollar.kt") public void testMultiDollar() throws Exception { runTest("../../../idea/tests/testData/intentions/convertToRawStringTemplate/multiDollar.kt"); @@ -5267,6 +5272,11 @@ public abstract class K2IntentionTestGenerated extends AbstractK2IntentionTest { runTest("../../../idea/tests/testData/intentions/toRawStringLiteral/dollar2.kt"); } + @TestMetadata("KTIJ-30275.kt") + public void testKTIJ_30275() throws Exception { + runTest("../../../idea/tests/testData/intentions/toRawStringLiteral/KTIJ-30275.kt"); + } + @TestMetadata("quotesAndSlashes.kt") public void testQuotesAndSlashes() throws Exception { runTest("../../../idea/tests/testData/intentions/toRawStringLiteral/quotesAndSlashes.kt"); diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/intentions/K1IntentionTestGenerated.java b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/intentions/K1IntentionTestGenerated.java index 8f0fa4f17af9..3379781f17c7 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/intentions/K1IntentionTestGenerated.java +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/intentions/K1IntentionTestGenerated.java @@ -9597,6 +9597,11 @@ public abstract class K1IntentionTestGenerated extends AbstractK1IntentionTest { runTest("testData/intentions/convertToRawStringTemplate/consecutiveBreaks.kt"); } + @TestMetadata("KTIJ-30275.kt") + public void testKTIJ_30275() throws Exception { + runTest("testData/intentions/convertToRawStringTemplate/KTIJ-30275.kt"); + } + @TestMetadata("multiDollar.kt") public void testMultiDollar() throws Exception { runTest("testData/intentions/convertToRawStringTemplate/multiDollar.kt"); @@ -19519,6 +19524,11 @@ public abstract class K1IntentionTestGenerated extends AbstractK1IntentionTest { runTest("testData/intentions/toRawStringLiteral/dollar2.kt"); } + @TestMetadata("KTIJ-30275.kt") + public void testKTIJ_30275() throws Exception { + runTest("testData/intentions/toRawStringLiteral/KTIJ-30275.kt"); + } + @TestMetadata("quotesAndSlashes.kt") public void testQuotesAndSlashes() throws Exception { runTest("testData/intentions/toRawStringLiteral/quotesAndSlashes.kt"); diff --git a/plugins/kotlin/idea/tests/testData/intentions/convertToRawStringTemplate/KTIJ-30275.kt b/plugins/kotlin/idea/tests/testData/intentions/convertToRawStringTemplate/KTIJ-30275.kt new file mode 100644 index 000000000000..cfa9511a2fd8 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/intentions/convertToRawStringTemplate/KTIJ-30275.kt @@ -0,0 +1,13 @@ +// IS_APPLICABLE: false +// SKIP_ERRORS_BEFORE +// SKIP_ERRORS_AFTER +// SKIP_WARNINGS_AFTER +// COMPILER_ARGUMENTS: -Xmulti-dollar-interpolation + +fun main(args: Array) { + val x = "x" + val y = $$"$${x.length}" + + println(y) //ouput is 1 + +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/intentions/toRawStringLiteral/KTIJ-30275.kt b/plugins/kotlin/idea/tests/testData/intentions/toRawStringLiteral/KTIJ-30275.kt new file mode 100644 index 000000000000..9fd40501b072 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/intentions/toRawStringLiteral/KTIJ-30275.kt @@ -0,0 +1,10 @@ +// IS_APPLICABLE: false +// IGNORE_K1 + +fun main(args: Array) { + val x = "x" + val y = $$"$${x.length}" + + println(y) //ouput is 1 + +} \ No newline at end of file