From de8601bb5d0cf6b64b5d12aaa641f60a465bc342 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Wed, 29 Apr 2015 19:47:38 +0300 Subject: [PATCH] Got rid of JetPsiFactory.createBinaryExpression --- .../org/jetbrains/kotlin/psi/JetPsiFactory.kt | 22 +++++++------------ ...implifyNegatedBinaryExpressionIntention.kt | 11 ++-------- .../ReplaceWithOrdinaryAssignmentIntention.kt | 4 ++-- .../SimplifyBooleanWithConstantsIntention.kt | 10 +++------ .../SwapBinaryExpressionIntention.kt | 11 ++++++---- .../idea/intentions/ToInfixCallIntention.kt | 2 +- .../ReplaceBinaryInfixIntention.kt | 8 +++---- .../ReplaceContainsIntention.kt | 8 ++----- .../BranchedFoldingUtils.java | 9 ++++---- .../BranchedUnfoldingUtils.java | 7 +++--- .../declarations/DeclarationUtils.java | 8 +++---- .../JoinBlockIntoSingleStatementHandler.kt | 9 ++------ 12 files changed, 43 insertions(+), 66 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetPsiFactory.kt b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetPsiFactory.kt index 99a9c6b5407..aa36b44daef 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetPsiFactory.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetPsiFactory.kt @@ -17,22 +17,23 @@ package org.jetbrains.kotlin.psi import com.intellij.lang.ASTNode +import com.intellij.openapi.application.ApplicationManager 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.PsiFileFactory +import com.intellij.psi.codeStyle.CodeStyleManager import com.intellij.psi.util.PsiTreeUtil import com.intellij.util.LocalTimeCounter -import org.jetbrains.kotlin.resolve.ImportPath -import org.jetbrains.kotlin.lexer.JetKeywordToken +import org.jetbrains.kotlin.analyzer.ModuleInfo 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 com.intellij.openapi.util.Key +import org.jetbrains.kotlin.resolve.ImportPath import java.io.PrintWriter 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(contextElement: JetElement): JetPsiFactory = JetPsiFactory(contextElement.getProject()) @@ -328,13 +329,6 @@ public class JetPsiFactory(private val project: Project) { 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 { return JetTypeCodeFragment(project, "fragment.kt", text, context) } diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/intentions/SimplifyNegatedBinaryExpressionIntention.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/intentions/SimplifyNegatedBinaryExpressionIntention.kt index 35e8327c754..4cee2cd55d8 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/intentions/SimplifyNegatedBinaryExpressionIntention.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/intentions/SimplifyNegatedBinaryExpressionIntention.kt @@ -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("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() diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceWithOrdinaryAssignmentIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceWithOrdinaryAssignmentIntention.kt index ee39ab032fa..e94205bfd55 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceWithOrdinaryAssignmentIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceWithOrdinaryAssignmentIntention.kt @@ -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(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)) } } diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/SimplifyBooleanWithConstantsIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/SimplifyBooleanWithConstantsIntention.kt index 735e4a9a2a7..a4b72cedec4 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/SimplifyBooleanWithConstantsIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/SimplifyBooleanWithConstantsIntention.kt @@ -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( "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() } diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/SwapBinaryExpressionIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/SwapBinaryExpressionIntention.kt index 82f93426e42..26ba8516836 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/SwapBinaryExpressionIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/SwapBinaryExpressionIntention.kt @@ -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(javaClass(), "Flip binary expression") { @@ -62,10 +64,11 @@ public class SwapBinaryExpressionIntention : JetSelfTargetingIntention 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) } } diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/attributeCallReplacements/ReplaceBinaryInfixIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/attributeCallReplacements/ReplaceBinaryInfixIntention.kt index 118070e7aa0..965644a1d8d 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/attributeCallReplacements/ReplaceBinaryInfixIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/attributeCallReplacements/ReplaceBinaryInfixIntention.kt @@ -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 ) ) } diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/attributeCallReplacements/ReplaceContainsIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/attributeCallReplacements/ReplaceContainsIntention.kt index 19d4e391a5f..264ebb4010f 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/attributeCallReplacements/ReplaceContainsIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/attributeCallReplacements/ReplaceContainsIntention.kt @@ -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() )) } diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/BranchedFoldingUtils.java b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/BranchedFoldingUtils.java index f9939ff566a..9b1d182016b 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/BranchedFoldingUtils.java +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/BranchedFoldingUtils.java @@ -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); diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/BranchedUnfoldingUtils.java b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/BranchedUnfoldingUtils.java index 82244ddc00a..10f9b3c9969 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/BranchedUnfoldingUtils.java +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/BranchedUnfoldingUtils.java @@ -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); diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/declarations/DeclarationUtils.java b/idea/src/org/jetbrains/kotlin/idea/intentions/declarations/DeclarationUtils.java index 6ec7c68ae59..8d7e8669c35 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/declarations/DeclarationUtils.java +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/declarations/DeclarationUtils.java @@ -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; } } diff --git a/idea/src/org/jetbrains/kotlin/idea/joinLines/JoinBlockIntoSingleStatementHandler.kt b/idea/src/org/jetbrains/kotlin/idea/joinLines/JoinBlockIntoSingleStatementHandler.kt index d74227ecdad..6e9ba7e142c 100644 --- a/idea/src/org/jetbrains/kotlin/idea/joinLines/JoinBlockIntoSingleStatementHandler.kt +++ b/idea/src/org/jetbrains/kotlin/idea/joinLines/JoinBlockIntoSingleStatementHandler.kt @@ -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()