Got rid of JetPsiFactory.createBinaryExpression

This commit is contained in:
Valentin Kipyatkov
2015-04-29 19:47:38 +03:00
parent b0aca040d8
commit de8601bb5d
12 changed files with 43 additions and 66 deletions
@@ -16,19 +16,12 @@
package org.jetbrains.kotlin.idea.intentions
import org.jetbrains.kotlin.psi.JetPrefixExpression
import org.jetbrains.kotlin.psi.JetExpression
import org.jetbrains.kotlin.psi.JetParenthesizedExpression
import org.jetbrains.kotlin.lexer.JetToken
import org.jetbrains.kotlin.lexer.JetSingleValueToken
import org.jetbrains.kotlin.psi.JetOperationExpression
import org.jetbrains.kotlin.psi.JetIsExpression
import org.jetbrains.kotlin.psi.JetBinaryExpression
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.JetPsiUtil
import org.jetbrains.kotlin.idea.JetBundle
import org.jetbrains.kotlin.psi.JetPsiFactory
import org.jetbrains.kotlin.psi.*
public class SimplifyNegatedBinaryExpressionIntention : JetSelfTargetingOffsetIndependentIntention<JetPrefixExpression>("simplify.negated.binary.expression", javaClass()) {
@@ -85,7 +78,7 @@ public class SimplifyNegatedBinaryExpressionIntention : JetSelfTargetingOffsetIn
"${expression.getLeftHandSide().getText() ?: ""} ${invertedOperation.getValue()} ${expression.getTypeReference()?.getText() ?: ""}"
)
}
is JetBinaryExpression -> psiFactory.createBinaryExpression(
is JetBinaryExpression -> psiFactory.createExpressionByPattern("$0 $1 $2",
expression.getLeft(),
invertedOperation.getValue(),
expression.getRight()
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.JetBinaryExpression
import org.jetbrains.kotlin.psi.JetPsiFactory
import org.jetbrains.kotlin.psi.JetSimpleNameExpression
import org.jetbrains.kotlin.psi.createExpressionByPattern
public class ReplaceWithOrdinaryAssignmentIntention : JetSelfTargetingIntention<JetBinaryExpression>(javaClass(), "Replace with ordinary assignment") {
override fun isApplicableTo(element: JetBinaryExpression, caretOffset: Int): Boolean {
@@ -39,7 +40,6 @@ public class ReplaceWithOrdinaryAssignmentIntention : JetSelfTargetingIntention<
assert(assignOpText.endsWith("="))
val operationText = assignOpText.substring(0, assignOpText.length() - 1)
val expression = factory.createBinaryExpression(left, operationText, right)
element.replace(factory.createBinaryExpression(left, "=", expression))
element.replace(factory.createExpressionByPattern("$0 = $0 $operationText $1", left, right))
}
}
@@ -17,10 +17,6 @@
package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.psi.JetBinaryExpression
import org.jetbrains.kotlin.psi.JetExpression
import org.jetbrains.kotlin.psi.JetPsiFactory
import org.jetbrains.kotlin.psi.JetParenthesizedExpression
import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.resolve.CompileTimeConstantUtils
import org.jetbrains.kotlin.resolve.DelegatingBindingTrace
@@ -29,6 +25,7 @@ import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.psi.psiUtil.copied
import org.jetbrains.kotlin.psi.psiUtil.replaced
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.psi.*
public class SimplifyBooleanWithConstantsIntention : JetSelfTargetingOffsetIndependentIntention<JetBinaryExpression>(
"simplify.boolean.with.constants", javaClass()) {
@@ -101,9 +98,8 @@ public class SimplifyBooleanWithConstantsIntention : JetSelfTargetingOffsetIndep
if (simpleRight.canBeReducedToTrue() || simpleRight.canBeReducedToFalse())
return toSimplifiedBooleanBinaryExpressionWithConstantOperand(simpleRight, simpleLeft, op)
val opText = element.getOperationReference().getText()
if (opText == null) return element.copied()
return psiFactory.createBinaryExpression(simpleLeft, opText, simpleRight)
val opText = element.getOperationReference().getText() ?: return element.copied()
return psiFactory.createExpressionByPattern("$0 $opText $1", simpleLeft, simpleRight)
}
else -> return element.copied()
}
@@ -22,6 +22,8 @@ import org.jetbrains.kotlin.psi.JetPsiFactory
import org.jetbrains.kotlin.psi.JetExpression
import org.jetbrains.kotlin.idea.util.JetPsiPrecedences
import org.jetbrains.kotlin.lexer.JetTokens.*
import org.jetbrains.kotlin.psi.createExpressionByPattern
import org.jetbrains.kotlin.psi.psiUtil.copied
import org.jetbrains.kotlin.types.expressions.OperatorConventions
public class SwapBinaryExpressionIntention : JetSelfTargetingIntention<JetBinaryExpression>(javaClass(), "Flip binary expression") {
@@ -62,10 +64,11 @@ public class SwapBinaryExpressionIntention : JetSelfTargetingIntention<JetBinary
}
val left = leftSubject(element)!!
val right = rightSubject(element)!!
val psiFactory = JetPsiFactory(element)
left.replace(psiFactory.createExpression(right.getText()))
right.replace(psiFactory.createExpression(left.getText()))
element.replace(psiFactory.createBinaryExpression(element.getLeft()!!, convertedOperator, element.getRight()!!))
val rightCopy = right.copied()
val leftCopy = left.copied()
left.replace(rightCopy)
right.replace(leftCopy)
element.replace(JetPsiFactory(element).createExpressionByPattern("$0 $convertedOperator $1" , element.getLeft()!!, element.getRight()!!))
}
private fun leftSubject(element: JetBinaryExpression): JetExpression? {
@@ -46,7 +46,7 @@ public class ToInfixCallIntention : JetSelfTargetingIntention<JetCallExpression>
val argument = element.getValueArguments().single().getArgumentExpression()!!
val name = element.getCalleeExpression()!!.getText()
val newCall = JetPsiFactory(element).createExpressionByPattern("$0 $1 $2", receiver, name, argument)
val newCall = JetPsiFactory(element).createExpressionByPattern("$0 $name $1", receiver, argument)
dotQualified.replace(newCall)
}
}
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.idea.intentions.attributeCallReplacements
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.psi.JetPsiFactory
import org.jetbrains.kotlin.psi.createExpressionByPattern
public open class ReplaceBinaryInfixIntention : AttributeCallReplacementIntention("replace.binary.operator.with.infix") {
@@ -49,11 +50,10 @@ public open class ReplaceBinaryInfixIntention : AttributeCallReplacementIntentio
override fun replaceCall(call: CallDescription, editor: Editor) {
val argument = (handleErrors(editor, call.getPositionalArguments()) ?: return)[0].getArgumentExpression()
val operation = lookup(call.functionName)!! // Lookup must succeed
call.element.replace(
JetPsiFactory(call.element).createBinaryExpression(
call.element.getReceiverExpression(),
lookup(call.functionName)!!, // Lookup must succeed
argument
JetPsiFactory(call.element).createExpressionByPattern("$0 $operation $1",
call.element.getReceiverExpression(), argument
)
)
}
@@ -16,14 +16,11 @@
package org.jetbrains.kotlin.idea.intentions.attributeCallReplacements
import org.jetbrains.kotlin.psi.JetExpression
import org.jetbrains.kotlin.lexer.JetTokens
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.types.checker.JetTypeChecker
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.psi.JetPsiFactory
import org.jetbrains.kotlin.psi.JetPsiUtil
import org.jetbrains.kotlin.psi.JetFunctionLiteralExpression
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
public open class ReplaceContainsIntention : AttributeCallReplacementIntention("replace.contains.with.in") {
@@ -56,9 +53,8 @@ public open class ReplaceContainsIntention : AttributeCallReplacementIntention("
}
}
call.element.replace(psiFactory.createBinaryExpression(
call.element.replace(psiFactory.createExpressionByPattern("$0 in $1",
argument,
"in",
call.element.getReceiverExpression()
))
}
@@ -25,6 +25,7 @@ import java.util.ArrayList;
import java.util.List;
import static org.jetbrains.kotlin.psi.PsiPackage.JetPsiFactory;
import static org.jetbrains.kotlin.psi.PsiPackage.createExpressionByPattern;
public class BranchedFoldingUtils {
private BranchedFoldingUtils() {
@@ -172,8 +173,8 @@ public class BranchedFoldingUtils {
String op = thenAssignment.getOperationReference().getText();
JetSimpleNameExpression lhs = (JetSimpleNameExpression) thenAssignment.getLeft();
JetBinaryExpression assignment = JetPsiFactory(ifExpression).createBinaryExpression(lhs, op, ifExpression);
JetIfExpression newIfExpression = (JetIfExpression)assignment.getRight();
JetExpression assignment = createExpressionByPattern(JetPsiFactory(ifExpression), "$0 $1 $2", lhs, op, ifExpression);
JetIfExpression newIfExpression = (JetIfExpression)((JetBinaryExpression)assignment).getRight();
assertNotNull(newIfExpression);
@@ -276,8 +277,8 @@ public class BranchedFoldingUtils {
String op = firstAssignment.getOperationReference().getText();
JetSimpleNameExpression lhs = (JetSimpleNameExpression) firstAssignment.getLeft();
JetBinaryExpression assignment = JetPsiFactory(whenExpression).createBinaryExpression(lhs, op, whenExpression);
JetWhenExpression newWhenExpression = (JetWhenExpression)assignment.getRight();
JetExpression assignment = createExpressionByPattern(JetPsiFactory(whenExpression), "$0 $1 $2", lhs, op, whenExpression);
JetWhenExpression newWhenExpression = (JetWhenExpression)((JetBinaryExpression)assignment).getRight();
assertNotNull(newWhenExpression);
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.idea.intentions.declarations.DeclarationUtils;
import org.jetbrains.kotlin.psi.*;
import static org.jetbrains.kotlin.psi.PsiPackage.JetPsiFactory;
import static org.jetbrains.kotlin.psi.PsiPackage.createExpressionByPattern;
public class BranchedUnfoldingUtils {
private BranchedUnfoldingUtils() {
@@ -95,8 +96,8 @@ public class BranchedUnfoldingUtils {
//noinspection ConstantConditions
JetPsiFactory psiFactory = JetPsiFactory(assignment);
thenExpr.replace(psiFactory.createBinaryExpression(lhs, op, thenExpr));
elseExpr.replace(psiFactory.createBinaryExpression(lhs, op, elseExpr));
thenExpr.replace(createExpressionByPattern(psiFactory, "$0 $1 $2", lhs, op, thenExpr));
elseExpr.replace(createExpressionByPattern(psiFactory, "$0 $1 $2", lhs, op, elseExpr));
PsiElement resultElement = assignment.replace(newIfExpression);
@@ -119,7 +120,7 @@ public class BranchedUnfoldingUtils {
assertNotNull(currExpr);
//noinspection ConstantConditions
currExpr.replace(JetPsiFactory(assignment).createBinaryExpression(lhs, op, currExpr));
currExpr.replace(createExpressionByPattern(JetPsiFactory(assignment), "$0 $1 $2", lhs, op, currExpr));
}
PsiElement resultElement = assignment.replace(newWhenExpression);
@@ -23,11 +23,11 @@ import org.jetbrains.kotlin.idea.caches.resolve.ResolvePackage;
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers;
import org.jetbrains.kotlin.idea.util.ShortenReferences;
import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.resolve.BindingContext;
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode;
import org.jetbrains.kotlin.types.JetType;
import static org.jetbrains.kotlin.psi.PsiPackage.JetPsiFactory;
import static org.jetbrains.kotlin.psi.PsiPackage.createExpressionByPattern;
public class DeclarationUtils {
private DeclarationUtils() {
@@ -62,9 +62,7 @@ public class DeclarationUtils {
JetPsiFactory psiFactory = JetPsiFactory(property);
//noinspection ConstantConditions, unchecked
JetBinaryExpression newInitializer = psiFactory.createBinaryExpression(
psiFactory.createSimpleName(property.getName()), "=", initializer
);
JetExpression newInitializer = createExpressionByPattern(psiFactory, "$0 = $1", property.getName(), initializer);
newInitializer = (JetBinaryExpression) parent.addAfter(newInitializer, property);
parent.addAfter(psiFactory.createNewLine(), property);
@@ -85,6 +83,6 @@ public class DeclarationUtils {
ShortenReferences.DEFAULT.process(property.getTypeReference());
}
return newInitializer;
return (JetBinaryExpression) newInitializer;
}
}
@@ -19,13 +19,8 @@ package org.jetbrains.kotlin.idea.joinLines
import com.intellij.codeInsight.editorActions.JoinRawLinesHandlerDelegate
import com.intellij.openapi.editor.Document
import com.intellij.psi.PsiFile
import org.jetbrains.kotlin.psi.JetFile
import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.JetBlockExpression
import org.jetbrains.kotlin.psi.JetContainerNode
import org.jetbrains.kotlin.psi.JetWhenEntry
import org.jetbrains.kotlin.psi.JetIfExpression
import org.jetbrains.kotlin.psi.JetPsiFactory
import org.jetbrains.kotlin.psi.*
public class JoinBlockIntoSingleStatementHandler : JoinRawLinesHandlerDelegate {
override fun tryJoinRawLines(document: Document, file: PsiFile, start: Int, end: Int): Int {
@@ -54,7 +49,7 @@ public class JoinBlockIntoSingleStatementHandler : JoinRawLinesHandlerDelegate {
val condition2 = statement.getCondition()
val body = statement.getThen()
if (condition1 != null && condition2 != null && body != null) {
val newCondition = JetPsiFactory(pparent).createBinaryExpression(condition1, "&&", condition2)
val newCondition = JetPsiFactory(pparent).createExpressionByPattern("$0 && $1", condition1, condition2)
condition1.replace(newCondition)
val newBody = block.replace(body)
return newBody.getTextRange()!!.getStartOffset()