Change to function invocation fix: use parentheses in string template

#KT-5071 Fixed
This commit is contained in:
Toshiaki Kameyama
2018-10-23 13:11:54 +03:00
committed by Mikhail Glukhikh
parent 3c75d46328
commit 9c18e24de5
13 changed files with 106 additions and 5 deletions
@@ -404,6 +404,11 @@ class KtPsiFactory @JvmOverloads constructor(private val project: Project, val m
return stringTemplateExpression.entries[0] as KtSimpleNameStringTemplateEntry
}
fun createLiteralStringTemplateEntry(literal: String): KtLiteralStringTemplateEntry {
val stringTemplateExpression = createExpression("\"$literal\"") as KtStringTemplateExpression
return stringTemplateExpression.entries[0] as KtLiteralStringTemplateEntry
}
fun createStringTemplate(content: String) = createExpression("\"$content\"") as KtStringTemplateExpression
fun createPackageDirective(fqName: FqName): KtPackageDirective {
@@ -19,10 +19,7 @@ package org.jetbrains.kotlin.idea.quickfix
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.createExpressionByPattern
import org.jetbrains.kotlin.psi.*
class ChangeToFunctionInvocationFix(element: KtExpression) : KotlinQuickFixAction<KtExpression>(element) {
override fun getFamilyName() = "Change to function invocation"
@@ -31,7 +28,21 @@ class ChangeToFunctionInvocationFix(element: KtExpression) : KotlinQuickFixActio
public override fun invoke(project: Project, editor: Editor?, file: KtFile) {
val element = element ?: return
element.replace(KtPsiFactory(file).createExpressionByPattern("$0()", element))
val psiFactory = KtPsiFactory(element)
val nextLiteralStringEntry = element.parent.nextSibling as? KtLiteralStringTemplateEntry
val nextText = nextLiteralStringEntry?.text
if (nextText != null && nextText.startsWith("(") && nextText.contains(")")) {
val parentheses = nextText.takeWhile { it != ')' } + ")"
val newNextText = nextText.removePrefix(parentheses)
if (newNextText.isNotEmpty()) {
nextLiteralStringEntry.replace(psiFactory.createLiteralStringTemplateEntry(newNextText))
} else {
nextLiteralStringEntry.delete()
}
element.replace(KtPsiFactory(file).createExpressionByPattern("$0$1", element, parentheses))
} else {
element.replace(KtPsiFactory(file).createExpressionByPattern("$0()", element))
}
}
companion object : KotlinSingleIntentionActionFactory() {
@@ -0,0 +1,6 @@
// "Change to function invocation" "true"
fun foo() {}
fun test(){
"$foo<caret>"
}
@@ -0,0 +1,6 @@
// "Change to function invocation" "true"
fun foo() {}
fun test(){
"${foo()}"
}
@@ -0,0 +1,6 @@
// "Change to function invocation" "true"
fun foo() {}
fun test(){
"$foo<caret>()"
}
@@ -0,0 +1,6 @@
// "Change to function invocation" "true"
fun foo() {}
fun test(){
"${foo()}"
}
@@ -0,0 +1,6 @@
// "Change to function invocation" "true"
fun bar(i: Int, j: Int) {}
fun test(){
"$bar<caret>(1, 2)"
}
@@ -0,0 +1,6 @@
// "Change to function invocation" "true"
fun bar(i: Int, j: Int) {}
fun test(){
"${bar(1, 2)}"
}
@@ -0,0 +1,6 @@
// "Change to function invocation" "true"
fun foo() {}
fun test(){
"$foo<caret>("
}
@@ -0,0 +1,6 @@
// "Change to function invocation" "true"
fun foo() {}
fun test(){
"${foo()}("
}
@@ -0,0 +1,6 @@
// "Change to function invocation" "true"
fun bar(i: Int, j: Int) {}
fun test(s: String){
"$bar<caret>(1, 2) sometext $s"
}
@@ -0,0 +1,6 @@
// "Change to function invocation" "true"
fun bar(i: Int, j: Int) {}
fun test(s: String){
"${bar(1, 2)} sometext $s"
}
@@ -12768,6 +12768,31 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
public void testFunInvWithoutParentheses() throws Exception {
runTest("idea/testData/quickfix/variables/changeToFunctionInvocation/funInvWithoutParentheses.kt");
}
@TestMetadata("inStringTemplate.kt")
public void testInStringTemplate() throws Exception {
runTest("idea/testData/quickfix/variables/changeToFunctionInvocation/inStringTemplate.kt");
}
@TestMetadata("inStringTemplate2.kt")
public void testInStringTemplate2() throws Exception {
runTest("idea/testData/quickfix/variables/changeToFunctionInvocation/inStringTemplate2.kt");
}
@TestMetadata("inStringTemplate3.kt")
public void testInStringTemplate3() throws Exception {
runTest("idea/testData/quickfix/variables/changeToFunctionInvocation/inStringTemplate3.kt");
}
@TestMetadata("inStringTemplate4.kt")
public void testInStringTemplate4() throws Exception {
runTest("idea/testData/quickfix/variables/changeToFunctionInvocation/inStringTemplate4.kt");
}
@TestMetadata("inStringTemplate5.kt")
public void testInStringTemplate5() throws Exception {
runTest("idea/testData/quickfix/variables/changeToFunctionInvocation/inStringTemplate5.kt");
}
}
@TestMetadata("idea/testData/quickfix/variables/changeToPropertyAccess")