Replace with string templates: add curly braces if needed
#KT-37173 Fixed
This commit is contained in:
committed by
Yan Zhulanow
parent
8d3e41ae60
commit
bf3e739edf
+11
-3
@@ -13,6 +13,9 @@ import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiElementVisitor
|
||||
import org.jetbrains.kotlin.idea.KotlinBundle
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.core.canDropBraces
|
||||
import org.jetbrains.kotlin.idea.core.dropBraces
|
||||
import org.jetbrains.kotlin.idea.core.replaced
|
||||
import org.jetbrains.kotlin.idea.imports.importableFqName
|
||||
import org.jetbrains.kotlin.idea.intentions.ConvertToStringTemplateIntention
|
||||
import org.jetbrains.kotlin.idea.intentions.callExpression
|
||||
@@ -76,10 +79,15 @@ class ReplaceStringFormatWithLiteralInspection : AbstractKotlinInspection() {
|
||||
|
||||
val args = callExpression.valueArguments.mapNotNull { it.getArgumentExpression() }
|
||||
val format = args[0].text.removePrefix("\"").removeSuffix("\"")
|
||||
val replaceArgs = args.asSequence().drop(1).mapTo(LinkedList()) { ConvertToStringTemplateIntention.buildText(it, false) }
|
||||
val replaceArgs = args.asSequence().drop(1).mapTo(LinkedList()) { ConvertToStringTemplateIntention.buildText(it, true) }
|
||||
val stringLiteral = stringPlaceHolder.replace(format) { replaceArgs.pop() }
|
||||
|
||||
(qualifiedExpression ?: callExpression).also { it.replace(KtPsiFactory(it).createStringTemplate(stringLiteral)) }
|
||||
(qualifiedExpression ?: callExpression)
|
||||
.let { it.replaced(KtPsiFactory(it).createStringTemplate(stringLiteral)) }
|
||||
.entries
|
||||
.forEach {
|
||||
val blockEntry = (it as? KtBlockStringTemplateEntry)
|
||||
if (blockEntry?.canDropBraces() == true) blockEntry.dropBraces()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
// WITH_RUNTIME
|
||||
fun main() {
|
||||
val id = "abc"
|
||||
val date = "123"
|
||||
val s = String.format(<caret>"%s_%s_%s", id, date, id)
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// WITH_RUNTIME
|
||||
fun main() {
|
||||
val id = "abc"
|
||||
val date = "123"
|
||||
val s = "${id}_${date}_$id"
|
||||
}
|
||||
+5
@@ -11162,6 +11162,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/inspectionsLocal/replaceStringFormatWithLiteral"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), null, true);
|
||||
}
|
||||
|
||||
@TestMetadata("curlyBraces.kt")
|
||||
public void testCurlyBraces() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/replaceStringFormatWithLiteral/curlyBraces.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("formattableArgs.kt")
|
||||
public void testFormattableArgs() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/replaceStringFormatWithLiteral/formattableArgs.kt");
|
||||
|
||||
Reference in New Issue
Block a user