[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
This commit is contained in:
wldeh
2023-04-11 11:07:09 +00:00
committed by intellij-monorepo-bot
parent 564658a081
commit d48b99349b
10 changed files with 34 additions and 23 deletions
@@ -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 {
@@ -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)
@@ -37,7 +37,7 @@ class ToRawStringLiteralIntention : SelfTargetingOffsetIndependentIntention<KtSt
}
val converted = convertContent(element)
return !converted.contains("\"\"\"") && !hasTrailingSpaces(converted)
return !converted.contains("\"\"\"")
}
override fun applyTo(element: KtStringTemplateExpression, editor: Editor?) {
@@ -78,15 +78,5 @@ class ToRawStringLiteralIntention : SelfTargetingOffsetIndependentIntention<KtSt
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
}
@@ -8774,10 +8774,20 @@ public abstract class K1IntentionTestGenerated extends AbstractK1IntentionTest {
runTest("testData/intentions/convertToRawStringTemplate/basic.kt");
}
@TestMetadata("consecutiveBreaks.kt")
public void testConsecutiveBreaks() throws Exception {
runTest("testData/intentions/convertToRawStringTemplate/consecutiveBreaks.kt");
}
@TestMetadata("octalEscape.kt")
public void testOctalEscape() throws Exception {
runTest("testData/intentions/convertToRawStringTemplate/octalEscape.kt");
}
@TestMetadata("spaceBeforeBreak.kt")
public void testSpaceBeforeBreak() throws Exception {
runTest("testData/intentions/convertToRawStringTemplate/spaceBeforeBreak.kt");
}
}
@RunWith(JUnit3RunnerWithInners.class)
@@ -0,0 +1,2 @@
val foo = "abc\n" <caret>+
" \n\nworld!"
@@ -0,0 +1,4 @@
val foo = """abc
world!"""
@@ -0,0 +1,2 @@
val spaceBeforeLineBreak = "Hello, " <caret>+
" \nworld!"
@@ -0,0 +1,2 @@
val spaceBeforeLineBreak = """Hello,
world!"""
@@ -1,2 +1 @@
// IS_APPLICABLE: false
val v = <caret>"\"\\Hello, \nworld!\\\""
@@ -0,0 +1,2 @@
val v = """"\Hello,
world!\""""