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 ""
|
if (expr == null) return ""
|
||||||
val expression = KtPsiUtil.safeDeparenthesize(expr)
|
val expression = KtPsiUtil.safeDeparenthesize(expr)
|
||||||
val expressionText = expression.text
|
val expressionText = expression.text
|
||||||
return when (expression) {
|
when (expression) {
|
||||||
is KtConstantExpression -> {
|
is KtConstantExpression -> {
|
||||||
val bindingContext = expression.analyze(BodyResolveMode.PARTIAL)
|
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)
|
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 -> {
|
is KtStringTemplateExpression -> {
|
||||||
@@ -101,17 +110,17 @@ class ConvertToStringTemplateIntention : SelfTargetingOffsetIndependentIntention
|
|||||||
StringUtil.unquoteString(expressionText)
|
StringUtil.unquoteString(expressionText)
|
||||||
}
|
}
|
||||||
if (forceBraces && base.endsWith('$')) {
|
if (forceBraces && base.endsWith('$')) {
|
||||||
base.dropLast(1) + "\\$"
|
return base.dropLast(1) + "\\$"
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
base
|
return base
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
is KtNameReferenceExpression ->
|
is KtNameReferenceExpression ->
|
||||||
"$" + (if (forceBraces) "{$expressionText}" else expressionText)
|
return "$" + (if (forceBraces) "{$expressionText}" else expressionText)
|
||||||
|
|
||||||
else -> "\${" + expressionText.replace("\n+".toRegex(), " ") + "}"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return "\${" + expressionText.replace("\n+".toRegex(), " ") + "}"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -1,3 +1,3 @@
|
|||||||
fun main(args: Array<String>){
|
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);
|
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")
|
@TestMetadata("onlyForConcat.kt")
|
||||||
public void testOnlyForConcat() throws Exception {
|
public void testOnlyForConcat() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToStringTemplate/onlyForConcat.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToStringTemplate/onlyForConcat.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user