From d48b99349b0f93ea5eb50108407b03443edc0b1b Mon Sep 17 00:00:00 2001 From: wldeh Date: Thu, 6 Apr 2023 02:26:12 -0700 Subject: [PATCH] [kotlin] Fix "Convert concatenation to raw string" intention with spaces ^KTIJ-24892 fixed closes https://github.com/JetBrains/intellij-community/pull/2383 GitOrigin-RevId: 400aa5dc4816c49e17ffe2ca781609d828683a0d --- .../codeinsights/impl/base/StringTemplateUtils.kt | 12 +----------- .../intentions/tests/K2IntentionTestGenerated.java | 10 ++++++++++ .../idea/intentions/ToRawStringLiteralIntention.kt | 12 +----------- .../idea/intentions/K1IntentionTestGenerated.java | 10 ++++++++++ .../convertToRawStringTemplate/consecutiveBreaks.kt | 2 ++ .../consecutiveBreaks.kt.after | 4 ++++ .../convertToRawStringTemplate/spaceBeforeBreak.kt | 2 ++ .../spaceBeforeBreak.kt.after | 2 ++ .../intentions/toRawStringLiteral/trailingSpace.kt | 1 - .../toRawStringLiteral/trailingSpace.kt.after | 2 ++ 10 files changed, 34 insertions(+), 23 deletions(-) create mode 100644 plugins/kotlin/idea/tests/testData/intentions/convertToRawStringTemplate/consecutiveBreaks.kt create mode 100644 plugins/kotlin/idea/tests/testData/intentions/convertToRawStringTemplate/consecutiveBreaks.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/intentions/convertToRawStringTemplate/spaceBeforeBreak.kt create mode 100644 plugins/kotlin/idea/tests/testData/intentions/convertToRawStringTemplate/spaceBeforeBreak.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/intentions/toRawStringLiteral/trailingSpace.kt.after 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 c60949b445a2..00cce4caab7a 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 @@ -210,16 +210,6 @@ private fun convertContent(element: KtStringTemplateExpression): String { return StringUtilRt.convertLineSeparators(text, "\n") } -private fun hasTrailingSpaces(text: String): Boolean { - var afterSpace = false - for (c in text) { - if ((c == '\n' || c == '\r') && afterSpace) return true - afterSpace = c == ' ' || c == '\t' - } - - return false -} - private fun KtStringTemplateEntry.value() = if (this is KtEscapeStringTemplateEntry) this.unescapedValue else text fun KtStringTemplateExpression.canBeConvertedToStringLiteral(): Boolean { @@ -238,7 +228,7 @@ fun KtStringTemplateExpression.canBeConvertedToStringLiteral(): Boolean { } val converted = convertContent(this) - return !converted.contains("\"\"\"") && !hasTrailingSpaces(converted) + return !converted.contains("\"\"\"") } fun KtStringTemplateExpression.convertToStringLiteral(): KtExpression { 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 92eb1ba21d0b..74a63baa8715 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 @@ -3359,10 +3359,20 @@ public abstract class K2IntentionTestGenerated extends AbstractK2IntentionTest { runTest("../../../idea/tests/testData/intentions/convertToRawStringTemplate/basic.kt"); } + @TestMetadata("consecutiveBreaks.kt") + public void testConsecutiveBreaks() throws Exception { + runTest("../../../idea/tests/testData/intentions/convertToRawStringTemplate/consecutiveBreaks.kt"); + } + @TestMetadata("octalEscape.kt") public void testOctalEscape() throws Exception { runTest("../../../idea/tests/testData/intentions/convertToRawStringTemplate/octalEscape.kt"); } + + @TestMetadata("spaceBeforeBreak.kt") + public void testSpaceBeforeBreak() throws Exception { + runTest("../../../idea/tests/testData/intentions/convertToRawStringTemplate/spaceBeforeBreak.kt"); + } } @RunWith(JUnit3RunnerWithInners.class) diff --git a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/intentions/ToRawStringLiteralIntention.kt b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/intentions/ToRawStringLiteralIntention.kt index bbbd4f0ac7dc..5f8ef381d3b6 100644 --- a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/intentions/ToRawStringLiteralIntention.kt +++ b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/intentions/ToRawStringLiteralIntention.kt @@ -37,7 +37,7 @@ class ToRawStringLiteralIntention : SelfTargetingOffsetIndependentIntention+ + " \n\nworld!" \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/intentions/convertToRawStringTemplate/consecutiveBreaks.kt.after b/plugins/kotlin/idea/tests/testData/intentions/convertToRawStringTemplate/consecutiveBreaks.kt.after new file mode 100644 index 000000000000..eb296b74526f --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/intentions/convertToRawStringTemplate/consecutiveBreaks.kt.after @@ -0,0 +1,4 @@ +val foo = """abc + + +world!""" \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/intentions/convertToRawStringTemplate/spaceBeforeBreak.kt b/plugins/kotlin/idea/tests/testData/intentions/convertToRawStringTemplate/spaceBeforeBreak.kt new file mode 100644 index 000000000000..7773cf2d7b04 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/intentions/convertToRawStringTemplate/spaceBeforeBreak.kt @@ -0,0 +1,2 @@ +val spaceBeforeLineBreak = "Hello, " + + " \nworld!" \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/intentions/convertToRawStringTemplate/spaceBeforeBreak.kt.after b/plugins/kotlin/idea/tests/testData/intentions/convertToRawStringTemplate/spaceBeforeBreak.kt.after new file mode 100644 index 000000000000..812d042e3e72 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/intentions/convertToRawStringTemplate/spaceBeforeBreak.kt.after @@ -0,0 +1,2 @@ +val spaceBeforeLineBreak = """Hello, +world!""" \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/intentions/toRawStringLiteral/trailingSpace.kt b/plugins/kotlin/idea/tests/testData/intentions/toRawStringLiteral/trailingSpace.kt index 71a2782e9ee6..6f5849681f9d 100644 --- a/plugins/kotlin/idea/tests/testData/intentions/toRawStringLiteral/trailingSpace.kt +++ b/plugins/kotlin/idea/tests/testData/intentions/toRawStringLiteral/trailingSpace.kt @@ -1,2 +1 @@ -// IS_APPLICABLE: false val v = "\"\\Hello, \nworld!\\\"" \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/intentions/toRawStringLiteral/trailingSpace.kt.after b/plugins/kotlin/idea/tests/testData/intentions/toRawStringLiteral/trailingSpace.kt.after new file mode 100644 index 000000000000..02f907b62933 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/intentions/toRawStringLiteral/trailingSpace.kt.after @@ -0,0 +1,2 @@ +val v = """"\Hello, +world!\"""" \ No newline at end of file