More correct convertation to string template when number literals involved
This commit is contained in:
@@ -85,11 +85,20 @@ class ConvertToStringTemplateIntention : SelfTargetingOffsetIndependentIntention
|
||||
if (expr == null) return ""
|
||||
val expression = KtPsiUtil.safeDeparenthesize(expr)
|
||||
val expressionText = expression.text
|
||||
return when (expression) {
|
||||
when (expression) {
|
||||
is KtConstantExpression -> {
|
||||
val bindingContext = expression.analyze(BodyResolveMode.PARTIAL)
|
||||
val type = bindingContext.getType(expression)!!
|
||||
|
||||
if (KotlinBuiltIns.isChar(type)) {
|
||||
return expressionText.removePrefix("'").removeSuffix("'")
|
||||
}
|
||||
|
||||
val constant = ConstantExpressionEvaluator.getConstant(expression, bindingContext)
|
||||
StringUtil.escapeStringCharacters(constant?.getValue(bindingContext.getType(expression)!!).toString())
|
||||
val stringValue = constant?.getValue(type).toString()
|
||||
if (stringValue == expressionText) {
|
||||
return StringUtil.escapeStringCharacters(stringValue)
|
||||
}
|
||||
}
|
||||
|
||||
is KtStringTemplateExpression -> {
|
||||
@@ -101,17 +110,17 @@ class ConvertToStringTemplateIntention : SelfTargetingOffsetIndependentIntention
|
||||
StringUtil.unquoteString(expressionText)
|
||||
}
|
||||
if (forceBraces && base.endsWith('$')) {
|
||||
base.dropLast(1) + "\\$"
|
||||
return base.dropLast(1) + "\\$"
|
||||
}
|
||||
else {
|
||||
base
|
||||
return base
|
||||
}
|
||||
}
|
||||
|
||||
is KtNameReferenceExpression ->
|
||||
"$" + (if (forceBraces) "{$expressionText}" else expressionText)
|
||||
|
||||
else -> "\${" + expressionText.replace("\n+".toRegex(), " ") + "}"
|
||||
return "$" + (if (forceBraces) "{$expressionText}" else expressionText)
|
||||
}
|
||||
|
||||
return "\${" + expressionText.replace("\n+".toRegex(), " ") + "}"
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
fun main(args: Array<String>){
|
||||
val x = "abc0.32"
|
||||
val x = "abc${0.320}"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(p1: Int, p2: Int, p3: Int) {
|
||||
val v = <caret>"a" + 0xAAA + p1 + 123 + p2 + 1.25 + "b"
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(p1: Int, p2: Int, p3: Int) {
|
||||
val v = <caret>"a${0xAAA}${p1}123${p2}1.25b"
|
||||
}
|
||||
@@ -4675,6 +4675,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("numberLiterals.kt")
|
||||
public void testNumberLiterals() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToStringTemplate/numberLiterals.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("onlyForConcat.kt")
|
||||
public void testOnlyForConcat() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToStringTemplate/onlyForConcat.kt");
|
||||
|
||||
Reference in New Issue
Block a user