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