Refactored to use creation of expression by pattern instead of plain text

This commit is contained in:
Valentin Kipyatkov
2015-10-20 18:50:54 +03:00
parent 311bd5f25d
commit 7c8967ea4e
7 changed files with 38 additions and 44 deletions
@@ -272,6 +272,16 @@ public class BuilderByPattern<TElement> {
return this return this
} }
public fun appendExpressions(expressions: Iterable<KtExpression?>, separator: String = ","): BuilderByPattern<TElement> {
for ((index, expression) in expressions.withIndex()) {
if (index > 0) {
appendFixedText(separator)
}
appendExpression(expression)
}
return this
}
public fun appendTypeReference(typeRef: KtTypeReference?): BuilderByPattern<TElement> { public fun appendTypeReference(typeRef: KtTypeReference?): BuilderByPattern<TElement> {
if (typeRef != null) { if (typeRef != null) {
patternBuilder.append("$" + arguments.size()) patternBuilder.append("$" + arguments.size())
@@ -167,26 +167,30 @@ public class OperatorToFunctionIntention : JetSelfTargetingIntention<KtExpressio
} }
private fun convertArrayAccess(element: KtArrayAccessExpression): KtExpression { private fun convertArrayAccess(element: KtArrayAccessExpression): KtExpression {
val parent = element.getParent() var expressionToReplace: KtExpression = element
val array = element.getArrayExpression()!!.getText() val transformed = KtPsiFactory(element).buildExpression {
val indices = element.getIndicesNode() appendExpression(element.arrayExpression)
val indicesText = indices.getText()?.removeSurrounding("[","]") ?: throw AssertionError("Indices node of ArrayExpression shouldn't be null: JetArrayAccessExpression = ${element.getText()}")
val transformation : String appendFixedText(".")
val replaced : KtElement
if (parent is KtBinaryExpression && parent.getOperationReference().getReferencedNameElementType() == KtTokens.EQ && element == parent.left) { val parent = element.parent
// part of an assignment if (parent is KtBinaryExpression && parent.operationReference.getReferencedNameElementType() == KtTokens.EQ && element == parent.left) {
val right = parent.getRight()!!.getText() expressionToReplace = parent
transformation = "$array.set($indicesText, $right)"
replaced = parent appendFixedText("set(")
} appendExpressions(element.indexExpressions)
else { appendFixedText(",")
transformation = "$array.get($indicesText)" appendExpression(parent.right)
replaced = element }
else {
appendFixedText("get(")
appendExpressions(element.indexExpressions)
}
appendFixedText(")")
} }
val transformed = KtPsiFactory(element).createExpression(transformation) return expressionToReplace.replace(transformed) as KtExpression
return replaced.replace(transformed) as KtExpression
} }
private fun convertCall(element: KtCallExpression): KtExpression { private fun convertCall(element: KtCallExpression): KtExpression {
@@ -17,10 +17,8 @@
package org.jetbrains.kotlin.idea.intentions package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.Editor
import com.intellij.psi.impl.source.tree.PsiErrorElementImpl
import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.utils.addIfNotNull
import java.util.* import java.util.*
public class ConvertNegatedExpressionWithDemorgansLawIntention : JetSelfTargetingOffsetIndependentIntention<KtPrefixExpression>(javaClass(), "DeMorgan Law") { public class ConvertNegatedExpressionWithDemorgansLawIntention : JetSelfTargetingOffsetIndependentIntention<KtPrefixExpression>(javaClass(), "DeMorgan Law") {
@@ -54,12 +52,7 @@ public class ConvertNegatedExpressionWithDemorgansLawIntention : JetSelfTargetin
val operands = splitBooleanSequence(baseExpression)!!.asReversed() val operands = splitBooleanSequence(baseExpression)!!.asReversed()
val newExpression = KtPsiFactory(element).buildExpression { val newExpression = KtPsiFactory(element).buildExpression {
for ((i, operand) in operands.withIndex()) { appendExpressions(operands.map { it.negate() }, separator = operatorText)
if (i > 0) {
appendFixedText(operatorText)
}
appendExpression(operand.negate())
}
} }
element.replace(newExpression) element.replace(newExpression)
@@ -46,10 +46,7 @@ public class EliminateWhenSubjectIntention : JetSelfTargetingIntention<KtWhenExp
appendFixedText("else") appendFixedText("else")
} }
else { else {
for ((i, condition) in entry.getConditions().withIndex()) { appendExpressions(entry.conditions.map { it.toExpression(subject) })
if (i > 0) appendFixedText(",")
appendExpression(condition.toExpression(subject))
}
} }
appendFixedText("->") appendFixedText("->")
@@ -23,7 +23,7 @@ import org.jetbrains.kotlin.idea.intentions.branchedTransformations.getSubjectTo
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.introduceSubject import org.jetbrains.kotlin.idea.intentions.branchedTransformations.introduceSubject
import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.*
import java.util.ArrayList import java.util.*
public class IfToWhenIntention : JetSelfTargetingRangeIntention<KtIfExpression>(javaClass(), "Replace 'if' with 'when'") { public class IfToWhenIntention : JetSelfTargetingRangeIntention<KtIfExpression>(javaClass(), "Replace 'if' with 'when'") {
override fun applicabilityRange(element: KtIfExpression): TextRange? { override fun applicabilityRange(element: KtIfExpression): TextRange? {
@@ -43,10 +43,8 @@ public class IfToWhenIntention : JetSelfTargetingRangeIntention<KtIfExpression>(
orBranches.addOrBranches(condition) orBranches.addOrBranches(condition)
} }
for ((i, expr) in orBranches.withIndex()) { appendExpressions(orBranches)
if (i > 0) appendFixedText(",")
appendExpression(expr)
}
appendFixedText("->") appendFixedText("->")
val thenBranch = ifExpression.getThen() val thenBranch = ifExpression.getThen()
@@ -68,10 +68,7 @@ public class WhenToIfIntention : JetSelfTargetingRangeIntention<KtWhenExpression
else -> { else -> {
return buildExpression { return buildExpression {
for ((i, condition) in conditions.withIndex()) { appendExpressions(conditions.map { it.toExpression(subject) }, separator = "||")
if (i > 0) appendFixedText("||")
appendExpression(condition.toExpression(subject))
}
} }
} }
} }
@@ -102,12 +102,7 @@ class ReplaceGetOrSetIntention : JetSelfTargetingRangeIntention<KtDotQualifiedEx
appendFixedText("[") appendFixedText("[")
val arguments = if (isSet) allArguments.dropLast(1) else allArguments val arguments = if (isSet) allArguments.dropLast(1) else allArguments
for ((index, argument) in arguments.withIndex()) { appendExpressions(arguments.map { it.getArgumentExpression() })
if (index > 0) {
appendFixedText(",")
}
appendExpression(argument.getArgumentExpression())
}
appendFixedText("]") appendFixedText("]")