Fix handling of characters in "convert to string template"
So #KT-23045 Fixed So #KT-23046 Fixed
This commit is contained in:
@@ -86,19 +86,14 @@ open class ConvertToStringTemplateIntention : SelfTargetingOffsetIndependentInte
|
|||||||
val bindingContext = expression.analyze(BodyResolveMode.PARTIAL)
|
val bindingContext = expression.analyze(BodyResolveMode.PARTIAL)
|
||||||
val type = bindingContext.getType(expression)!!
|
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 constant = ConstantExpressionEvaluator.getConstant(expression, bindingContext)
|
||||||
val stringValue = constant?.getValue(type).toString()
|
if (constant != null) {
|
||||||
if (stringValue == expressionText) {
|
val stringValue = constant.getValue(type).toString()
|
||||||
return StringUtil.escapeStringCharacters(stringValue)
|
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
|
// 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);
|
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")
|
@TestMetadata("insertBracesForSimpleNamedExpression.kt")
|
||||||
public void testInsertBracesForSimpleNamedExpression() throws Exception {
|
public void testInsertBracesForSimpleNamedExpression() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToStringTemplate/insertBracesForSimpleNamedExpression.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToStringTemplate/insertBracesForSimpleNamedExpression.kt");
|
||||||
@@ -7539,6 +7551,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
doTest(fileName);
|
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")
|
@TestMetadata("noBracesForLastSimpleExpression.kt")
|
||||||
public void testNoBracesForLastSimpleExpression() throws Exception {
|
public void testNoBracesForLastSimpleExpression() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToStringTemplate/noBracesForLastSimpleExpression.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToStringTemplate/noBracesForLastSimpleExpression.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user