Inline Variable: Inline string template entries
#KT-7654 Fixed
This commit is contained in:
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.psi
|
||||
import com.intellij.lang.ASTNode
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.KtNodeType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.canPlaceAfterSimpleNameEntry
|
||||
|
||||
public abstract class KtExpressionImpl(node: ASTNode) : KtElementImpl(node), KtExpression {
|
||||
|
||||
@@ -38,14 +39,33 @@ public abstract class KtExpressionImpl(node: ASTNode) : KtElementImpl(node), KtE
|
||||
val parent = expression.getParent()
|
||||
|
||||
if (newElement is KtExpression) {
|
||||
when (parent) {
|
||||
is KtExpression -> {
|
||||
when {
|
||||
parent is KtStringTemplateEntryWithExpression &&
|
||||
newElement is KtStringTemplateExpression &&
|
||||
// Do not mix raw and non-raw templates
|
||||
parent.parent.firstChild.text == newElement.firstChild.text -> {
|
||||
val entriesToAdd = newElement.entries
|
||||
val templateExpression = parent.parent as KtStringTemplateExpression
|
||||
if (entriesToAdd.size > 0) {
|
||||
templateExpression.addRangeBefore(entriesToAdd.first(), entriesToAdd.last(), parent)
|
||||
val lastNewEntry = parent.prevSibling
|
||||
val nextElement = parent.nextSibling
|
||||
if (lastNewEntry is KtSimpleNameStringTemplateEntry &&
|
||||
lastNewEntry.expression != null &&
|
||||
!canPlaceAfterSimpleNameEntry(nextElement)) {
|
||||
lastNewEntry.replace(KtPsiFactory(expression).createBlockStringTemplateEntry(lastNewEntry.expression!!))
|
||||
}
|
||||
}
|
||||
parent.delete()
|
||||
}
|
||||
|
||||
parent is KtExpression -> {
|
||||
if (KtPsiUtil.areParenthesesNecessary(newElement, expression, parent)) {
|
||||
return rawReplaceHandler(KtPsiFactory(expression).createExpressionByPattern("($0)", newElement))
|
||||
}
|
||||
}
|
||||
|
||||
is KtSimpleNameStringTemplateEntry -> {
|
||||
parent is KtSimpleNameStringTemplateEntry -> {
|
||||
if (newElement !is KtSimpleNameExpression) {
|
||||
val newEntry = parent.replace(KtPsiFactory(expression).createBlockStringTemplateEntry(newElement)) as KtBlockStringTemplateEntry
|
||||
return newEntry.getExpression()!!
|
||||
|
||||
@@ -36,6 +36,7 @@ import org.jetbrains.kotlin.resolve.calls.model.ArgumentMatch
|
||||
import org.jetbrains.kotlin.types.expressions.OperatorConventions
|
||||
import java.util.*
|
||||
import kotlin.test.assertTrue
|
||||
import kotlin.text.Regex
|
||||
|
||||
// NOTE: in this file we collect only Kotlin-specific methods working with PSI and not modifying it
|
||||
|
||||
@@ -423,4 +424,11 @@ public fun KtExpression.getOutermostParenthesizerOrThis(): KtExpression {
|
||||
}?.first as KtExpression? ?: this
|
||||
}
|
||||
|
||||
public fun PsiElement.isFunctionalExpression(): Boolean = this is KtNamedFunction && nameIdentifier == null
|
||||
public fun PsiElement.isFunctionalExpression(): Boolean = this is KtNamedFunction && nameIdentifier == null
|
||||
|
||||
private val BAD_NEIGHBOUR_FOR_SIMPLE_TEMPLATE_ENTRY_PATTERN = Regex("[a-zA-Z0-9_].*")
|
||||
|
||||
fun canPlaceAfterSimpleNameEntry(element: PsiElement?): Boolean {
|
||||
val entryText = element?.text ?: return true
|
||||
return !BAD_NEIGHBOUR_FOR_SIMPLE_TEMPLATE_ENTRY_PATTERN.matches(entryText)
|
||||
}
|
||||
Reference in New Issue
Block a user