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
@@ -17,22 +17,23 @@
package org.jetbrains.kotlin.psi package org.jetbrains.kotlin.psi
import com.intellij.lang.ASTNode import com.intellij.lang.ASTNode
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.Project import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Key
import com.intellij.psi.PsiComment
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFileFactory import com.intellij.psi.PsiFileFactory
import com.intellij.psi.codeStyle.CodeStyleManager
import com.intellij.psi.util.PsiTreeUtil import com.intellij.psi.util.PsiTreeUtil
import com.intellij.util.LocalTimeCounter import com.intellij.util.LocalTimeCounter
import org.jetbrains.kotlin.resolve.ImportPath import org.jetbrains.kotlin.analyzer.ModuleInfo
import org.jetbrains.kotlin.lexer.JetKeywordToken
import org.jetbrains.kotlin.idea.JetFileType import org.jetbrains.kotlin.idea.JetFileType
import org.jetbrains.kotlin.lexer.JetKeywordToken
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.JetPsiFactory.CallableBuilder.Target import org.jetbrains.kotlin.psi.JetPsiFactory.CallableBuilder.Target
import com.intellij.openapi.util.Key import org.jetbrains.kotlin.resolve.ImportPath
import java.io.PrintWriter import java.io.PrintWriter
import java.io.StringWriter import java.io.StringWriter
import com.intellij.openapi.application.ApplicationManager
import com.intellij.psi.PsiComment
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.analyzer.ModuleInfo
public fun JetPsiFactory(project: Project?): JetPsiFactory = JetPsiFactory(project!!) public fun JetPsiFactory(project: Project?): JetPsiFactory = JetPsiFactory(project!!)
public fun JetPsiFactory(contextElement: JetElement): JetPsiFactory = JetPsiFactory(contextElement.getProject()) public fun JetPsiFactory(contextElement: JetElement): JetPsiFactory = JetPsiFactory(contextElement.getProject())
@@ -328,13 +329,6 @@ public class JetPsiFactory(private val project: Project) {
return createExpression("$" + fieldName) return createExpression("$" + fieldName)
} }
public fun createBinaryExpression(lhs: JetExpression, op: String, rhs: JetExpression): JetBinaryExpression {
val expression = createExpression("a $op b") as JetBinaryExpression
expression.getLeft().replace(lhs)
expression.getRight().replace(rhs)
return expression
}
public fun createTypeCodeFragment(text: String, context: PsiElement?): JetTypeCodeFragment { public fun createTypeCodeFragment(text: String, context: PsiElement?): JetTypeCodeFragment {
return JetTypeCodeFragment(project, "fragment.kt", text, context) return JetTypeCodeFragment(project, "fragment.kt", text, context)
} }
@@ -16,19 +16,12 @@
package org.jetbrains.kotlin.idea.intentions 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.JetToken
import org.jetbrains.kotlin.lexer.JetSingleValueToken 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 com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.lexer.JetTokens import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.JetPsiUtil
import org.jetbrains.kotlin.idea.JetBundle 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()) { 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() ?: ""}" "${expression.getLeftHandSide().getText() ?: ""} ${invertedOperation.getValue()} ${expression.getTypeReference()?.getText() ?: ""}"
) )
} }
is JetBinaryExpression -> psiFactory.createBinaryExpression( is JetBinaryExpression -> psiFactory.createExpressionByPattern("$0 $1 $2",
expression.getLeft(), expression.getLeft(),
invertedOperation.getValue(), invertedOperation.getValue(),
expression.getRight() expression.getRight()
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.JetBinaryExpression import org.jetbrains.kotlin.psi.JetBinaryExpression
import org.jetbrains.kotlin.psi.JetPsiFactory import org.jetbrains.kotlin.psi.JetPsiFactory
import org.jetbrains.kotlin.psi.JetSimpleNameExpression import org.jetbrains.kotlin.psi.JetSimpleNameExpression
import org.jetbrains.kotlin.psi.createExpressionByPattern
public class ReplaceWithOrdinaryAssignmentIntention : JetSelfTargetingIntention<JetBinaryExpression>(javaClass(), "Replace with ordinary assignment") { public class ReplaceWithOrdinaryAssignmentIntention : JetSelfTargetingIntention<JetBinaryExpression>(javaClass(), "Replace with ordinary assignment") {
override fun isApplicableTo(element: JetBinaryExpression, caretOffset: Int): Boolean { override fun isApplicableTo(element: JetBinaryExpression, caretOffset: Int): Boolean {
@@ -39,7 +40,6 @@ public class ReplaceWithOrdinaryAssignmentIntention : JetSelfTargetingIntention<
assert(assignOpText.endsWith("=")) assert(assignOpText.endsWith("="))
val operationText = assignOpText.substring(0, assignOpText.length() - 1) val operationText = assignOpText.substring(0, assignOpText.length() - 1)
val expression = factory.createBinaryExpression(left, operationText, right) element.replace(factory.createExpressionByPattern("$0 = $0 $operationText $1", left, right))
element.replace(factory.createBinaryExpression(left, "=", expression))
} }
} }
@@ -17,10 +17,6 @@
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 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.lexer.JetTokens
import org.jetbrains.kotlin.resolve.CompileTimeConstantUtils import org.jetbrains.kotlin.resolve.CompileTimeConstantUtils
import org.jetbrains.kotlin.resolve.DelegatingBindingTrace 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.copied
import org.jetbrains.kotlin.psi.psiUtil.replaced import org.jetbrains.kotlin.psi.psiUtil.replaced
import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.psi.*
public class SimplifyBooleanWithConstantsIntention : JetSelfTargetingOffsetIndependentIntention<JetBinaryExpression>( public class SimplifyBooleanWithConstantsIntention : JetSelfTargetingOffsetIndependentIntention<JetBinaryExpression>(
"simplify.boolean.with.constants", javaClass()) { "simplify.boolean.with.constants", javaClass()) {
@@ -101,9 +98,8 @@ public class SimplifyBooleanWithConstantsIntention : JetSelfTargetingOffsetIndep
if (simpleRight.canBeReducedToTrue() || simpleRight.canBeReducedToFalse()) if (simpleRight.canBeReducedToTrue() || simpleRight.canBeReducedToFalse())
return toSimplifiedBooleanBinaryExpressionWithConstantOperand(simpleRight, simpleLeft, op) return toSimplifiedBooleanBinaryExpressionWithConstantOperand(simpleRight, simpleLeft, op)
val opText = element.getOperationReference().getText() val opText = element.getOperationReference().getText() ?: return element.copied()
if (opText == null) return element.copied() return psiFactory.createExpressionByPattern("$0 $opText $1", simpleLeft, simpleRight)
return psiFactory.createBinaryExpression(simpleLeft, opText, simpleRight)
} }
else -> return element.copied() else -> return element.copied()
} }
@@ -22,6 +22,8 @@ import org.jetbrains.kotlin.psi.JetPsiFactory
import org.jetbrains.kotlin.psi.JetExpression import org.jetbrains.kotlin.psi.JetExpression
import org.jetbrains.kotlin.idea.util.JetPsiPrecedences import org.jetbrains.kotlin.idea.util.JetPsiPrecedences
import org.jetbrains.kotlin.lexer.JetTokens.* 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 import org.jetbrains.kotlin.types.expressions.OperatorConventions
public class SwapBinaryExpressionIntention : JetSelfTargetingIntention<JetBinaryExpression>(javaClass(), "Flip binary expression") { public class SwapBinaryExpressionIntention : JetSelfTargetingIntention<JetBinaryExpression>(javaClass(), "Flip binary expression") {
@@ -62,10 +64,11 @@ public class SwapBinaryExpressionIntention : JetSelfTargetingIntention<JetBinary
} }
val left = leftSubject(element)!! val left = leftSubject(element)!!
val right = rightSubject(element)!! val right = rightSubject(element)!!
val psiFactory = JetPsiFactory(element) val rightCopy = right.copied()
left.replace(psiFactory.createExpression(right.getText())) val leftCopy = left.copied()
right.replace(psiFactory.createExpression(left.getText())) left.replace(rightCopy)
element.replace(psiFactory.createBinaryExpression(element.getLeft()!!, convertedOperator, element.getRight()!!)) right.replace(leftCopy)
element.replace(JetPsiFactory(element).createExpressionByPattern("$0 $convertedOperator $1" , element.getLeft()!!, element.getRight()!!))
} }
private fun leftSubject(element: JetBinaryExpression): JetExpression? { private fun leftSubject(element: JetBinaryExpression): JetExpression? {
@@ -46,7 +46,7 @@ public class ToInfixCallIntention : JetSelfTargetingIntention<JetCallExpression>
val argument = element.getValueArguments().single().getArgumentExpression()!! val argument = element.getValueArguments().single().getArgumentExpression()!!
val name = element.getCalleeExpression()!!.getText() 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) dotQualified.replace(newCall)
} }
} }
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.idea.intentions.attributeCallReplacements
import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.psi.JetPsiFactory import org.jetbrains.kotlin.psi.JetPsiFactory
import org.jetbrains.kotlin.psi.createExpressionByPattern
public open class ReplaceBinaryInfixIntention : AttributeCallReplacementIntention("replace.binary.operator.with.infix") { 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) { override fun replaceCall(call: CallDescription, editor: Editor) {
val argument = (handleErrors(editor, call.getPositionalArguments()) ?: return)[0].getArgumentExpression() val argument = (handleErrors(editor, call.getPositionalArguments()) ?: return)[0].getArgumentExpression()
val operation = lookup(call.functionName)!! // Lookup must succeed
call.element.replace( call.element.replace(
JetPsiFactory(call.element).createBinaryExpression( JetPsiFactory(call.element).createExpressionByPattern("$0 $operation $1",
call.element.getReceiverExpression(), call.element.getReceiverExpression(), argument
lookup(call.functionName)!!, // Lookup must succeed
argument
) )
) )
} }
@@ -16,14 +16,11 @@
package org.jetbrains.kotlin.idea.intentions.attributeCallReplacements package org.jetbrains.kotlin.idea.intentions.attributeCallReplacements
import org.jetbrains.kotlin.psi.JetExpression
import org.jetbrains.kotlin.lexer.JetTokens import org.jetbrains.kotlin.lexer.JetTokens
import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.types.checker.JetTypeChecker import org.jetbrains.kotlin.types.checker.JetTypeChecker
import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.psi.JetPsiFactory import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.JetPsiUtil
import org.jetbrains.kotlin.psi.JetFunctionLiteralExpression
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
public open class ReplaceContainsIntention : AttributeCallReplacementIntention("replace.contains.with.in") { 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, argument,
"in",
call.element.getReceiverExpression() call.element.getReceiverExpression()
)) ))
} }
@@ -25,6 +25,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.jetbrains.kotlin.psi.PsiPackage.JetPsiFactory; import static org.jetbrains.kotlin.psi.PsiPackage.JetPsiFactory;
import static org.jetbrains.kotlin.psi.PsiPackage.createExpressionByPattern;
public class BranchedFoldingUtils { public class BranchedFoldingUtils {
private BranchedFoldingUtils() { private BranchedFoldingUtils() {
@@ -172,8 +173,8 @@ public class BranchedFoldingUtils {
String op = thenAssignment.getOperationReference().getText(); String op = thenAssignment.getOperationReference().getText();
JetSimpleNameExpression lhs = (JetSimpleNameExpression) thenAssignment.getLeft(); JetSimpleNameExpression lhs = (JetSimpleNameExpression) thenAssignment.getLeft();
JetBinaryExpression assignment = JetPsiFactory(ifExpression).createBinaryExpression(lhs, op, ifExpression); JetExpression assignment = createExpressionByPattern(JetPsiFactory(ifExpression), "$0 $1 $2", lhs, op, ifExpression);
JetIfExpression newIfExpression = (JetIfExpression)assignment.getRight(); JetIfExpression newIfExpression = (JetIfExpression)((JetBinaryExpression)assignment).getRight();
assertNotNull(newIfExpression); assertNotNull(newIfExpression);
@@ -276,8 +277,8 @@ public class BranchedFoldingUtils {
String op = firstAssignment.getOperationReference().getText(); String op = firstAssignment.getOperationReference().getText();
JetSimpleNameExpression lhs = (JetSimpleNameExpression) firstAssignment.getLeft(); JetSimpleNameExpression lhs = (JetSimpleNameExpression) firstAssignment.getLeft();
JetBinaryExpression assignment = JetPsiFactory(whenExpression).createBinaryExpression(lhs, op, whenExpression); JetExpression assignment = createExpressionByPattern(JetPsiFactory(whenExpression), "$0 $1 $2", lhs, op, whenExpression);
JetWhenExpression newWhenExpression = (JetWhenExpression)assignment.getRight(); JetWhenExpression newWhenExpression = (JetWhenExpression)((JetBinaryExpression)assignment).getRight();
assertNotNull(newWhenExpression); assertNotNull(newWhenExpression);
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.idea.intentions.declarations.DeclarationUtils;
import org.jetbrains.kotlin.psi.*; import org.jetbrains.kotlin.psi.*;
import static org.jetbrains.kotlin.psi.PsiPackage.JetPsiFactory; import static org.jetbrains.kotlin.psi.PsiPackage.JetPsiFactory;
import static org.jetbrains.kotlin.psi.PsiPackage.createExpressionByPattern;
public class BranchedUnfoldingUtils { public class BranchedUnfoldingUtils {
private BranchedUnfoldingUtils() { private BranchedUnfoldingUtils() {
@@ -95,8 +96,8 @@ public class BranchedUnfoldingUtils {
//noinspection ConstantConditions //noinspection ConstantConditions
JetPsiFactory psiFactory = JetPsiFactory(assignment); JetPsiFactory psiFactory = JetPsiFactory(assignment);
thenExpr.replace(psiFactory.createBinaryExpression(lhs, op, thenExpr)); thenExpr.replace(createExpressionByPattern(psiFactory, "$0 $1 $2", lhs, op, thenExpr));
elseExpr.replace(psiFactory.createBinaryExpression(lhs, op, elseExpr)); elseExpr.replace(createExpressionByPattern(psiFactory, "$0 $1 $2", lhs, op, elseExpr));
PsiElement resultElement = assignment.replace(newIfExpression); PsiElement resultElement = assignment.replace(newIfExpression);
@@ -119,7 +120,7 @@ public class BranchedUnfoldingUtils {
assertNotNull(currExpr); assertNotNull(currExpr);
//noinspection ConstantConditions //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); 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.IdeDescriptorRenderers;
import org.jetbrains.kotlin.idea.util.ShortenReferences; import org.jetbrains.kotlin.idea.util.ShortenReferences;
import org.jetbrains.kotlin.psi.*; import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.resolve.BindingContext;
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode; import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode;
import org.jetbrains.kotlin.types.JetType; import org.jetbrains.kotlin.types.JetType;
import static org.jetbrains.kotlin.psi.PsiPackage.JetPsiFactory; import static org.jetbrains.kotlin.psi.PsiPackage.JetPsiFactory;
import static org.jetbrains.kotlin.psi.PsiPackage.createExpressionByPattern;
public class DeclarationUtils { public class DeclarationUtils {
private DeclarationUtils() { private DeclarationUtils() {
@@ -62,9 +62,7 @@ public class DeclarationUtils {
JetPsiFactory psiFactory = JetPsiFactory(property); JetPsiFactory psiFactory = JetPsiFactory(property);
//noinspection ConstantConditions, unchecked //noinspection ConstantConditions, unchecked
JetBinaryExpression newInitializer = psiFactory.createBinaryExpression( JetExpression newInitializer = createExpressionByPattern(psiFactory, "$0 = $1", property.getName(), initializer);
psiFactory.createSimpleName(property.getName()), "=", initializer
);
newInitializer = (JetBinaryExpression) parent.addAfter(newInitializer, property); newInitializer = (JetBinaryExpression) parent.addAfter(newInitializer, property);
parent.addAfter(psiFactory.createNewLine(), property); parent.addAfter(psiFactory.createNewLine(), property);
@@ -85,6 +83,6 @@ public class DeclarationUtils {
ShortenReferences.DEFAULT.process(property.getTypeReference()); 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.codeInsight.editorActions.JoinRawLinesHandlerDelegate
import com.intellij.openapi.editor.Document import com.intellij.openapi.editor.Document
import com.intellij.psi.PsiFile import com.intellij.psi.PsiFile
import org.jetbrains.kotlin.psi.JetFile
import org.jetbrains.kotlin.lexer.JetTokens import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.JetBlockExpression import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.JetContainerNode
import org.jetbrains.kotlin.psi.JetWhenEntry
import org.jetbrains.kotlin.psi.JetIfExpression
import org.jetbrains.kotlin.psi.JetPsiFactory
public class JoinBlockIntoSingleStatementHandler : JoinRawLinesHandlerDelegate { public class JoinBlockIntoSingleStatementHandler : JoinRawLinesHandlerDelegate {
override fun tryJoinRawLines(document: Document, file: PsiFile, start: Int, end: Int): Int { 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 condition2 = statement.getCondition()
val body = statement.getThen() val body = statement.getThen()
if (condition1 != null && condition2 != null && body != null) { 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) condition1.replace(newCondition)
val newBody = block.replace(body) val newBody = block.replace(body)
return newBody.getTextRange()!!.getStartOffset() return newBody.getTextRange()!!.getStartOffset()