Reformat: ConvertToStringTemplateIntention
This commit is contained in:
committed by
Mikhail Glukhikh
parent
66906e2c80
commit
a3b35cbfe0
@@ -29,11 +29,14 @@ import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluat
|
|||||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||||
|
|
||||||
class ConvertToStringTemplateInspection : IntentionBasedInspection<KtBinaryExpression>(
|
class ConvertToStringTemplateInspection : IntentionBasedInspection<KtBinaryExpression>(
|
||||||
ConvertToStringTemplateIntention::class,
|
ConvertToStringTemplateIntention::class,
|
||||||
{ it -> ConvertToStringTemplateIntention.shouldSuggestToConvert(it) }
|
{ it -> ConvertToStringTemplateIntention.shouldSuggestToConvert(it) }
|
||||||
)
|
)
|
||||||
|
|
||||||
open class ConvertToStringTemplateIntention : SelfTargetingOffsetIndependentIntention<KtBinaryExpression>(KtBinaryExpression::class.java, "Convert concatenation to template") {
|
open class ConvertToStringTemplateIntention : SelfTargetingOffsetIndependentIntention<KtBinaryExpression>(
|
||||||
|
KtBinaryExpression::class.java,
|
||||||
|
"Convert concatenation to template"
|
||||||
|
) {
|
||||||
override fun isApplicableTo(element: KtBinaryExpression): Boolean {
|
override fun isApplicableTo(element: KtBinaryExpression): Boolean {
|
||||||
if (!isApplicableToNoParentCheck(element)) return false
|
if (!isApplicableToNoParentCheck(element)) return false
|
||||||
|
|
||||||
@@ -68,8 +71,7 @@ open class ConvertToStringTemplateIntention : SelfTargetingOffsetIndependentInte
|
|||||||
return if (left is KtBinaryExpression && isApplicableToNoParentCheck(left)) {
|
return if (left is KtBinaryExpression && isApplicableToNoParentCheck(left)) {
|
||||||
val leftRight = buildText(left.right, forceBraces)
|
val leftRight = buildText(left.right, forceBraces)
|
||||||
fold(left.left, leftRight + right, factory)
|
fold(left.left, leftRight + right, factory)
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
val leftText = buildText(left, forceBraces)
|
val leftText = buildText(left, forceBraces)
|
||||||
factory.createExpression("\"$leftText$right\"") as KtStringTemplateExpression
|
factory.createExpression("\"$leftText$right\"") as KtStringTemplateExpression
|
||||||
}
|
}
|
||||||
@@ -105,16 +107,14 @@ open class ConvertToStringTemplateIntention : SelfTargetingOffsetIndependentInte
|
|||||||
val base = if (expressionText.startsWith("\"\"\"") && expressionText.endsWith("\"\"\"")) {
|
val base = if (expressionText.startsWith("\"\"\"") && expressionText.endsWith("\"\"\"")) {
|
||||||
val unquoted = expressionText.substring(3, expressionText.length - 3)
|
val unquoted = expressionText.substring(3, expressionText.length - 3)
|
||||||
StringUtil.escapeStringCharacters(unquoted)
|
StringUtil.escapeStringCharacters(unquoted)
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
StringUtil.unquoteString(expressionText)
|
StringUtil.unquoteString(expressionText)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (forceBraces) {
|
if (forceBraces) {
|
||||||
if (base.endsWith('$')) {
|
if (base.endsWith('$')) {
|
||||||
return base.dropLast(1) + "\\$"
|
return base.dropLast(1) + "\\$"
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
val lastPart = expression.children.lastOrNull()
|
val lastPart = expression.children.lastOrNull()
|
||||||
if (lastPart is KtSimpleNameStringTemplateEntry) {
|
if (lastPart is KtSimpleNameStringTemplateEntry) {
|
||||||
return base.dropLast(lastPart.textLength) + "\${" + lastPart.text.drop(1) + "}"
|
return base.dropLast(lastPart.textLength) + "\${" + lastPart.text.drop(1) + "}"
|
||||||
|
|||||||
Reference in New Issue
Block a user