Fix two exceptions in "Convert concatenation to template

So #KT-23045 Fixed
So #KT-23045 Fixed
This commit is contained in:
Toshiaki Kameyama
2018-03-22 11:30:21 +03:00
committed by Mikhail Glukhikh
parent 7328f75103
commit 26dc5840f1
4 changed files with 16 additions and 7 deletions
@@ -87,13 +87,12 @@ open class ConvertToStringTemplateIntention : SelfTargetingOffsetIndependentInte
val type = bindingContext.getType(expression)!! val type = bindingContext.getType(expression)!!
if (KotlinBuiltIns.isChar(type)) { if (KotlinBuiltIns.isChar(type)) {
val value = expressionText.removePrefix("'").removeSuffix("'") return expressionText
return when (value) { // escape double quote and unescape single one and $ .removePrefix("'")
"\"" -> "\\\"" .removeSuffix("'")
"\\'" -> "'" .replace("\"", "\\\"")
"$" -> if (forceBraces) "\\$" else "$" .replace("\\'", "'")
else -> value .run { if (forceBraces) replace("$", "\\$") else this }
}
} }
val constant = ConstantExpressionEvaluator.getConstant(expression, bindingContext) val constant = ConstantExpressionEvaluator.getConstant(expression, bindingContext)
@@ -0,0 +1,2 @@
// DISABLE-ERRORS
val foo = "foo" + ' "' + "bar" + ' ""'<caret>
@@ -0,0 +1,2 @@
// DISABLE-ERRORS
val foo = "foo \"bar \"\""<caret>
@@ -6858,6 +6858,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName); doTest(fileName);
} }
@TestMetadata("invalidChar.kt")
public void testInvalidChar() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToStringTemplate/invalidChar.kt");
doTest(fileName);
}
@TestMetadata("kt11295.kt") @TestMetadata("kt11295.kt")
public void testKt11295() throws Exception { public void testKt11295() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToStringTemplate/kt11295.kt"); String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToStringTemplate/kt11295.kt");