Added ConvertToConcatenatedStringIntention
This commit is contained in:
committed by
Nikolay Krasko
parent
8e2878a6eb
commit
b2858e5b82
@@ -719,6 +719,11 @@
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.jet.plugin.intentions.ConvertToConcatenatedStringIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.jet.plugin.inspections.ExplicitGetInspection"
|
||||
displayName="Explicit 'get'"
|
||||
groupName="Kotlin"
|
||||
|
||||
@@ -324,6 +324,8 @@ convert.to.for.each.function.call.intention=Replace with a forEach function call
|
||||
convert.to.for.each.function.call.intention.family=Replace with a forEach Function Call
|
||||
convert.to.string.template=Convert concatenation to template
|
||||
convert.to.string.template.family=Convert Concatenation to Template
|
||||
convert.to.concatenated.string.intention=Convert template to concatenated string
|
||||
convert.to.concatenated.string.intention.family=Convert Template to Concatenated String
|
||||
|
||||
property.is.implemented.too.many=Has implementations
|
||||
property.is.overridden.too.many=Is overridden in subclasses
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package org.jetbrains.jet.plugin.intentions
|
||||
|
||||
import org.jetbrains.jet.lang.psi.JetStringTemplateExpression
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import org.jetbrains.jet.lang.psi.JetPsiFactory
|
||||
import org.jetbrains.jet.lang.psi.JetStringTemplateEntry
|
||||
import org.jetbrains.jet.lang.psi.JetBinaryExpression
|
||||
import org.jetbrains.jet.lang.psi.JetStringTemplateEntryWithExpression
|
||||
import org.jetbrains.jet.lang.psi.JetExpression
|
||||
import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache
|
||||
import org.jetbrains.jet.lang.resolve.BindingContextUtils
|
||||
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns
|
||||
import org.jetbrains.jet.lang.psi.JetIfExpression
|
||||
import org.jetbrains.jet.lang.psi.JetBlockExpression
|
||||
|
||||
public class ConvertToConcatenatedStringIntention : JetSelfTargetingIntention<JetStringTemplateExpression>("convert.to.concatenated.string.intention", javaClass()) {
|
||||
override fun isApplicableTo(element: JetStringTemplateExpression): Boolean {
|
||||
return element.getEntries().any { it is JetStringTemplateEntryWithExpression }
|
||||
}
|
||||
|
||||
override fun applyTo(element: JetStringTemplateExpression, editor: Editor) {
|
||||
val tripleQuoted = isTripleQuoted(element.getText()!!)
|
||||
val quote = if (tripleQuoted) "\"\"\"" else "\""
|
||||
val entries = element.getEntries()
|
||||
|
||||
val result = entries.stream()
|
||||
.withIndices()
|
||||
.map { indexToEntry ->
|
||||
val (index, entry) = indexToEntry
|
||||
entry.toSeparateString(quote, convertExplicitly = index == 0, isFinalEntry = index == entries.size - 1)
|
||||
}
|
||||
.makeString(separator = "+")
|
||||
.replaceAll("""$quote\+$quote""", "")
|
||||
|
||||
val replacement = JetPsiFactory.createExpression(element.getProject(), result)
|
||||
|
||||
element.replace(replacement)
|
||||
}
|
||||
|
||||
private fun isTripleQuoted(str: String) = str.startsWith("\"\"\"") && str.endsWith("\"\"\"")
|
||||
|
||||
private fun JetStringTemplateEntry.toSeparateString(quote: String, convertExplicitly: Boolean, isFinalEntry: Boolean): String {
|
||||
if (this !is JetStringTemplateEntryWithExpression) return getText()!!.quote(quote)
|
||||
|
||||
val expression = getExpression()!!
|
||||
|
||||
val expressionText = if (needsParenthesis(expression, isFinalEntry)) "(${expression.getText()})" else expression.getText()!!
|
||||
return if (convertExplicitly && !expression.isStringExpression()) {
|
||||
expressionText + ".toString()"
|
||||
}
|
||||
else {
|
||||
expressionText
|
||||
}
|
||||
}
|
||||
|
||||
private fun needsParenthesis(expression: JetExpression, isFinalEntry: Boolean) = when (expression) {
|
||||
is JetBinaryExpression -> true
|
||||
is JetIfExpression -> expression.getElse() !is JetBlockExpression && !isFinalEntry
|
||||
else -> false
|
||||
}
|
||||
|
||||
private fun String.quote(quote: String) = quote + this + quote
|
||||
|
||||
private fun JetExpression.isStringExpression(): Boolean {
|
||||
val context = AnalyzerFacadeWithCache.getContextForElement(this)
|
||||
val elementType = BindingContextUtils.getRecordedTypeInfo(this, context)?.getType()
|
||||
|
||||
return KotlinBuiltIns.getInstance().isString(elementType)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user