Introduce Variable: Remove braces from replaced string template entries when possible
#KT-5310 Fixed
This commit is contained in:
+10
-3
@@ -21,11 +21,13 @@ import org.jetbrains.jet.lang.psi.JetBlockStringTemplateEntry
|
|||||||
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression
|
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression
|
||||||
import org.jetbrains.jet.lang.psi.JetPsiFactory
|
import org.jetbrains.jet.lang.psi.JetPsiFactory
|
||||||
import java.util.regex.*
|
import java.util.regex.*
|
||||||
|
import org.jetbrains.jet.lang.psi.JetStringTemplateEntryWithExpression
|
||||||
|
|
||||||
public class RemoveCurlyBracesFromTemplateIntention : JetSelfTargetingIntention<JetBlockStringTemplateEntry>(
|
public class RemoveCurlyBracesFromTemplateIntention : JetSelfTargetingIntention<JetBlockStringTemplateEntry>(
|
||||||
"remove.unnecessary.curly.brackets.from.string.template", javaClass()) {
|
"remove.unnecessary.curly.brackets.from.string.template", javaClass()) {
|
||||||
|
|
||||||
class object {
|
class object {
|
||||||
|
val INSTANCE = RemoveCurlyBracesFromTemplateIntention()
|
||||||
val pattern = Pattern.compile("[a-zA-Z0-9_].*")
|
val pattern = Pattern.compile("[a-zA-Z0-9_].*")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,10 +37,15 @@ public class RemoveCurlyBracesFromTemplateIntention : JetSelfTargetingIntention<
|
|||||||
return element.getExpression() is JetSimpleNameExpression
|
return element.getExpression() is JetSimpleNameExpression
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun applyTo(element: JetBlockStringTemplateEntry, editor: Editor) {
|
fun convertIfApplicable(element: JetBlockStringTemplateEntry): JetStringTemplateEntryWithExpression {
|
||||||
if (!isApplicableTo(element)) return
|
if (!isApplicableTo(element)) return element
|
||||||
|
|
||||||
val name = (element.getExpression() as JetSimpleNameExpression).getReferencedName()
|
val name = (element.getExpression() as JetSimpleNameExpression).getReferencedName()
|
||||||
element.replace(JetPsiFactory(element).createSimpleNameStringTemplateEntry(name))
|
val newEntry = JetPsiFactory(element).createSimpleNameStringTemplateEntry(name)
|
||||||
|
return element.replace(newEntry) as JetStringTemplateEntryWithExpression
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun applyTo(element: JetBlockStringTemplateEntry, editor: Editor) {
|
||||||
|
convertIfApplicable(element)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+9
@@ -53,6 +53,7 @@ import org.jetbrains.jet.plugin.caches.resolve.ResolvePackage;
|
|||||||
import org.jetbrains.jet.plugin.codeInsight.CodeInsightUtils;
|
import org.jetbrains.jet.plugin.codeInsight.CodeInsightUtils;
|
||||||
import org.jetbrains.jet.plugin.codeInsight.ShortenReferences;
|
import org.jetbrains.jet.plugin.codeInsight.ShortenReferences;
|
||||||
import org.jetbrains.jet.plugin.intentions.ConvertToBlockBodyAction;
|
import org.jetbrains.jet.plugin.intentions.ConvertToBlockBodyAction;
|
||||||
|
import org.jetbrains.jet.plugin.intentions.RemoveCurlyBracesFromTemplateIntention;
|
||||||
import org.jetbrains.jet.plugin.refactoring.JetNameSuggester;
|
import org.jetbrains.jet.plugin.refactoring.JetNameSuggester;
|
||||||
import org.jetbrains.jet.plugin.refactoring.JetNameValidatorImpl;
|
import org.jetbrains.jet.plugin.refactoring.JetNameValidatorImpl;
|
||||||
import org.jetbrains.jet.plugin.refactoring.JetRefactoringBundle;
|
import org.jetbrains.jet.plugin.refactoring.JetRefactoringBundle;
|
||||||
@@ -445,6 +446,14 @@ public class KotlinIntroduceVariableHandler extends KotlinIntroduceHandlerBase {
|
|||||||
else {
|
else {
|
||||||
result = (JetExpression)replace.replace(replacement);
|
result = (JetExpression)replace.replace(replacement);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PsiElement parent = result != null ? result.getParent() : null;
|
||||||
|
if (parent instanceof JetBlockStringTemplateEntry) {
|
||||||
|
JetStringTemplateEntryWithExpression newEntry =
|
||||||
|
RemoveCurlyBracesFromTemplateIntention.INSTANCE.convertIfApplicable((JetBlockStringTemplateEntry) parent);
|
||||||
|
result = newEntry.getExpression();
|
||||||
|
}
|
||||||
|
|
||||||
references.add(result);
|
references.add(result);
|
||||||
if (isActualExpression) reference.set(result);
|
if (isActualExpression) reference.set(result);
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
fun a(x: Int) {
|
fun a(x: Int) {
|
||||||
val i = x + 1
|
val i = x + 1
|
||||||
val a = i
|
val a = i
|
||||||
"it's a number ${i}"
|
"it's a number $i"
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
fun a() {
|
fun a() {
|
||||||
val i = 1
|
val i = 1
|
||||||
"it's a number ${i}"
|
"it's a number $i"
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user