Fix handling of characters in "convert to string template"

So #KT-23045 Fixed
So #KT-23046 Fixed
This commit is contained in:
Mikhail Glukhikh
2018-04-03 13:41:16 +03:00
parent d0e99e9a3f
commit 6d9457853e
8 changed files with 34 additions and 13 deletions
@@ -86,19 +86,14 @@ open class ConvertToStringTemplateIntention : SelfTargetingOffsetIndependentInte
val bindingContext = expression.analyze(BodyResolveMode.PARTIAL)
val type = bindingContext.getType(expression)!!
if (KotlinBuiltIns.isChar(type)) {
return expressionText
.removePrefix("'")
.removeSuffix("'")
.replace("\"", "\\\"")
.replace("\\'", "'")
.run { if (forceBraces) replace("$", "\\$") else this }
}
val constant = ConstantExpressionEvaluator.getConstant(expression, bindingContext)
val stringValue = constant?.getValue(type).toString()
if (stringValue == expressionText) {
return StringUtil.escapeStringCharacters(stringValue)
if (constant != null) {
val stringValue = constant.getValue(type).toString()
if (KotlinBuiltIns.isChar(type) || stringValue == expressionText) {
return buildString {
StringUtil.escapeStringCharacters(stringValue.length, stringValue, if (forceBraces) "\"$" else "\"", this)
}
}
}
}
@@ -0,0 +1,4 @@
// IS_APPLICABLE: false
// SKIP_ERRORS_BEFORE
// SKIP_ERRORS_AFTER
val foo = '\"' + "foo"<caret>
@@ -0,0 +1 @@
val foo = "foo" + '\"'<caret>
@@ -0,0 +1 @@
val foo = "foo\""
@@ -1,2 +1,2 @@
// DISABLE-ERRORS
val foo = "foo \"bar \"\""<caret>
val foo = "foo${' "'}bar${' ""'}"
@@ -0,0 +1 @@
val foo = "\n\n" + '\n'<caret>
@@ -0,0 +1 @@
val foo = "\n\n\n"
@@ -7443,6 +7443,18 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("escapedQuote.kt")
public void testEscapedQuote() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToStringTemplate/escapedQuote.kt");
doTest(fileName);
}
@TestMetadata("escapedQuote2.kt")
public void testEscapedQuote2() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToStringTemplate/escapedQuote2.kt");
doTest(fileName);
}
@TestMetadata("insertBracesForSimpleNamedExpression.kt")
public void testInsertBracesForSimpleNamedExpression() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToStringTemplate/insertBracesForSimpleNamedExpression.kt");
@@ -7539,6 +7551,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("newLine.kt")
public void testNewLine() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToStringTemplate/newLine.kt");
doTest(fileName);
}
@TestMetadata("noBracesForLastSimpleExpression.kt")
public void testNoBracesForLastSimpleExpression() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToStringTemplate/noBracesForLastSimpleExpression.kt");